Skip to content

Instantly share code, notes, and snippets.

@sunnymui
Last active July 28, 2021 07:09
Show Gist options
  • Save sunnymui/fec3da385bc595dd745df09ece323d9c to your computer and use it in GitHub Desktop.
Save sunnymui/fec3da385bc595dd745df09ece323d9c to your computer and use it in GitHub Desktop.
Git: Rebasing a Repo
//
// Basic Rebase
//
// gets the upstream changes from the remote origin
// if getting from an upstream origin i.e. you forked a repo and then you cloned your own
// fork to local then fetch from the upstream origin instead of origin
git fetch origin
// rebases those changes from the remote origin master branch onto the local develop master branch
git rebase origin/master
// if you need to push the changes from the upstream origin to your fork's repo, assuming the url is saved as fork-origin
git push -f fork-origin develop
//
// Stashing Changes
//
// If you have uncommited changes you want to put away for a sec while you rebase:
git stash
// Add the stashed changes back to your working branch
git stash apply
// Clear the entire stash
git stash clear
//
// Rebasing Master to Update a Feature Branch
//
// get the changes from origin
git fetch
// switch to your feature branch
git checkout my-new-feat
// rebase master and replay your feature branch changes over it
git rebase origin/master
//
// Deleting a Branch
//
// delete an merged branch
git branch -d name-of-branch
// delete an unmerged branch
git branch -D name-of-branch
//
// Reset / Throw Away Your Changes
//
// reset to the your most recent commit, you can't undo this!
git reset --hard
// reset to a 1 commit earlier than your most recent
git reset --hard HEAD ~1
// reset to a specific commit in the history
git reset --hard af2653a // <-- the commit id from git log
//
// Merge In A Branch, But Overwrite Almost Everything In It With Our Current Work Branch
//
// the branch name is what you want to merge in, but use the files
// in your current working branch to overwrite everything in it
// useful if you have 2 branches that have gotten way out of sync
// and you just want to PR to overwrite a branch with one version
// in every merge case but preserve history
// * WARNING *
// in certain cases it may not fully overwrite everything in old branch with what you want from new branch
// in that case you need to use reset to really overwrite everything
git merge -s ours your-branch-name
//
// Overwrite a Branch With Another Branch Completely
//
// first checkout the branch to overwrite
git checkout gross-branch
// then overwrite it with a reset, here using the remote master branch
git reset --hard origin/master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment