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 undo 1 commit without loosing changes
Git forceful deletion of a branch without merging it
Git create a branch from another branch
Git rename a local branch
-m, --move move/rename a branch and its reflog
Git rename a remote branch
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
To unstag all changes useGet git diff
after it was staged using git add
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 checkout remote branch
git link and sync all remote branch to local branch
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
git stash changes. By stashing changes you can change your current branch without having to worry about commiting changes in branch.
git stash changes including untracked files. By stashing changes you can change your current branch without having to worry about commiting changes in branch.
Clear git stash
git goto to a particular commit
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 goback to previous commit (IMPORTANT: All changes will be discarded)
git goback to (previous - 1 commit) (IMPORTANT: All changes will be discarded)
git check history of a file
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
Note: The new changes will be commited into the working branchgit cherry pick without commit
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 accept all incoming changes
Overwrite any current changes and accept all conflicts from incoming changes, you can use the theirs strategy instead:
git accept all current changes
Accept all current changes and ignore any incoming changes
prune all stale branches from your local repository
This will delete all local branches that already have been removed from the remote
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
To fix this, they'll have to either delete their existing repository and re-clone it, or do a re-base
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
~/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
~/work/.gitconfig
add directory specific configuration here