Git Commands Cheat Sheet

This guide contains common Git commands and practical examples for day-to-day development.


1. Check Git version

1
git --version

2. Configure Git identity

Set your global username:

1
git config --global user.name "Your Name"

Set your global email:

1
git config --global user.email "you@example.com"

Check configuration:

1
git config --list

Check a specific value:

1
2
git config user.name
git config user.email

3. Initialize a repository

1
git init

Initialize a repository in a specific directory:

1
git init my-project

4. Clone a repository

HTTPS:

1
git clone https://github.com/user/repository.git

SSH:

1
git clone git@github.com:user/repository.git

Clone into a custom directory:

1
git clone git@github.com:user/repository.git my-folder

5. Check repository status

1
git status

Short output:

1
git status -s

6. View changes

Show unstaged changes:

1
git diff

Show staged changes:

1
git diff --staged

Show changes in a specific file:

1
git diff path/to/file

7. Stage files

Stage one file:

1
git add file.txt

Stage multiple files:

1
git add file1.txt file2.txt

Stage all changes:

1
git add .

Stage all tracked and untracked changes:

1
git add -A

8. Unstage files

Unstage one file:

1
git restore --staged file.txt

Unstage everything:

1
git restore --staged .

Older syntax:

1
git reset HEAD file.txt

9. Commit changes

1
git commit -m "Add feature"

Commit tracked file changes without running git add manually:

1
git commit -am "Fix bug"

-a does not include new untracked files.


10. Amend the last commit

Change the last commit message:

1
git commit --amend -m "New commit message"

Add forgotten staged changes to the last commit:

1
2
git add forgotten-file.txt
git commit --amend --no-edit

Avoid amending commits that have already been shared unless you understand the consequences of rewriting history.


11. View commit history

1
git log

Compact format:

1
git log --oneline

Graph view:

1
git log --oneline --graph --decorate --all

Show a limited number of commits:

1
git log -10 --oneline

Show commits for a file:

1
git log -- path/to/file

12. Show a commit

1
git show COMMIT_HASH

Show the latest commit:

1
git show HEAD

13. List branches

Local branches:

1
git branch

Remote branches:

1
git branch -r

All branches:

1
git branch -a

14. Create a branch

1
git branch feature/my-feature

Create and switch:

1
git switch -c feature/my-feature

Older syntax:

1
git checkout -b feature/my-feature

15. Switch branches

1
git switch main

Older syntax:

1
git checkout main

Switch back to the previous branch:

1
git switch -

16. Rename a branch

Rename the current branch:

1
git branch -m new-name

Rename another local branch:

1
git branch -m old-name new-name

17. Delete a branch

Delete a merged local branch:

1
git branch -d feature/my-feature

Force-delete a local branch:

1
git branch -D feature/my-feature

Delete a remote branch:

1
git push origin --delete feature/my-feature

18. Merge a branch

Switch to the target branch:

1
git switch main

Merge another branch:

1
git merge feature/my-feature

19. Abort a merge

If a merge has conflicts and you want to cancel it:

1
git merge --abort

20. Rebase a branch

Rebase the current branch onto main:

1
git rebase main

Typical workflow:

1
2
3
git switch feature/my-feature
git fetch origin
git rebase origin/main

21. Continue or abort a rebase

After resolving conflicts:

1
2
git add .
git rebase --continue

Abort:

1
git rebase --abort

Skip the current commit:

1
git rebase --skip

22. Interactive rebase

Edit recent commits:

1
git rebase -i HEAD~3

Common actions:

1
2
3
4
5
6
pick
reword
edit
squash
fixup
drop

23. Pull changes

1
git pull

Pull from a specific remote branch:

1
git pull origin main

Pull using rebase:

1
git pull --rebase

24. Fetch changes

Fetch without modifying your working branch:

1
git fetch

Fetch all remotes:

1
git fetch --all

Remove references to deleted remote branches:

1
git fetch --prune

25. Push changes

Push the current branch:

1
git push

Push and set upstream:

1
git push -u origin feature/my-feature

After that:

1
git push

is enough.


26. Force push safely

After rewriting branch history:

1
git push --force-with-lease

Prefer this over:

1
git push --force

--force-with-lease helps prevent accidentally overwriting remote changes made by someone else.


27. List remotes

1
git remote -v

28. Add a remote

1
git remote add origin git@github.com:user/repository.git

29. Change remote URL

1
git remote set-url origin git@github.com:user/repository.git

Verify:

1
git remote -v

30. Remove a remote

1
git remote remove origin

31. Show remote information

1
git remote show origin

32. Stash changes

Temporarily save working changes:

1
git stash

With a message:

1
git stash push -m "Work in progress"

Include untracked files:

1
git stash -u

33. List stashes

1
git stash list

34. Restore stashed changes

Apply the latest stash and keep it:

1
git stash apply

Apply and remove it from the stash list:

1
git stash pop

Apply a specific stash:

1
git stash apply stash@{1}

35. Delete stashes

Delete one stash:

1
git stash drop stash@{0}

Delete all stashes:

1
git stash clear

36. Discard local file changes

Restore one file:

1
git restore file.txt

Restore all tracked files:

1
git restore .

This discards unstaged changes.


37. Restore a file from another branch

1
git restore --source main path/to/file

From a commit:

1
git restore --source COMMIT_HASH path/to/file

38. Reset commits

Soft reset

Move HEAD but keep changes staged:

1
git reset --soft HEAD~1

Mixed reset

Move HEAD and keep changes unstaged:

1
git reset HEAD~1

or:

1
git reset --mixed HEAD~1

Hard reset

Move HEAD and discard changes:

1
git reset --hard HEAD~1

Be very careful with --hard.


39. Reset local branch to remote

Fetch first:

1
git fetch origin

Then:

1
git reset --hard origin/main

This makes the current branch match the remote branch exactly.


40. Revert a commit

Create a new commit that reverses another commit:

1
git revert COMMIT_HASH

Revert the latest commit:

1
git revert HEAD

This is safer than reset for commits already pushed to a shared branch.


41. Cherry-pick a commit

Apply a specific commit to the current branch:

1
git cherry-pick COMMIT_HASH

Abort after conflicts:

1
git cherry-pick --abort

Continue after resolving conflicts:

1
2
git add .
git cherry-pick --continue

42. Tag a commit

Create a lightweight tag:

1
git tag v1.0.0

Create an annotated tag:

1
git tag -a v1.0.0 -m "Release v1.0.0"

Tag a specific commit:

1
git tag -a v1.0.0 COMMIT_HASH -m "Release v1.0.0"

43. Push tags

Push one tag:

1
git push origin v1.0.0

Push all tags:

1
git push origin --tags

44. Delete tags

Delete locally:

1
git tag -d v1.0.0

Delete remotely:

1
git push origin --delete v1.0.0

45. Ignore files

Create or edit:

1
.gitignore

Example:

1
2
3
4
5
6
7
node_modules/
bin/
obj/
.env
*.log
.vscode/
.idea/

If a file is already tracked, adding it to .gitignore is not enough.

Stop tracking it:

1
git rm --cached file.txt

For a directory:

1
git rm -r --cached directory/

46. Clean untracked files

Preview what would be deleted:

1
git clean -n

Delete untracked files:

1
git clean -f

Delete untracked directories too:

1
git clean -fd

Always run git clean -n first.


47. Find who changed a line

1
git blame file.txt

Specific line range:

1
git blame -L 10,20 file.txt

48. Search commit messages

1
git log --grep="keyword"

49. Search code history

Find commits that added or removed a specific string:

1
git log -S "SomeText"

Show patches too:

1
git log -S "SomeText" -p

50. Compare branches

1
git diff main..feature/my-feature

Compare commits:

1
git diff COMMIT_A COMMIT_B

51. Check which branches contain a commit

1
git branch --contains COMMIT_HASH

Remote branches too:

1
git branch -a --contains COMMIT_HASH

52. Reflog — recover lost commits

Git reflog records movements of HEAD:

1
git reflog

Example:

1
2
abc1234 HEAD@{0}: reset: moving to HEAD~1
def5678 HEAD@{1}: commit: Important work

Recover a lost commit by creating a branch:

1
git branch recovered-work def5678

Or reset back to it:

1
git reset --hard def5678

git reflog is extremely useful after accidental reset, rebase, or branch deletion.


53. Remove the last local commit but keep changes

Keep changes staged:

1
git reset --soft HEAD~1

Keep changes unstaged:

1
git reset HEAD~1

54. Undo the last pushed commit safely

If the commit is already shared:

1
2
git revert HEAD
git push

Prefer this over rewriting shared history.


55. Update a feature branch with latest main

Using rebase:

1
2
3
git switch feature/my-feature
git fetch origin
git rebase origin/main

Then, if the branch was already pushed:

1
git push --force-with-lease

Alternative using merge:

1
2
3
git switch feature/my-feature
git fetch origin
git merge origin/main

56. Squash recent commits

For the last 3 commits:

1
git rebase -i HEAD~3

Example editor:

1
2
3
pick   abc123 First change
squash def456 Second change
squash ghi789 Third change

Save and edit the final commit message.


57. Rename the default branch

Rename locally:

1
git branch -m master main

Push:

1
git push -u origin main

Delete the old remote branch after changing the repository’s default branch:

1
git push origin --delete master

58. Show tracked files

1
git ls-files

59. Show current branch

1
git branch --show-current

60. Show repository root

1
git rev-parse --show-toplevel

61. Show current commit hash

Full hash:

1
git rev-parse HEAD

Short hash:

1
git rev-parse --short HEAD

62. Create an empty commit

Useful for testing CI/CD pipelines:

1
git commit --allow-empty -m "Trigger pipeline"

Push it:

1
git push

63. Common workflow

Create a feature branch:

1
2
3
git switch main
git pull
git switch -c feature/my-feature

Work and commit:

1
2
3
git status
git add .
git commit -m "Implement feature"

Push:

1
git push -u origin feature/my-feature

Update with latest main:

1
2
git fetch origin
git rebase origin/main

Push rewritten history if needed:

1
git push --force-with-lease

64. Common recovery workflow

Check what happened:

1
2
3
git status
git log --oneline --graph --decorate -20
git reflog

If you accidentally changed a file:

1
git restore file.txt

If you accidentally staged a file:

1
git restore --staged file.txt

If you need to undo a local commit but keep the work:

1
git reset --soft HEAD~1

If a shared commit must be undone:

1
git revert COMMIT_HASH

If a commit seems lost:

1
git reflog

65. Useful aliases

Create a compact log alias:

1
git config --global alias.lg "log --oneline --graph --decorate --all"

Then use:

1
git lg

Status shortcut:

1
git config --global alias.st status

Branch shortcut:

1
git config --global alias.br branch

Commit shortcut:

1
git config --global alias.cm commit

Useful daily commands

1
2
3
4
5
6
7
8
9
10
11
12
13
14
git status
git diff
git add .
git commit -m "Message"
git log --oneline --graph --decorate --all
git switch branch-name
git switch -c new-branch
git fetch origin
git pull --rebase
git push
git stash
git stash pop
git rebase origin/main
git reflog

Safety recommendations

  • Use git status before destructive operations.
  • Prefer git revert for commits already shared with others.
  • Prefer git push --force-with-lease over git push --force.
  • Run git clean -n before git clean -f.
  • Be careful with git reset --hard.
  • Use git reflog when you think a commit has been lost.
  • Never commit passwords, private keys, API tokens, or production .env files.