8

I have a pushed commit on branch (it's not main repo branch). It's a last commit I did. I want to delete it from repo. After pushing I've already done some local changes (at same branch).
I want to keep all my changes locally.
I'd go git revert <commit>, than git add . -> git commit -> git push origin my-branch?

3 Answers3

5

Please correct me if I'm wrong, but this is my understanding of your situation:

  1. You made some changes, and committed them locally with git commit.
  2. You pushed your changes to a remote repository (Github?) with git push.
  3. Now, you've realized there is a problem with the changes you just pushed, and you'd like to correct it. You made these changes locally, but have not committed them yet.

If that's correct, from this point, you have two options. You can either:

  • Simply make a new commit, and push it up. This is the simplest option, but it does leave your mistake in the git history. To do this, you'd simply git add, git commit and git push, as you did before.
  • OR, you can amend the commit you made, and push the amended commit (essentially deleting the first (incorrect) commit you pushed). This option rewrites history, which is fine as long as no one else has done work based on this branch yet. And this option will remove your bad commit completely from the history.

If you want to do option 2, you need to:

  1. git add to stage your files, just like you normally would when creating a new commit.
  2. git commit --amend Amend the previous commit with your current local changes. After this, it will have a completely new commit hash (as far as git is concerned, it bears no relation to the previous bad commit).
  3. git push -f origin Force push your changes. The force is required because you're changing branch history.
mkasberg
  • 16,022
  • 3
  • 42
  • 46
2

The easiest way is to do a reset HEAD.

Approach 1:

  1. Stash your local changes.

git stash

  1. reset your branch to the commit number last_commit-1 as follows.

git reset HEAD~1

or

git reset HEAD^

  1. Now pop changes from stash.

git stash pop

  1. Resolve if any conflict occurs during stash pop.

  2. Now commit all your changes together and you are done.

The advantage with this approach is that you will save 1 commit hash for yourself.

Approach 2:

This approach is that

  1. you just prepare another commit with your other local changes that you have.

  2. Now squash the changes as follows.

git rebase -i HEAD~2

replace pick with squash for the recent commit that you made in step 1.

  1. Now save the changes. Use Ctrl+O for this. Next you will be prompted for the commit message. You could modify the commit message as per your requirement.

We are done!

My personal preference would always be to use squashing. But with one precaution i.e. use it usually when fewer people are working on a branch else it might turn into hell if somebody novice did a git pull rather than a git reset --hard origin/branch_name on local.

Hope it helps!

Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86
1

Here are the steps you can do that.

  1. Take back up of your local branch. Let say if your branch is master and your back brach name is like master_backup. You can use below git command.

    git branch master-backup if you are on the same branch. or you can also use

    git branch master-backup master

  2. git revert <your commit id>

Resolve if there is any conflict.

  1. git push origin master (Push your changes after revert)

  2. git reset --hard master-backup (Get your changes back to local master)

Amit Bera
  • 7,075
  • 1
  • 19
  • 42