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 clone [repo] | Clone the repository at location [repo] to local machine. E.g.: |
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 diff | Show file changes that are not staged yet. |
git diff --staged | Show file changes of staged files. |
git diff HEAD | Show changes between the last commit (HEAD*) and the working directory. |
git commit -m "[message] " | Commit all changes that are staged. E.g.: |
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 log | Show commit history. |
git log -[limit] | Limit the number of commits to show. E.g: |
git log --stat | List changed files and the number of lines changed in each commit. |
git log -- [file] | Show commits that have the specified file. E.g. |
git show [commit] | Show changes in the specified commit. E.g.: |
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 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 --hard | Reset 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 checkout -b [branch-name] | Create an new branch and checkout. |
git branch | List 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 -v | Show 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 pull [remote] | Download the changes from a remote branch and merge into current branch in local repository. E.g.: |
git push [remote] [branch] | Publish all local commits on current branch to remote. E.g.: |
git push --all | Publish commits on all local branches to remote. |
Configure Git | |
---|---|
git config --global -l | List all key values in the global configuration file. |
git config --global -e | Open 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-files | List 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.