Skip to content

Instantly share code, notes, and snippets.

@hugoduraes
Last active May 10, 2024 15:31
Show Gist options
  • Save hugoduraes/ed79ac69866682867d74ecc62ba4cdbb to your computer and use it in GitHub Desktop.
Save hugoduraes/ed79ac69866682867d74ecc62ba4cdbb to your computer and use it in GitHub Desktop.

Reset

You can reset the commit for a local branches using git reset

To change the commit of a local branch:

git fetch
git reset origin/master --hard

Be careful though, as the docs put it:

Resets the index and working tree. Any changes to tracked files in the working tree since are discarded. If you want to actually keep whatever changes you've got locally - do a --soft reset instead. which will update the commit history for the branch, but not change any files in the working directory (and you can then commit them).

Rebase

You can replay your local commits ontop of any other commit/branch using git rebase

git fetch
git rebase -i origin/master

This will invoke rebase in interactive mode where you can choose how to apply each individual commit that isn't in the history you are rebasing on top of.

If the commits you removed (with git push -f) have already been pulled into the local history, they will be listed as commits that will be reapplied - they would need to be deleted as part of the rebase or they will simply be re-included into the history for the branch - and reappear in the remote history on next push.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment