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 checkout -b NEW_BRANCH_NAME SOURCE_BRANCH_NAME

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

Remove sensitive files and their commits from Git history

These commands will re-write git history and therefore advisibale to create a backup

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

PATH_TO_FILE="/a/b/c"

# If multiple branch has the secret
git filter-branch --force --index-filter \
  "git rm --cached --ignore-unmatch $PATH_TO_FILE" \
  --prune-empty --tag-name-filter cat -- --all

# If only one branch has the secret
BRANCH_CONTAINING_FILE="main"
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch $PATH_TO_FILE" \
--prune-empty --tag-name-filter cat -- $BRANCH_CONTAINING_FILE
force-push your local changes to overwrite your remote repository, as well as all the branches you've pushed up
git push --force --verbose --dry-run
git push origin --force
In order to remove the sensitive file from your tagged releases, you'll also need to force-push against your Git tags
git push origin --force --tags
When others try pull down your latest changes after this, they'll get a message indicating that the changes can't be applied because it's not a fast-forward.

To fix this, they'll have to either delete their existing repository and re-clone it, or do a re-base

git rebase --interactive

Complete breakdown of commands here

git setup diffferent configuration for different folders

Setup ~/.gitconfig
point differnt directories to respective gitconfig

[includeIf "gitdir:~/personal/"]
    path = ~/personal/.gitconfig

[includeIf "gitdir:~/work/"]
    path = ~/work/.gitconfig


[init]
 defaultBranch = main
Setup ~/personal/.gitconfig add directory specific configuration here
[user]
        email = anand.shivam44@yahoo.com
        name = Shivam Anand
[pull]
        rebase = true

[core]
        editor = vim
        sshCommand = ssh -i ~/.ssh/shivam_personal_laptop_ubuntu
Setup ~/work/.gitconfig add directory specific configuration here
[user]
        email = shivam.anand@work-email.company
        name = S Anand
[pull]
        rebase = true 
[core]
        editor = vim
        sshCommand = ssh -i ~/.ssh/shivam-work