Skip to content

Instantly share code, notes, and snippets.

@prasanthkothuri
Last active May 31, 2023 06:21
Show Gist options
  • Save prasanthkothuri/3a10377cf18120bb4bd045f733633ddd to your computer and use it in GitHub Desktop.
Save prasanthkothuri/3a10377cf18120bb4bd045f733633ddd to your computer and use it in GitHub Desktop.
git magic commands

squash the last X commits into a single commit

combine
git reset --soft HEAD~X  
git commit -m "combined commit message"
push
git push origin +name-of-branch

git cherry pick multiple commits

check out the branch you want to apply the commits
git checkout prod
cherry pick single (git cherry-pick commit-hash) or multiple
git cherry-pick A^..B  
git push origin +name-of-branch  

git rebase (rebase B from A)

git checkout A  
git pull A  
git checkout B  
git pull  
git rebase A  
resolving merge conflict - https://help.github.com/articles/resolving-a-merge-conflict-using-the-command-line/

git stash (saving half done work)

git stash  
git pull
git stash list  
git stash apply (or git stash apply stash@{3}  

discard local changes

# Revert changes to modified files
git reset --hard
# Remove all untracked files and directories
git clean -fd
# Revert changes to a single file
git checkout -- <file_path>

delete branches

# local branch
git branch -d test
# local branch with changes
git branch -D test
# remote branch
git push origin --delete test

git cherry pick from fork

git checkout <branch>
git remote add <fork-alias> <fork-URL>
git fetch <fork-alias>
git cherry-pick <commit-hash>
git push origin <branch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment