- Stage files with confirmation (commit only part of a file)
- git add --patch (git add -p)
- Delete local branch if it has already been fully merged in its upstream branch
- git branch -d <branch_name>
- Delete local branch, irrespective of its merged status
- git branch -D <branch_name>
- Bringing files from another branch or commit to current branch
- git checkout <commit_hash> <relative_path_to_file_or_dir>
- git checkout <remote_name>/<branch_name> <file_or_dir>
- Delete remote branch
- git push <remote_name> --delete <branch_name>
- git push origin --delete <branch_name>
- git push <remote_name> --delete <branch_name>
- Reset local repository branch to be just like remote repository HEAD
- git fetch origin; git reset --hard origin/<branch>
- Undo staging (git add <filename>)
- git reset <filename>
- Undo all staged files
- git reset
- Undo last local commit
- While preserving local changes: git reset --soft HEAD~1
- Without preserving local changes: git reset --hard HEAD~1
- Combining/squashing multiple commits into one (change
pick
tosquash
for the commits you want to squash)- git rebase -i HEAD~<numbers of commits to look back>
- git rebase -i
- Rebase changes in branch 2 on top of main branch (for smooth PR)
- git rebase main
- Stop tracking a file
- git rm --cached <file>
- Stop tracking a folder
- git rm -r --cached <folder>
- Look at file in a different branch while staying in current branch
- git show <branch_name>:</path/to/file>
- git show <branch_name>:</path/to/file> | vim -
- Show what was changed in a commit
- git show <COMMIT_HASH>
- Undo changes and return to previous branch. Useful if you are experimenting in detached state
- git switch -
- Keep changes you made in detached state and create a branch for it
- git switch -c <new branch name>
- Get current commit hash
- git rev-parse HEAD