Post

Git

🧰 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

1
git config --list

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

1
git init <Repo Name>

Clone a remote repository

1
git clone <remote URL>

πŸ“ 3. Local Changes

Add a specific file

1
git add <filename>

Add all files

1
git add *

Commit with a message

1
git commit -m "Commit Message"

πŸ” 4. Track Changes

View unstaged changes

1
git diff

View staged changes

1
git diff --staged

View changes after commit

1
git diff HEAD

View diff between two commits

1
git diff <commit1> <commit2>

View diff between branches

1
git diff <branch1> <branch2>

Check status

1
git status

Show object data

1
git show

πŸ“œ 5. Commit History

View full log

1
git log

View log in one-line format

1
git log --oneline

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)

1
git blame <filename>

🚫 6. Ignoring Files

Create .gitignore

1
touch .gitignore

List ignored files

1
git ls-files -i --exclude-standard

🌿 7. Branching

Create a new branch

1
git branch <branch-name>

Create a new branch with tags

1
git checkout -b <branch-name> tags/<tag-name>

List all branches

1
git branch --list

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

1
git stash

Stash with a message

1
git stash save "Your stash message"

List all stashes

1
git stash list

Apply last stash

1
git stash apply

View stash content

1
git stash show

Apply and remove last stash

1
git stash pop

Drop last stash

1
git stash drop

Clear all stashes

1
git stash clear

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

1
git branch --merged

List Unmerged Branches

1
git branch --no-merged

Merge a commit

1
git merge <commit-id>

Rebase a branch

1
git rebase <branch-name>

Continue rebase

1
git rebase --continue

Skip a rebase commit

1
git rebase --skip

Interactive rebase

1
git rebase -i

🌐 9. Remote Management

View remotes

1
git remote -v

Add remote

1
git remote add <name> <url>

Fetch from remote

1
git fetch <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

1
git remote rm <name>

Rename remote

1
git remote rename <old> <new>

Show remote info

1
git remote show <name>

Change remote URL

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

πŸ“€ 10. Pushing Updates

Push to remote

1
git push origin master

Force push

1
git push <remote> <branch> -f

Delete remote branch

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

πŸ“₯ 11. Pulling Updates

Pull from remote

1
git pull origin master

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

1
git fetch --all

Sync local repo

1
git fetch origin

βͺ 12. Undo Changes

Revert a commit

1
git revert <commit-id>

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

1
git rm <filename>

Remove file from Git but keep locally

1
git rm --cached <filename>

🏷️ 14. Git Tags

Create a lightweight tag

1
git tag <tagname>

Create an annotated tag

1
git tag -a <tagname> -m "Tag message"

List all tags

1
git tag

Show tag details (annotated)

1
git show <tagname>

Tag a specific commit

1
git tag <tagname> <commit-hash>

Delete a local tag

1
git tag -d <tagname>

Delete a remote tag

1
git push origin --delete <tagname>

Push a single tag to remote

1
git push origin <tagname>

Push all local tags to remote

1
git push --tags
This post is licensed under CC BY 4.0 by the author.