coderolls Staff
coderolls Staff Hi 👋🏼, At coderolls we write tutorials about Java programming language and related technologies.

GIT Cheat Sheet

A cheat sheet for GIT, a free and open-source software for distributed version control.

Create Repository

Clone an existing repository

1
git clone https://github.com/username/repository-name.git

Create a new local repository

1
git init

Local Changes

See the changed files in your working directory

1
git diff

See changes in a particular file in your working directory

1
git diff <complete-file-path>

Stage Changes

Add local changes of a file to the next commit (Stage changes from a file)

1
git add <complete-file-path>

Add all local changes to the next commit (Stage all changes from all files)

1
git add .

Commit Changes

Commit (commit previously staged changes)

1
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.

1
git commit -m "<commit-messaege-title>"

How to commit all local changes ( even if they are not staged)

1
git commit -a

How to make changes in the last commit

1
git commit --amend

Note: The above command will not amend published commits.

Stash Changes

Stash local changes

1
git stash

Stash local changes with a message

1
git stash save "<message>"

List all stashes

1
git stash list

Reapply previously stashed changes

1
git stash apply

Reapply a particular stash from the list

1
git stash apply stash@{2}

stash@{2} is stash identifier

Reapply the latest stashed changes and remove it from your stashes

1
git stash pop

Reapply a particular stash and remove it from your stashes

1
git stash pop stash@{2}

stash@{2} is stash identifier

View a summary of a stash

1
git stash show

Delete all of your stashes

1
git stash clear

Commit History

Show all commits of a repository, the newest first

1
git log

Show changes over time for a specific file

1
git log <file-path>

Check who changed what and when in a specific file

1
git blame <file-path>

Branches and Tags

Branches

List all local branches

1
git branch

List all local and remote branches

1
git branch -a

Switch HEAD branch

1
git checkout <branch-name>

Create a new branch from the current HEAD branch

1
git branch <branch-name>

Create a new branch from the current HEAD branch and Switch to new branch

1
git checkout -b <branch-name>

Create a new tracking branch based on a remote branch

1
git checkout --track <remote/branch> 

Delete a local git branch

1
git branch -d <local-branch-name>

Delete a remote git branch

1
git push <remote> --delete <branch-name>

Note: In most of the cases the <remote> is origin.

Tag

Mark a current commit with tag

1
git tag <tag-name>

Update and Publish

List all currently configured remotes

1
git remote -v

Show information about a remote

1
git remote show <remote>

Add new remote repository

1
git remote add <remote> <url>

Change the url of an existing remote repository:

1
git remote set-url <remote> <url>

Download all changes from , but don't integrate into HEAD

1
git fetch <remote>

Download changes and directly merge/integrate into HEAD

1
git pull <remote> <branch>

Publish local changes on a remote

1
git push <remote> <branch>

Delete a branch on the remote

1
glt branch -dr <remote/branch>

Publish your tags

1
git push tags

Note: In most of the cases the <remote> is origin.

Merge and Rebase

Merge into your current HEAD

1
git merge <branch>

Rebase your current HEAD onto

1
git rebase <branch>

Abort a rebase

1
git rebase --abort

Continue a rebase after resolving conflicts

1
git rebase --continue

Use your configured merge tool to solve conflicts

1
git mergetool

Use your editor to manually solve conflicts and (after resolving) mark the file as resolved

In case of adding a file

1
git add <resolved-file>

In case of removing the file

1
git rm <resolved-file>

Undo

Discard all local changes in your working directory

1
git reset --hard HEAD

Discard local changes in a specific file

1
git checkout HEAD <file>

Revert a commit (by producing a new commit with contrary changes)

1
git revert <commit>

Reset your HEAD pointer to a previous commit …and discard all changes since then

1
git reset --hard <commit>

…and preserve all changes as unstaged changes

1
git reset <commit>

..and preserve uncommitted local changes

1
git reset --keep <commit>
Join Newsletter
Get the latest tutorials right in your inbox. We never spam!

comments powered by Disqus