Revert a commit thatâs already in remote
git revert <hash>
use -n
if you want to revert more than one commit. This wonât push a commit just yet but make changes to your index/staging first, so you can revert multiple hashes into one commit.
Reset local repo to match Remote
git fetch origin
git reset --hard origin/<branch>
If you want to save your current branchâs state before doing this (just in case), you can do:
git commit -a -m "Saving my work, just in case"
git branch my-saved-work
Now your work is saved on the branch âmy-saved-workâ in case you decide you want it back (or want to look at it later or diff it against your updated branch).
Reset commits to staging without affecting any current changes in the workspace
git reset --soft <commit>
If your commit looks like this:
commit asd235 commit fs34ws
and you wish to reset asd235, set <commit>
to fs34ws. Or use HEAD~1
as hash to reset 1 commit before the head
Revert a merged branch
git revert -m 1 <merge-commit>
Run that command on the branch that commit was merged into.
With â-m 1â we tell git to revert to the first parent of the mergecommit on the master branch. -m 2 would specify to revert to the first parent on the develop branch where the merge came from initially.
(taken from https://www.christianengvall.se/undo-pushed-merge-git/)
Revert a file in the previous commit
If youâve accidentally committed a file that was wrong and you donât want to revert the entire commit:
git reset <commit_with_change>~1 -- <filename>
This will reset the file to a staged phase of the commit with change.
Hint: git log --name-only
if you need to see the file names in the commit history.
Make your changes and commit it. Or if you just need to revert it:
git reset HEAD <filename>
git checkout -- <filename>
Then commit.
If you need to revert more than 1 file, then I recommend the steps below:
git reset --soft <commit_with_change>~1
git add -i
What now> 3 # Choose revert option
Then select the files you want to revert and to the same next steps as a single file revert.
When done push your changes, I usually use a rebase workflow so Iâd git push --force-with-lease
.
Unstage files that canât be reset
Usually due to the EOL control characters conflicting with remote or something. Google has much to say about this.
Try changing your git config git config core.filemode false
Or reload your whole local repo with this:
git rm --cached -r .
git reset --hard <remote>/<branch> # eg: git reset --hard origin/master