Essential Git Commands Cheatsheet

Last updated on 24th September 2019

Git is an open source version control system that helps developers and development teams to track, share, organize and collaborate their work. This cheat sheet contains most commonly used git commands with some examples.

Create Repository
git init [directory]

Create a new local repository. If [directory] is omitted then the current directory is initialized as a git repository. E.g.: git init .

git clone [repo]

Clone the repository at location [repo] to local machine. E.g.: git clone https://github.com/opentechguides/cheatsheats.git

Make changes
git status List all files that are changed or newly added.
git add [file]

Stage (prepare) the specified file for next commit. You can specify a directory instead of a file to stage all changes in that directory or git add --all to stage all files in the working tree+.

git diffShow file changes that are not staged yet.
git diff --stagedShow file changes of staged files.
git diff HEADShow changes between the last commit (HEAD*) and the working directory.
git commit -m "[message] "

Commit all changes that are staged. E.g.: git commit -m "Updated file abc"

git commit -a -m "[message] "Automatically stage all file modifications and deletions and then commit those changes. New files are not committed.
git rm [file]Remove the file from the working directory and stage the deletion.
View Commits
git logShow commit history.
git log -[limit]

Limit the number of commits to show. E.g: git log -2 will show the last 2 commits.

git log --statList changed files and the number of lines changed in each commit.
git log -- [file]

Show commits that have the specified file. E.g. git log -- mydir\filename.txt

git show [commit]

Show changes in the specified commit. E.g.: git show HEAD~1

Undo Changes
git reset [file]Un-stage the file from the staging area, but keep the changes.
git revert [commit]

Undo the changes made in the specified [commit] and create a new commit. E.g.: git revert 159fc

git reset [commit]Undo changes and move the HEAD back up to the specified [commit].
git reset --hard [commit]Undo changes and move the HEAD back up to the specified [commit]. Reset files in the staging area and working directory to match the commit.
git reset --hardReset the working directory and staging area to match the latest commit.
Branching
git branch [branch-name]

Create a new branch^. This won't checkout the branch. E.g.: git branch feature_abc-nn

git checkout -b [branch-name]Create an new branch and checkout.
git branchList all branches.
git checkout [branch-name]Checkout an existing branch.
git merge [branch-name]Merge the specified to the current branch.
git branch -d [branch-name]Delete the specified branch.
Sync with Remote Repos
git remote add [name] [url]

Establish a relationship with another repository to share your work. E.g.: git remote add origin https://github.com/opentechguides/cheatsheats.git

git remote -vShow currently added remote names and URLs.
git fetch [remote]

Download latest changes from remote branch but do not merge into local repository. E.g.: git fetch origin

git pull [remote]

Download the changes from a remote branch and merge into current branch in local repository. E.g.: git pull origin

git push [remote] [branch]

Publish all local commits on current branch to remote. E.g.: git push origin master

git push --allPublish commits on all local branches to remote.
Configure Git
git config --global -lList all key values in the global configuration file.
git config --global -eOpen the global configuration file for editing.
Other Essentials
git help [command]Get help for the specified command.
git blame [file]Show who changed what in the specified file.
git ls-filesList all files that are version controlled in the current working directory.
git ls-tree -r [branch]List files in the specified branch.

* HEAD is a reference to the last commit in the currently check-out branch. It point to the tip of the branch.

^ Branch is a pointer to the tip of a set of commits.

+ Working tree is a directory in your filesystem that is associated with a git repository.


Post a comment

Comments

Nothing yet..be the first to share wisdom.