0

How to reset commit of the remote branch? (use git command)

The situation is this,

branch A

git add modify files
git commit -m "ISSUE FIX A"
git push origin A

Found problems from ISSUE FIX A commit, so I want to reset this commit

git reset HEAD^
git commit -m "Reset commit HEADNUM"
git push origin A

then error message

 ! [rejected]        BRANCH A -> BRANCH A (non-fast-forward)

Please let me know any solution.. thanks

Park
  • 401
  • 4
  • 10
  • 2
    Possible duplicate of [Changing git commit message after push (given that no one pulled from remote)](https://stackoverflow.com/questions/8981194/changing-git-commit-message-after-push-given-that-no-one-pulled-from-remote) – gazdagergo Aug 10 '18 at 06:30

5 Answers5

1

Just fix the issue on your local, save it and:

git add .
git commit --amend --no-edit
git push --force-with-lease

The commit and its hash will change keeping the same commit message. The --force-with-lease overwrites your remote branch with your local, unless someone else committed on it.

gazdagergo
  • 6,187
  • 1
  • 31
  • 45
0

Because you are trying to edit the history, you need to use git push --force origin A.

Although this should only be done if you are pushing to your own pull request, or somewhere that people will not be surprised if the history changes.

By the way, you can amend your commit without using git reset by using git commit --amend to amend the last commit.

Adam Millerchip
  • 20,844
  • 5
  • 51
  • 74
  • Thanks for the answer, but I can't do this because have no permission – Park Aug 10 '18 at 06:13
  • 1
    In that case you cannot reset it. You should make a new commit to fix the changes, or make a new pull request (if you are making a pull request). – Adam Millerchip Aug 10 '18 at 06:15
0
  1. Remove 'Reset commit HEADNUM' commit.
  2. Pull 'ISSUE FIX A' from the origin.
  3. Use git reset --soft HEAD~
n1th1l
  • 55
  • 1
  • 8
0

you want to add some more changes to that last commit, you can simply stage them as normal and then commit again:

$ git add some/changed/file.ext $ git commit --amend -m "commit message"

or you can Hard and Soft commands :

neither produces any new commits nor does it delete any old ones. It works by resetting your current HEAD branch to an older revision (also called "rolling back" to that older revision):

$ git reset --hard Commit ID After this command, your currently checked out branch will be at revision commitID. The commits that came after this one are effectively undone and are no longer visible in the history of this branch.

Be careful, however: calling the command with the "--hard" option will discard all local changes that you might currently have. The project is completely restored as it was in that past revision. If you call it with "--keep" instead of "--hard", all changes from rolled back revisions will be preserved as local changes in your working directory.

Nihit K
  • 25
  • 3
0

NOTE: If this is a public branch repository used by others, its your responsibility to inform about your changes. So they can perform a reset with origin.

For Local: git reset --hard z99909W z99909W is the commit id found in bitbucket.

For Repository: git push --force origin z99909W:branchA

Arun
  • 300
  • 2
  • 13