We show you the most common Git commands in this Cheat Sheet. It is difficult to memorize all the important Git commands, so save this article for future use.
Setup and Config
#Visit the link to download and install Git on any platform
#set a name that is identifiable for credit when review version history
git config --global user.name “[firstname lastname]”
#set an email address that will be associated with each history marker
git config --global user.email “[valid-email]”
#set automatic command line coloring for Git for easy reviewing
git config --global color.ui auto
Getting and creating projects
#initialize an existing directory as a Git repository
#retrieve an entire repository from a hosted location via URL
git clone [url]
#show modified files in working directory, staged for your next commit
#add a file as it looks now to your next commit (stage)
git add [file]
#unstage a file while retaining the changes in working directory
git reset [file]
#diff of what is changed but not staged
#diff of what is staged but not yet committed
git diff --staged
#commit your staged content as a new commit snapshot
git commit -m “[descriptive message]”
#delete the file from project and stage the removal for commit
git rm [file]
#change an existing file path and stage the move
git mv [existing-path] [new-path]
#allows you to view the history of changes to a file.
git show [filename]
Branching and merging
#List all local branches in repository. With -a: show all branches (with remote).
git branch [-a]
#Create new branch, referencing the current HEAD.
git branch [branch_name]
#Switch working directory to the specified branch. With -b: Git will create the specified branch if it does not exist.
git checkout [-b][branch_name]
#Join specified [from name] branch into your current branch (the one you are on currently).
git merge [from name]
#Remove selected branch, if it is already merged into any other. -D instead of -d forces deletion.
git branch -d [name]
#List all tags.
#Create a tag reference named name for current commit. Add commit sha to tag a specific commit instead of current one.
git tag [name] [commit sha]
#Create a tag object named name for current commit.
git tag -a [name] [commit sha]
#Remove a tag from local repository.
git tag -d [name]
Sharing and update projects
#Fetch changes from the remote, but not update tracking branches.
git fetch [remote]
#Delete remote Refs that were removed from the remote repository.
git fetch --prune [remote]
#Fetch changes from the remote and merge current branch with its upstream.
git pull [remote]
#Push local changes to the remote. Use –tags to push tags.
git push [--tags] [remote]
#Push local branch to remote repository. Set its copy as an upstream.
git push -u [remote] [branch]
#add a git URL as an alias
git remote add [alias] [url]
Inspection and comparation
#List commit history of current branch. -n count limits list to last n commits.
git log [-n count]
#An overview with reference labels and history graph. One commit per line.
git log --oneline --graph --decorate
#List commits that are present on the current branch and not merged into ref. A ref can be a branch name or a tag name.
git log ref..
#List commit that are present on ref and not merged into current branch.
git log ..ref
#List operations (e.g. checkouts or commits) made on local repository.
#Switches the current branch to the target reference, leaving a difference as an uncommitted change. When –hard is used, all changes are discarded.
git reset [--hard] [target reference]
#Create a new commit, reverting changes from the specified commit. It generates an inversion of changes.
git revert [commit sha]