Most Popular Git Commands with Examples
1. Introduction
GIT is a popular versioning system accepted and used throughout the world. GIT based versioning can be managed using either command line or GUI tools available for every operating system. However, developers have highly preferred to use command line tools for hassle-free versioning. This article is a one-stop guide for almost every GIT command that you would need to know in order to work with GIT based versioning system in your project.
2. GIT Terminologies
Before we move on to the commands, there are certain terms related to GIT that you need to be aware of. You can skip to the commands directly in case you are already aware about the terminologies.
Repository: A collection of files that make up a project is termed as a repository. Any folder in your local storage can be converted to a GIT repository and shared across the team.
Branch: A branch is like a named copy of the repository. Developers use branches to make changes in the files. These changes are later combined into the main branch (called master branch by default).
Clone: Cloning is to create a local copy of the files that are stored online on some remote server. A cloned copy is used to easily edit the files using a custom IDE or tool.
Fork: Forking a repository is to create a copy of the repository of another user into your account and freely edit the files without the risk of affecting the main repository.
Commit: In a cloned repository, once the changes are completed, developer marks the changes as complete by committing the relevant files.
Push: The changes that the developer commits resides in the local repository. They are ready to be moved to the remote branch of the repository. This process of pushing the committed local changes is called “PUSH”.
Pull: A pull allows to grab the latest changes that have been added into the remote repository of a particular branch.
Pull request: A pull request is used to request owner or authorised developer of a specific branch to take up changes of a particular branch into the requested branch.
Merge: On approving the pull request, the code from one branch combines into the other branch. This process is popularly known as Merge. The pull request is often called as merge request owing to this terminology.
Fetch: Fetch is slightly different from a Pull request. In a Fetch request, the latest changes are downloaded onto the local repository but not merged into the files.
3. GIT Commands
The commands have been executed on Mac Bash. In case of Windows, GIT comes with a custom bash tool that can be used to work with the files using GIT commands. At few instances, output of the commands are edited in order to provide better clarity.
3.1 GIT Configuration
The below commands store the name and email to be used while committing the files. In the remote repositories, numerous people work together on the repository. These parameters help to identify the author of the changes that are being pushed to the remote repository.
1 2 | $ git config --global user.name "Java Code Geeks"$ git config --global user.email "your.email@domain.com" |
These global settings will be put in a file named .gitconfig. The settings will be used for every project that you maintain using GIT based versioning. The settings can be overwritten using the same command at any point of time without affecting the repositories. The config could be checked at any point of time by using the below command:
1 | $ git config user.name |
The user.name could be replaced by any relevant parameter that needs to be checked. It is also possible to view all the configuration list. The execution of the related command is shown below:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | $ git config --listcore.excludesfile=~/.gitignorecore.legacyheaders=falsecore.quotepath=falsemergetool.keepbackup=truepush.default=simplecolor.ui=autocolor.interactive=autorepack.usedeltabaseoffset=truealias.s=statusalias.a=!git add . && git statusalias.au=!git add -u . && git statusalias.aa=!git add . && git add -u . && git statusalias.c=commitalias.cm=commit -malias.ca=commit --amendalias.ac=!git add . && git commitalias.acm=!git add . && git commit -malias.l=log --graph --all --pretty=format:'%C(yellow)%h%C(cyan)%d%Creset %s %C(white)- %an, %ar%Creset'alias.ll=log --stat --abbrev-commitalias.lg=log --color --graph --pretty=format:'%C(bold white)%h%Creset -%C(bold green)%d%Creset %s %C(bold green)(%cr)%Creset %C(bold blue)<%an>%Creset' --abbrev-commit --date=relativealias.llg=log --color --graph --pretty=format:'%C(bold white)%H %d%Creset%n%s%n%+b%C(bold blue)%an <%ae>%Creset %C(bold green)%cr (%ci)' --abbrev-commitalias.d=diffalias.master=checkout masteralias.spull=svn rebasealias.spush=svn dcommitalias.alias=!git config --list | grep 'alias\.' | sed 's/alias\.\([^=]*\)=\(.*\)/\1\ => \2/' | sortinclude.path=~/.gitcincludeinclude.path=.githubconfiginclude.path=.gitcredentialdiff.exif.textconv=exifcredential.helper=osxkeychainuser.name=Abhishek Kothariuser.email=abk.edu@gmail.com |
Further, one can remove a specific configuration value by unsetting the relevant configuration variable. In order to do so, one needs to be in a GIT repository directory. The command to unset a specific argument is mentioned below:
1 | $ git config --unset user.name |
3.2 Initialising a GIT repository
The first step towards configuring your project versioning is to initialise the project directory as a GIT repository. Initialising of a repository basically creates a folder with the name .git. The folder would contain the complete details of the branches being cloned or created in the particular folder.
1 2 3 | $ cd /path-to-your-project/$ git initInitialized empty Git repository in /path-to-your-project/.git/ |
Here the “path-to-your-project” is your corresponding project directory where your command line tool needs to point to. Once initialised, files can be added to the local repository or pulled into the local repository folder from a remote repository. An initialised GIT repository can handle versioning for all the types of files unless it is specified to be ignored. The files can be ignored based on their type or names as required. It is necessary to create the file .gitignore in the project folder to help identify the files to be ignored from versioning.
For instance, consider a Java project under development. During commit, one would prefer to avoid the class files from being committed. In such a case one could use the command given below:
1 | echo *.class >> .gitignore |
The command will create the file as well as store *.class in it. This content in the file would mean that all the files with an extension .class will be ignored when the files are pushed to the repository.
3.3 Adding files to the index of local repository & committing them
With GIT repository once initialised, the developer has two options:
- Add the files and push the files to a repository on remote server
- Clone a remote repository in case a project already exists on the remote server
Here, a demo Java project has been created using Eclipse IDE in the folder containing the initialised repository. The project currently contains a single Java class – TestClass.java as shown in figure below.
The first step to push these changes to remote repository is to add the files to index and later commit them.
In order to add all the initial files to the index, the below command can be used.
1 | $ git add * |
On executing the above command, there would be no feedback. However, in the backend, the files are ready to be committed. The files can now be committed to the repository.
1 | $ git commit |
On executing the git commit command, a command line editor screen appears as shown below:


Hi, maybe did you miss -b in command git checkout to create a new branch? Here when you say ” git checkout could also be used to create a new branch using below command.
$ git checkout new-branch”