Posts Git - undo pushed commits local and remote
Post
Cancel

Git - undo pushed commits local and remote

Oh Shit, Git!?! - https://ohshitgit.com/ (mirror)

1
2
3
4
5
6
7
git reflog
# you will see a list of every thing you've
# done in git, across all branches!
# each one has an index HEAD@{index}
# find the one before you broke everything
git reset HEAD@{index}
# magic time machine

Tower - CMD+Z for Git is Here


src - https://ncona.com/2011/07/how-to-delete-a-commit-in-git-local-and-remote/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//src - https://ncona.com/2011/07/how-to-delete-a-commit-in-git-local-and-remote/

//set default editor
git config --global core.editor "C:/x/VSCodium/VSCodium.exe"

//list the 10 last commits
git log -10 --pretty=oneline --abbrev-commit

//ask to rebase the two last
//here the editor will appear listing the two last commits and the available options,
//you just need to delete commit line with the editor NOTHING ELSE.
//
//WARNING most recent commit is the one at the #bottom#
//simple delete the line, save the file, close the editor
git rebase -i HEAD~2
//commit just deleted locally

//apply the DELETION to remote server, the + sign before the name of the branch you are pushing,
//this tells git to force the push
git push origin +mast3r

^tested&working^

Vibs said at replies : There is now better way to do same

1
2
3
1. git reset --hard CommitIDToBeReverted
2. git clean -f -d
3. git push -u origin +NameOfBranch

Anthony said at replies, to use reset HEAD :

1
2
3
git reset --hard HEAD~2
versus
git rebase -i HEAD~2

other src - https://stackoverflow.com/a/46948061 (mirror)

You can REVERT (or you can also call it DELETE) the Git Commit BOTH Locally and Remotely if you follow the steps as given below via git command line.

Run the following command to see the commit id that you want to revert

1
    git log --oneline --decorate --graph

You will get like a following screenshot enter image description here

If you also check remote (via Web Interface) then you can see that this would be same as shown below

enter image description here

As per screenshot currently you are on commit id e110322 however you want to revert back to 030bbf6 BOTH LOCALLY and REMOTELY.

Perform the following steps to DELETE/REVERT Commits Locally+Remotely


First Locally Reverting to commit id 030bbf6

1
    git reset --hard 030bbf6

followed by

1
    git clean -f -d

These two commands clean force reset to commit stage 030bbf6 as shown below in snapshot

enter image description here

now if you run git status then you’ll see that you are TWO Commits BEHIND from the remote branch as shown below enter image description here

Run following to update your indexes (if there are any updates). It is recommended that you ask all developers not to accept any pull requests on main remote branch.

1
    git fetch --all

Once you are done with it then you are required to Push this commit forcefully by using + symbol in-front of branch as shown below. I have used here as master branch, you can replace it with any

enter image description here

1
    git push -u origin +master

now if you see the web interface of remote then commit there should be reverted as well.


Merge to Master

0- Commit & push to current current branch

1- Checkout & pull master for latest

2- Checkout current branch

3- rclick master > merge master to current branch

4- Commit & push current branch


CherryPick

-checkout the remote u want -create new branch -cherry pick there, by going to commits list and rclick > Cherry-Pick Commit

when you doing cherry pick, already commit the changes to new branch, the only thing you have to do is to PUSH the branch!


Fork

a fast and friendly git client for Mac and Windows

https://fork.dev/

Magit

is a complete text-based user interface to Git

https://magit.vc/ for VSCode - https://marketplace.visualstudio.com/items?itemName=kahole.magit

origin - https://www.pipiscrew.com/?p=18385 git-undo-pushed-commits-local-and-remote

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags