20 Git Commands For Beginners

Git Commands Cheatsheet : list of most common git commands

Git is the most popular version control system for open source repository. This is a very old open-source version control system that is extremely fast.

The design of git is distributed version control that makes it the preferred choice for open source projects that have contributors across the world.

A lot of people do not know that GIT is 10+-year-old version control system. It was developed and designed by Linux Torvalds for Linux kernel development.

Many beginners may confuse git with Github too. Github is just a cloud-based git VCS provider that have become very popular in recent years. There are dozens of git as service providers available.

You can learn more in depth about git using these git books.

Clone A Remote Repo To Your Local

git clone [repository-url]

Refresh Your Repo To Get Latest From Remote Repo

git pull

Refresh Repo From Remote With Preserving Local Changes

This is the most common scenario for developers where we have some local changes and do not want to lose them. This requires the use of stash feature in git. You can do following steps

First, save your local changes in a stash using stash command

git stash

then do a pull from the remote

git pull

pop the changes from stash - this will result in the merge of your changes. This may show you conflicts that you can resolve locally.

git stash pop

List All Branches In The Repo / Show My Current Branch

Below command will list all the git branches in your repository and show a star (*) in front to current working branch.

git branch -a

How To Switch Branch?

git checkout [branch-name]

Check The Status For Current Repository / Know Local Modified Files / Files To Add

git status

Files that are already part of repo and change will show here as “modified”

New files will show in "Untracked files:” section

Add New Files To Version Control

git add filename

Check The Log Messages / Browse Past Commit Information

git log

The above command will throw all the messages on the screen scattered in multiple lines. You can make it pretty by using one line option

git log --oneline

Commit Changes To Repo / Check-In Changes

git commit -m “Message to commit this file or directory” [directory-or-filename]

Discard A Locally Modified File With Latest File On Repo / Revert Local Changes To Use Repo Latest

For single file just use checkout command

git checkout [filename]

For discarding, all local un-committed changes use below command

git checkout -- .

How To Create An Empty Directory In Git Repo

Git does not allow an empty directory to be committed in the repo. Therefore we can use a hack for it. To create an empty directory you need to add a file called .gitkeep in the empty directory (which means it will not remain empty) You can use touch command to create empty file

cd DirectoryToKeepEmpty
touch .gitkeep

How To Export Repository Sources In A Zip File

You can use git archive command option for this, below command will archive all source from the master branch. This will export a zip file name myarchive.zip that will contain only project files (no git meta files will be included)

git archive --format zip --output myarchive.zip master

How To Change Commit Message Of Locally Committed Changes

You can change the last commit message using amend command

git commit --amend -m "New commit message"

IF you need to make changes to several commits including commits already pushed to remote then use rebase command like this.

The first step, check the output of git log to find out how many commits need to be modified.

git log --oneline

Make a note of the 'oldest commit' to be modified (let us say its 5th commit)

git rebase --interactive HEAD~5

This will pop an editor for you to make all commit message changes in one single file. Make all necessary changes and save the file. After this continue the rebase using below command.

git rebase --continue

How To Create A Patch From Local Repo Changes In Git

Creating patch is very easy in git. You can use diff command to do it. For local un-committed (un-staged) changes use below command

git diff > filename.patch

For locally committed changes (staged changes) use below command

git diff --cached > filename.patch

How To Apply Git Patch

Use apply command

git apply filename.patch

Please note that I have used .patch as file name extension. This is just a convention, it can be any extension or no extension at all. Git does not care about the file extension for apply command.

How To Compare A File With A Past Revision

This is easy in git, just do a diff on the version like below. This command will compare the current latest version on your local repo to version cf39cd1

git diff cf39cd1 filename

How To Remove Untracked Files

Use git clean with force option, Below command will delete all untracked files and directories in your local working copy.

git clean -f -d

You can use git status command to check before and after to confirm. Not using force option may show you this error

fatal: clean.requireForce defaults to true and neither -i, -n, nor -f given; refusing to clean

How To Merge A Specific Commit

You can use cherry-pick to do specific commit merges. Run the below command, it will merge the commit [9e6fd16] in current branch

git cherry-pick 9e6fd16

How To Find The Branches That Contain A Commit

You can do this by using contains option in git branch command, the blow command will show you local and remote branches that contain the commit.

git branch -r —contains 8aa1ad932

How To Find A Previously Committed File Based on Git History

With below command, you can search inside all previously committed file for a specific text.

git rev-list --all | xargs git grep "StringBuffer"

Like this page? Feel free to print it and stick on your desk or share it with your friends.

What is your favorite git command? Did I miss it here? Please share in comments and I will be glad to add it.


Tutorials 1794129799774572786

Post a Comment Default Comments