git commands

Useful Git Command For Admin and Developers On Linux

We’ve shown you the useful Git command where you can commit, push, add files, and also add remote origin in Git. Once you have added the remote origin git then we can start commit and push the code to GitHub or any other Git repository server. Let’s see, Few useful Git commands.

1) Initialize the Git repositories:

Git repositories would be initialized at the root with a hidden folder that contains the source files, sub-directory, heads, etc. This will behave as open-source repositories for your project.

[[email protected] ~]# git init
Initialized empty Git repository in /root/.git/
[[email protected] ~]# ls /root/.git/
branches config description HEAD hooks info objects refs

Check Out: How To Install and Enable RepoForge Repository On Linux

2) Configure the user profile:

Git allows us to configure the user profile with a username and email id that can help us to recognize who made changes. we can set it for a particular project. 

[[email protected] ~]# git config user.name "user"
[[email protected] ~]# git config user.email "[email protected]"

There’s another option to set these policies globally using the below commands.

[[email protected] ~]# git config --global user.name "uer"
[[email protected] ~]# git config --global user.email "[email protected]"

3) Create a repository:

Now we will create a repository for our project where all the files would be kept. we have created a folder called curiousviral and initialize the git inside it. 

[[email protected] ~]# mkdir curiousviral
[[email protected] ~]# cd curiousviral/
[[email protected] curiousviral]# ls -a
. ..
[[email protected] curiousviral]# git init
Initialized empty Git repository in /root/curiousviral/.git/
[[email protected] curiousviral]# ls -a
. .. .git

Check Out: Ubuntu No X11 DISPLAY variable was set, but this program performed

4) Check the Git repository status:

we can also check the created git repository status. 

[[email protected] curiousviral]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)

Now create a file and add to the repository then check the status files would be added.

[[email protected] curiousviral]# touch file file1
[[email protected] curiousviral]# ls -a
. .. file file1 .git
[[email protected] curiousviral]# git add .
[[email protected] curiousviral]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: file
# new file: file1
#

5) Commit changes to the repository:

when you commit the changes in a file. It creates a commit ID. We can type a message whatever you have made changes with ‘-m‘.

[[email protected] curiousviral]# git commit -m "added two files"
[master (root-commit) 01e54e3] added two files
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file
create mode 100644 file1

You can also check who made these changes.

[[email protected] curiousviral]# git log
commit 01e54e321b03de48d524bc01361f3b68b1e9c84c   ## Commit ID
Author: uer <[email protected]>
Date: Sat Apr 18 14:40:24 2020 -0400

added two files

Check Out: How To Install and Configure MongoDB On Ubuntu

6) Check log History:

we can also check the log in the git repository. we added a few lines in a file and committed it to the repository then check the logs.

[[email protected] curiousviral]# echo "hello git" > file
[[email protected] curiousviral]# git add .
[[email protected] curiousviral]# git commit -m "changed in file"
[master 2050cf6] changed in file
1 file changed, 1 insertion(+)
[[email protected] curiousviral]# git log --shortstat
commit 2050cf6742a1c32c3661ae17a1cb4ac611f87e16
Author: uer <[email protected]>
Date: Sat Apr 18 14:51:23 2020 -0400

changed in file

1 file changed, 1 insertion(+)

you can also use the git show command is similar to the git log.

[[email protected] curiousviral]# git show
commit 2050cf6742a1c32c3661ae17a1cb4ac611f87e16
Author: uer <[email protected]>
Date: Sat Apr 18 14:51:23 2020 -0400

changed in file

diff --git a/file b/file
index e69de29..8d0e412 100644
--- a/file
+++ b/file
@@ -0,0 +1 @@
+hello git

we can also use the below commands to check the logs with date, author, log size, and skipping the commits.

[[email protected] curiousviral]# git log --oneline
2050cf6 changed in file
01e54e3 added two files
[[email protected] curiousviral]# git log --since=2020-18-04
## Check logs since date
[[email protected] curiousviral]# git log --log-size
## check log size
[[email protected] curiousviral]# git log --author=uer
## check log from a particular author

Check Out: How To Make EBS Volume To Use On AWS Cloud

7) Create a Branch:

we can create a branch in our repository. Whenever we tested the code from this branch and later we can merge it to the master branch. you can see currently I’m in the master branch ‘*‘ means your current branch.

[[email protected] curiousviral]# git branch failure        ## create branch
[[email protected] curiousviral]# git branch                ## check current branch
failure
* master

Suppose you want to work in the failure branch. So, you will have to checkout to the branch.

[[email protected] curiousviral]# git checkout failure
Switched to branch 'failure'
[[email protected] curiousviral]# git branch
* failure
master

8) Merge Branch to Master:

Now when you’re done with your files in a different branch rather than master. You want to merge the code to the master.

[[email protected] curiousviral]# git branch                 ## checkout the branch
* failure
master
[[email protected] curiousviral]# touch failurefile              ## create a file  
[[email protected] curiousviral]# echo "testing branch merging" > failurefile 
[[email protected] curiousviral]# git add failurefile 
[[email protected] curiousviral]# git commit -m "merge file from failure to master"
[failure c13a84e] merge file from failure to master
1 file changed, 1 insertion(+)
create mode 100644 failurefile
[[email protected] curiousviral]# git checkout master         ## checkout to master
Switched to branch 'master'
[[email protected] curiousviral]# ls                      ## files is not here
file file1
[[email protected] curiousviral]# git merge master failure     ## merge branch
Updating 2050cf6..c13a84e
Fast-forward
failurefile | 1 +
1 file changed, 1 insertion(+)
create mode 100644 failurefile
[[email protected] curiousviral]# ls                     ## file here after merging
failurefile file file1

Check Out: How To Install Git (Distributed Version Control) In Ubuntu 19.04

9) Add the Origin:

when you need to add the origin to push the data. we have to add the origin. we will send these files to the GitHub repository. 

[[email protected] ~]# git remote add origin https://github.com/Aryango/curiousviral.git
[[email protected] ~]# git remote -v
origin https://github.com/Aryango/curiousviral.git (fetch)
origin https://github.com/Aryango/curiousviral.git (push)

Now push the data to the master branch of curiousviral on GitHub.

[[email protected] curiousviral]# git push -u origin master

10) Check staging Area:

When we just create the files in the Git repository and not added to the git. These files remain in a Staging area until we don’t add them to the Git repository.

[[email protected] curiousviral]# touch stag
[[email protected] curiousviral]# git status -s
?? stag

?? It means files just created and not added to the Git repository.

[[email protected] curiousviral]# git add stag
[[email protected] curiousviral]# git status -s
A stag

A” Means files have been added and it’s in the staging area.

Check Out: How To Change Gitlab IP URL With Hostname In Linux

11) Delete the Branch:

Suppose your work is done in the branch you created ‘failure’ and now you want to delete it. we can use the ‘-D‘ option to delete that branch when we checkout the master branch using git checkout master.

[[email protected] curiousviral]# git branch
failure
* master
[[email protected] curiousviral]# git branch -D failure
Deleted branch failure (was c13a84e).
[[email protected] curiousviral]# git branch
* master

12) Clone a directory:

You can clone an existing directory git clone command. I’m going to download the curiousviral repository into the folder test.

[[email protected] ~]# git clone {git:url}
[[email protected] ~]# git clone https://github.com/Aryango/curiousviral.git /test

13) Pull changes from the repository:

Suppose, you want to pull the changes which were made in the repository. So, we can achieve this with the git pull command inside the existing folder.

[[email protected] ~]# git pull

Check Out: How To Install Prometheus Monitoring Tool On Linux

14) Push changes to the repository:

Suppose, you want to push the changes which you have made in the repository. So, Simply use the git push command.

[[email protected] ~]# git push

15) Display the remote repository:

We can also check the remote repository added on the server using the git remote command.

[[email protected]it ~]# git remote
origin
[[email protected] ~]# git remote --verbose
origin https://github.com/Aryango/Loading.git (fetch)
origin https://github.com/Aryango/Loading.git (push)

 

You’re done add origin remote git commit and push

LEAVE A REPLY

Please enter your comment!
Please enter your name here