π§° Git Cheat Sheet
π§ 1. Git Configuration
Set the name
1
| git config --global user.name "User name"
|
Set the email
1
| git config --global user.email "himanshudubey481@gmail.com"
|
Set the default editor
1
| git config --global core.editor Vim
|
Check the settings
Set up Git aliases
1
| git config --global alias.co checkout
|
1
| git config --global alias.br branch
|
1
| git config --global alias.ci commit
|
1
| git config --global alias.st status
|
π 2. Starting a Project
Initialize a local repository
Clone a remote repository
π 3. Local Changes
Add a specific file
Add all files
Commit with a message
1
| git commit -m "Commit Message"
|
π 4. Track Changes
View unstaged changes
View staged changes
View changes after commit
View diff between two commits
1
| git diff <commit1> <commit2>
|
View diff between branches
1
| git diff <branch1> <branch2>
|
Check status
Show object data
π 5. Commit History
View full log
View files modified in commits
1
2
3
| git log --stat
( or )
git log --stat --oneline -p
|
Show patch (diff) of commits
1
2
3
| git log -p
( or )
git log --oneline -p
|
Blame (line-wise history)
π« 6. Ignoring Files
Create .gitignore
List ignored files
1
| git ls-files -i --exclude-standard
|
πΏ 7. Branching
Create a new branch
1
| git branch <branch-name>
|
1
| git checkout -b <branch-name> tags/<tag-name>
|
List all branches
Delete a branch
1
| git branch -d <branch-name>
|
Delete a remote branch
1
| git push origin --delete <branch-name>
|
Rename a branch
1
| git branch -m <old-name> <new-name>
|
Switch to a branch
1
| git checkout <branch-name>
|
Create and switch to new branch
1
| git checkout -b <branch-name>
|
Checkout a remote branch
1
| git checkout <remote-branch>
|
π¦ Git Stash
Stash current changes
Stash with a message
1
| git stash save "Your stash message"
|
List all stashes
Apply last stash
View stash content
Apply and remove last stash
Drop last stash
Clear all stashes
Create a branch from stash
1
| git stash branch <branch-name>
|
π Cherry-pick
Cherry-pick is used to apply a specific commit (or commits) from one branch onto your current branch, without merging the whole branch.
Apply specific commit
1
| git cherry-pick <commit-id>
|
π 8. Merging & Rebasing
Switch to the target branch before merging another branch into it.
Merge branches
1
2
| git checkout <destination-branch>
git merge <source-branch-name>
|
List Merged Branches
List Unmerged Branches
Merge a commit
Rebase a branch
1
| git rebase <branch-name>
|
Continue rebase
Skip a rebase commit
Interactive rebase
π 9. Remote Management
View remotes
Add remote
1
| git remote add <name> <url>
|
Fetch from remote
View Changes After Fetch
Check whatβs different between your local branch and the fetched data
1
| git diff $(cat .git/GETCH_HEAD)
|
Checkout a File from Remote
Restore a specific file from the remote master branch
1
| git checkout origin/master -- <file-name>
|
Remove remote
Rename remote
1
| git remote rename <old> <new>
|
Show remote info
Change remote URL
1
| git remote set-url <name> <new-url>
|
π€ 10. Pushing Updates
Push to remote
Force push
1
| git push <remote> <branch> -f
|
Delete remote branch
1
| git push origin --delete <branch-name>
|
π₯ 11. Pulling Updates
Pull from remote
Pull specific remote branch
1
| git pull <remote-branch-URL>
|
Fetch remote repo
1
| git fetch <repository-url>
|
Fetch specific branch
1
| git fetch <branch-url> <branch-name>
|
Fetch all branches
Sync local repo
βͺ 12. Undo Changes
Revert a commit
Hard reset
Change the head to the $commit_id and remove the changes from working directory, staging and local
1
| git reset --hard <commit-id>
|
Soft reset
Revert commits to stage
1
| git reset --soft <commit-id>
|
Mixed reset
Revert commits to working directory (default option when we use revert)
1
| git reset --mixed <commit-id>
|
ποΈ 13. Removing Files
Remove file from Git and disk
Remove file from Git but keep locally
1
| git rm --cached <filename>
|
Create a lightweight tag
Create an annotated tag
1
| git tag -a <tagname> -m "Tag message"
|
Show tag details (annotated)
Tag a specific commit
1
| git tag <tagname> <commit-hash>
|
Delete a local tag
Delete a remote tag
1
| git push origin --delete <tagname>
|
Push a single tag to remote
1
| git push origin <tagname>
|