How do I get back to the previous commit from a remote branch?

Asked

Viewed 11,748 times

8

I have an app that has some local and remote branchs, only I had to perform a git reset --hard HEAD~1 in the branchs dev and development(as in the image below), and each of these branchs have remote (remotes/dev/master and remotes/origin/development, respectively) and now they are ahead of the local branchs. How do I align remote branchs with locations, I want all branches to commit "Removing city from the required parameters". It is possible?

Screenshot of my local repository:

inserir a descrição da imagem aqui

Note: When giving one push have a conflict:

! [rejected]        desenvolvimento -> desenvolvimento (non-fast-forward)
error: failed to push some refs to '[email protected]:augustoppimenta/recruti.git'
dica: Updates were rejected because the tip of your current branch is behind
dica: its remote counterpart. Integrate the remote changes (e.g.
dica: 'git pull ...') before pushing again.
dica: See the 'Note about fast-forwards' in 'git push --help' for details.
  • I’m going to have to do some tests here, because I didn’t quite understand your principle since I thought you were going through a situation that I had recently, rs.. in the meantime I’ll delete my reply :/

  • Beauty then. Thank you so much for your help!

  • @Math Brigadão. :)

2 answers

7


A quick way to do this is by doing a force push after you’ve checked out the commit you want

Ex.

git checkout <commit_hash>
git push origin desenvolvimento --force

This is probably not the ideal solution if you’re working with other people, since their local branch will be 2 commits ahead of the remote (which will probably ask them to send the changes back there), what they should do if this happens is to reset their branch to the same remote version:

git reset --hard origin/desenvolvimento

Remember that a forced push removes everything in the remote branch and puts what you have locally.. this can generate a lot of unwanted effects.

The ideal solution would be to revert commits that you don’t want in each of the branchs with Command:

git revert <commit_hash>

And then push them to the remote branchs

  • Hello @yurifariasg, in my case I already one git reset, then I’d have to make a git pull origin ...desenvolvimento and then make a git revert? Hence this git revert would already align the local branch with the remote branch?

  • 1

    If your branch is already aligned with the remote (pulled or git reset --hard origin/<branch>), just do git revert <commit_sha> and git will create a NEW commit by reversing the changes from that commit... imagine this as the inverse of that commit...

  • Got it! How I had already done the -force everything worked out. If you have this problem again I know how to fix it. Thank you so much for your help. Hug!

0

I went through the same problem today! the solution I found was git revert. Revert creates a new commit by reversing the changes made to the commit by passing !

Current commit

ccf554115 (HEAD) arrow function over default class react. using new api hook
3196af673 I refator files of gatsby starters for new useStaticQuery

After git revert ccf554115

28b9762e9 (HEAD) Revert "arrow function over default class react. using new api hook"
ccf554115 arrow function over default class react. using new api hook
3196af673 I refator files of gatsby starters for new useStaticQuery

After this it is of the one git push origin < sua branch >

Browser other questions tagged

You are not signed in. Login or sign up in order to post.