Jonyeezs' Git Reference

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