This guide contains common Git commands and practical examples for day-to-day development.
1. Check Git version
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:
Check a specific value:
1 2
| git config user.name git config user.email
|
3. Initialize a repository
Initialize a repository in a specific directory:
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
Short output:
6. View changes
Show unstaged changes:
Show staged changes:
Show changes in a specific file:
7. Stage files
Stage one file:
Stage multiple files:
1
| git add file1.txt file2.txt
|
Stage all changes:
Stage all tracked and untracked changes:
8. Unstage files
Unstage one file:
1
| git restore --staged file.txt
|
Unstage everything:
Older syntax:
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
Compact format:
Graph view:
1
| git log --oneline --graph --decorate --all
|
Show a limited number of commits:
Show commits for a file:
12. Show a commit
Show the latest commit:
13. List branches
Local branches:
Remote branches:
All branches:
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
Older syntax:
Switch back to the previous branch:
16. Rename a branch
Rename the current branch:
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:
Merge another branch:
1
| git merge feature/my-feature
|
19. Abort a merge
If a merge has conflicts and you want to cancel it:
20. Rebase a branch
Rebase the current branch onto 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:
Skip the current commit:
22. Interactive rebase
Edit recent commits:
Common actions:
1 2 3 4 5 6
| pick reword edit squash fixup drop
|
23. Pull changes
Pull from a specific remote branch:
Pull using rebase:
24. Fetch changes
Fetch without modifying your working branch:
Fetch all remotes:
Remove references to deleted remote branches:
25. Push changes
Push the current branch:
Push and set upstream:
1
| git push -u origin feature/my-feature
|
After that:
is enough.
26. Force push safely
After rewriting branch history:
1
| git push --force-with-lease
|
Prefer this over:
--force-with-lease helps prevent accidentally overwriting remote changes made by someone else.
27. List remotes
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:
30. Remove a remote
1
| git remote remove origin
|
32. Stash changes
Temporarily save working changes:
With a message:
1
| git stash push -m "Work in progress"
|
Include untracked files:
33. List stashes
34. Restore stashed changes
Apply the latest stash and keep it:
Apply and remove it from the stash list:
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:
36. Discard local file changes
Restore one file:
Restore all tracked files:
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:
Mixed reset
Move HEAD and keep changes unstaged:
or:
1
| git reset --mixed HEAD~1
|
Hard reset
Move HEAD and discard changes:
Be very careful with --hard.
39. Reset local branch to remote
Fetch first:
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:
Revert the latest commit:
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:
Continue after resolving conflicts:
1 2
| git add . git cherry-pick --continue
|
42. Tag a commit
Create a lightweight tag:
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"
|
Push one tag:
Push all tags:
Delete locally:
Delete remotely:
1
| git push origin --delete v1.0.0
|
45. Ignore files
Create or edit:
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:
Delete untracked files:
Delete untracked directories too:
Always run git clean -n first.
47. Find who changed a line
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:
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:
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:
Keep changes unstaged:
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:
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:
Delete the old remote branch after changing the repository’s default branch:
1
| git push origin --delete master
|
58. Show tracked 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:
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:
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:
If you accidentally staged a file:
1
| git restore --staged file.txt
|
If you need to undo a local commit but keep the work:
If a shared commit must be undone:
If a commit seems lost:
65. Useful aliases
Create a compact log alias:
1
| git config --global alias.lg "log --oneline --graph --decorate --all"
|
Then use:
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.