DEV Community

Cover image for Git Cheat Sheet👨‍💻
Satish Umagol
Satish Umagol

Posted on

Git Cheat Sheet👨‍💻

This cheat sheet features the most important and commonly used Git commands for easy reference.


☑️ CREATE

For Existing Data

cd ~/Projects/MyProject
Enter fullscreen mode Exit fullscreen mode
git init 
Enter fullscreen mode Exit fullscreen mode
git add .
Enter fullscreen mode Exit fullscreen mode

From Existing repo

git clone ~existing/repo ~/new/repo
Enter fullscreen mode Exit fullscreen mode
git clone you@host.in:dir/project.git
Enter fullscreen mode Exit fullscreen mode

default protocol is ssh


☑️ UPDATE

Fetch Leatest Change From Origin

git fetch
Enter fullscreen mode Exit fullscreen mode

this does not merge them

Pull leatest chage from origin

git pull
Enter fullscreen mode Exit fullscreen mode

does a fetch followed by a merge

Apply a Patch that someone sent you

git am -3 patch.mbox
Enter fullscreen mode Exit fullscreen mode

In case of conflict, resolve the conflict and

git am --resolve
Enter fullscreen mode Exit fullscreen mode

☑️ PUBLISH

Git commit all local change

git commit -a
Enter fullscreen mode Exit fullscreen mode

Git commit with message

git commit -m 'message'
Enter fullscreen mode Exit fullscreen mode

Preapre a patch for other developers

git format-patch origin
Enter fullscreen mode Exit fullscreen mode

push change to origin

git push [origin] [branch]
Enter fullscreen mode Exit fullscreen mode

Make a version or milestone

git  tag <vesrion_name>
Enter fullscreen mode Exit fullscreen mode

Push the tag

git push origin Tagname
Enter fullscreen mode Exit fullscreen mode

Push All tag

git push origin --tags
Enter fullscreen mode Exit fullscreen mode

Delete tag

git tag -d <Tagname>
Enter fullscreen mode Exit fullscreen mode

☑️ BRANCH

Switch to the BRANCH branch

git checkout <BRANCH>
Enter fullscreen mode Exit fullscreen mode

Merge branch B1 into branch B2

git checkout <B2>
Enter fullscreen mode Exit fullscreen mode
git merge <B1>
Enter fullscreen mode Exit fullscreen mode

Create branch based on HEAD

git branch <BRANCH>
Enter fullscreen mode Exit fullscreen mode

Create branch based on another

git checkout <new> <base>
Enter fullscreen mode Exit fullscreen mode

Delete Branch

git branch -d <branchName>
Enter fullscreen mode Exit fullscreen mode

☑️ REVERT

Return to the last committed state

git checkout -f | git reset --hard
Enter fullscreen mode Exit fullscreen mode

youcannot do the hard reset

Revert the last commit

git revert HEAD
Enter fullscreen mode Exit fullscreen mode

create a new commit

Fix the last commit

git commit -a -amend
Enter fullscreen mode Exit fullscreen mode

after editing the broken files

Checkout the ID version of file

git checkout <ID> <file>
Enter fullscreen mode Exit fullscreen mode

☑️ SHOW

File changed in working directory

git status
Enter fullscreen mode Exit fullscreen mode

Change to tracked files

git diff
Enter fullscreen mode Exit fullscreen mode

Change between ID1 and ID2

git diff <ID1> <ID2>
Enter fullscreen mode Exit fullscreen mode

History of change

git log
Enter fullscreen mode Exit fullscreen mode

History of change with files changed

git whatchanged
Enter fullscreen mode Exit fullscreen mode

Who Changed what and when in a file

git blame <file>
Enter fullscreen mode Exit fullscreen mode

A commit identifyby ID

git show <ID>
Enter fullscreen mode Exit fullscreen mode

A specific file from a specific ID

git diff <ID>:<FILE>
Enter fullscreen mode Exit fullscreen mode

All local branched

git branch
Enter fullscreen mode Exit fullscreen mode

star "*" mark the current branch

Search for patterns

git grep <pattern> [path]
Enter fullscreen mode Exit fullscreen mode

< 👨‍💻 Thank you For Your Time ....‍💻‍💻 />

Top comments (0)