How to revert last(latest) commit on my feature branch(Remote) in git

Issue

I have pushed 2 commits in my feature branch(remote) but now I have to revert back the latest commit for the branch.

e.g commit A latest
commit B first

here I want to delete commit A and want to leave changes as it was in commit B

I can see many solution in google but not sure what to do?
Some says make the changes and again push the changes in remote.
Some says to delete the last commit and rewrite the history.

I don’t want to loose the changes from the first commit. and just want to delete the latest commit from remote branch.

Solution

One easy approach is to create a new commit cancelling the changes of commit A, using the git revert operation.

This method has the advantage of not altering the commit history, which makes it straightforward for other users to pull the feature branch.

  • In your local repository, execute the following:
git revert <sha-of-commit-A>
  • Enter a commit message like "undo changes of commit-A"
  • Then push the feature branch to the remote.

The feature branch will then only contain the changes of commit-B.

Reference article with more details.

Answered By – Mehdi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published