15 Quick Git Commands with Examples in Linux

In this post, we will cover 15 quick git commands with examples in Linux

Git is an open-source distributed version control system and it’s free to use under GNU General Public License Version 2. In today’s context, everyone in software development uses it from small to very large projects because it keeps track of our source code history which adds more flexibility to projects. It’s usually used by developers in the software development process collaborating with the team as it has full control over version tracking of any set of files.

Git was developed by Linus Torvalds with the intention of support for distributed, non-linear workflows, the minimal number of complexities, data integrity, and flexibility in the software development.

Git Commands with Examples in Linux

Before jumping into the examples, make sure git is installed on your Linux system. Refer below, in case git is not installed.

1) Initialize Git

$ git init project_code

git-init-command-linux

The above command will initialize a new empty or existing repository. On command execution, it will create the empty repo called .git inside the given directory path. If no path is specified it will create the file in the current working directory.

After the Git initialization, next important task is to add your git global user name and email, run following commands.

$ git config --global user.name "Your Name"
$ git config --global user.email "your.email@example.com

2) Git Add

$ git add tomcat/webapps/ROOT/index.jsp

The git add command adds all the changes to the staging area so that git will know what changes in the files are going to commit.

$ git add tomcat/webapps/ROOT/*

Likewise, we can commit multiple files at once using the special characters that have special meaning in the system. In the above example * determine all files where every file in that directory will be added to the staging area.

3) Git Commit to Local Repo

$ git commit -m "First Commit done"

Git-Commit-Changes-Linux

This command will save all the files in the staging area to git the local repository that is created during git init. In the above command -m option determines the message to every git commit which is acts as a reference for each change.

4) Git Status

$ git status

Git-Status-Command-Linux

The above command will check the status of all the staged files or unstaged files. It shows the details about changes that haven’t been added to the stage and files that are ready to commit.

5) Git Differences

$ git diff

This command is used to check the difference between committed and unstaged files that help in tracking the changes we made.

$ git diff --staged

This command is used when you have already added the file to the staging area and want to check the differences between the staged and committed file.

6) Unstaged Files

$ git rm -f tomcat/webapps/ROOT/main.java

In this scenario, I want to remove the main.java file which we have added in the above example. This command will remove the file from the staging area and working directory permanently.

7) Git Logs

$ git log

Git-Log-Command-Linux

Git provides a logging system that keeps track of all the commits we made. The above command will list all the commits with their unique commit id, authors, commit message, and date of the commitment of the working branch. It’s like reviewing the history of commitment.

In order to get git log in one line, run

$ git log --oneline

8) Git Show

$ git show 1b32d3ff5fb5cef685ad10c91cb6de254cc222bf

This command shows the log message and textual differences of the commit.

Git-Show-Command-Linux

9)  Git Branches

$ git branch

Branching is the most effective pointer to a snapshot of every change in software development. In the context of a new release or bug fix for software, branching is important because it separately encapsulates your changes and makes it harder for unstable code to get merged into the main code base. This command is used to expose all the available branches in the local repository.

$ git branch "demo_git"

This command will create a new branch on our local git repository.

$ git branch -d demo_git

This command will delete the branch of your local repo.

Git-Branch-Command-Linux

10) Git Checkout

$ git checkout feature_demo
Switched to branch 'feature_demo'
$

If you ever wonder how to work on multiple branches we can simply switch branches from currently working one to another. The above command is an example to do so.

11) Git Merge     

$ git merge feature_demo

The default working branch is master for all and we want to merge all the commits of the feature_demo branch to master. Executing the command like above will merge another branch commit history to the current branch.

12) Git Remote

$ git remote add origin <link-to-Github-repo>

In git remote determine as a way of sharing local changes among the team using hosting service like GitHub or on an internal server. The above command is used to set up the connection between your local repo and a remote server. Origin in the command is the default name of remote it can be anything. Lastly, a link to the GitHub repo will be provided by the GitHub after repo is created.

13) Git Push

$ git push origin master

This command will push all of your local changes to the remote server which makes it easy in sharing changes across the team. In the command, the push is the command to push, and origin is the name of the remote that is declared while adding the remote server. Lastly, specify the name of the branch you want to push your local repo to, in my context I choose master which is the default branch.

Note: Without adding a remote server we cannot push out local commits.

14) Git Pull

$ git pull <link to repo>

This command will pull the changes done by you or your teammate on a remote server to your local repository. This way it’s easy to share different versions and bug fixes of software across teammates.

15) Git Clone

$ git clone <link to repo>

This command will make a copy of the existing repo in the remote servers to your remote device. A cloning repository is like copying the existing project of the remote server to your local directories.

Conclusion

From lower to higher way of software development version control is the most essential system that helps in minimizing the complexity of working on the team, increase efficiency in development, easy version handling, and increase flexibility. Git can be implemented in other sectors also but widely popular in software development.

These are some quick git command examples in Linux that I find more usable in my day to day of software development.

Also Read: 9 Quick ‘mv’ Command Practical Examples in Linux

Leave a Comment

five × 1 =