A cheat sheet for GIT, a free and open-source software for distributed version control.
Create Repository
Clone an existing repository
git clone https://github.com/username/repository-name.git
Create a new local repository
git init
Local Changes
See the changed files in your working directory
git diff
See changes in a particular file in your working directory
git diff <complete-file-path>
Stage Changes
Add local changes of a file to the next commit (Stage changes from a file)
git add <complete-file-path>
Add all local changes to the next commit (Stage all changes from all files)
git add .
Commit Changes
Commit (commit previously staged changes)
git commit
The above command will open a Vim prompt to add a commit message title and commit message description.
If you want to commit without entering the vim prompt, use the below command.
git commit -m "<commit-messaege-title>"
How to commit all local changes ( even if they are not staged)
git commit -a
How to make changes in the last commit
git commit --amend
Note: The above command will not amend published commits.
Stash Changes
Stash local changes
git stash
Stash local changes with a message
git stash save "<message>"
List all stashes
git stash list
Reapply previously stashed changes
git stash apply
Reapply a particular stash from the list
git stash apply stash@{2}
stash@{2}
is stash identifier
Reapply the latest stashed changes and remove it from your stashes
git stash pop
Reapply a particular stash and remove it from your stashes
git stash pop stash@{2}
stash@{2}
is stash identifier
View a summary of a stash
git stash show
Delete all of your stashes
git stash clear
Commit History
Show all commits of a repository, the newest first
git log
Show changes over time for a specific file
git log <file-path>
Check who changed what and when in a specific file
git blame <file-path>
Branches and Tags
Branches
List all local branches
git branch
List all local and remote branches
git branch -a
Switch HEAD branch
git checkout <branch-name>
Create a new branch from the current HEAD branch
git branch <branch-name>
Create a new branch from the current HEAD branch and Switch to new branch
git checkout -b <branch-name>
Create a new tracking branch based on a remote branch
git checkout --track <remote/branch>
git branch -d <local-branch-name>
git push <remote> --delete <branch-name>
Note: In most of the cases the
<remote>
isorigin
.
Tag
Mark a current commit with tag
git tag <tag-name>
Update and Publish
List all currently configured remotes
git remote -v
Show information about a remote
git remote show <remote>
Add new remote repository
git remote add <remote> <url>
Change the url of an existing remote repository:
git remote set-url <remote> <url>
Download all changes from
git fetch <remote>
Download changes and directly merge/integrate into HEAD
git pull <remote> <branch>
Publish local changes on a remote
git push <remote> <branch>
Delete a branch on the remote
glt branch -dr <remote/branch>
Publish your tags
git push tags
Note: In most of the cases the
<remote>
isorigin
.
Merge and Rebase
Merge
git merge <branch>
Rebase your current HEAD onto
git rebase <branch>
Abort a rebase
git rebase --abort
Continue a rebase after resolving conflicts
git rebase --continue
Use your configured merge tool to solve conflicts
git mergetool
Use your editor to manually solve conflicts and (after resolving) mark the file as resolved
In case of adding a file
git add <resolved-file>
In case of removing the file
git rm <resolved-file>
Undo
Discard all local changes in your working directory
git reset --hard HEAD
Discard local changes in a specific file
git checkout HEAD <file>
Revert a commit (by producing a new commit with contrary changes)
git revert <commit>
Reset your HEAD pointer to a previous commit …and discard all changes since then
git reset --hard <commit>
…and preserve all changes as unstaged changes
git reset <commit>
..and preserve uncommitted local changes
git reset --keep <commit>