Skip to content

Git Cheat Sheet

Git Cheat Sheet

To keep in sync with remote branch (You may loose your local changes in the branch)

git fetch --all
branch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
echo $branch
git reset --hard origin/$branch

Squash previous N commits into 1

git reset --soft HEAD~3 &&
git commit

Git undo 1 commit without loosing changes

git reset HEAD~1 --soft   

Git forceful deletion of a branch without merging it

git branch -D BRANCH_NAME

Git create a branch from another branch


Git rename a local branch

git checkout OLD_BRANCH_NAME
git branch -m NEW_BRANCH_NAME
-m, --move move/rename a branch and its reflog

Git rename a remote branch

git push origin -u NEW_BRANCH_NAME
git push origin --delete OLD_BRANCH_NAME

Modify previous commit message

#remote: Use this command to change commit message (one commit at a time):
git rebase --interactive 985f75c0e537c357414b2a60f38a07dfe941f91b^
# remote: In the default editor, modify 'pick' to 'edit' in the line whose commit you want to modify
git commit --amend
# remote: modify the commit message
run: git rebase --continue

Mistakenly did a git add. To remove a file from staging area use

git reset filename
To unstag all changes use
git reset

Get git diff after it was staged using git add

git diff --staged

How can I purge all the history and push it

git checkout --orphan <name_you_choose_for_orphan_branch>
git commit
git push <remote-name> <branch-name>

Remove a file from staging if it was added using git add / Undo git add

git restore --staged <file>

git checkout remote branch

git checkout -b BRANCH_NAME origin/BRANCH_NAME 

git branch -r | grep -v '\->' | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
git pull --all
also check this link

git stash changes. By stashing changes you can change your current branch without having to worry about commiting changes in branch.

git stash
git checkout BRANCH
git stash pop

git stash changes including untracked files. By stashing changes you can change your current branch without having to worry about commiting changes in branch.

git stash --include-untracked
git checkout BRANCH
git stash pop

Clear git stash

git stash clear

git goto to a particular commit

git reflog # check commits; you can also use git log
git reset --hard commit_SHA

git undo previous commit. This will undo the commit and remove changes from staging, but your previous commit file changes will not be lost

git reset HEAD~1

git goback to previous commit (IMPORTANT: All changes will be discarded)

git reset --hard HEAD

git goback to (previous - 1 commit) (IMPORTANT: All changes will be discarded)

git reset --hard HEAD~1

git check history of a file

git log -p -- filepath

git bring changes from a specific commit from a specific repository into working repository

use git log to get commit hash from the branch you branch you want to bring changes, then

git checkout WORKING_BRANCH
git cherry-pick <commit-hash>
Note: The new changes will be commited into the working branch

git cherry pick without commit

git cherry-pick -n <hash>

git cherry pick fast

# Get hash from the branch
HASH=$(git rev-parse HEAD)

# switch branch then
git cherry-pick -n $HASH

# verify the changes then make the commit
git restore --staged *
git restore *
git cherry-pick  $HASH

git abort merge if stuck in merge conflict

git merge --abort

git accept all incoming changes

Overwrite any current changes and accept all conflicts from incoming changes, you can use the theirs strategy instead:

git merge [branch] --strategy-option theirs

git accept all current changes

Accept all current changes and ignore any incoming changes

git merge [branch] --strategy-option ours

prune all stale branches from your local repository

This will delete all local branches that already have been removed from the remote

git remote prune origin --dry-run
git remote prune origin