1

I have gone through great difficulties with my first commit to the main project. Mainly because I did all the changes onto a downloaded code and not the cloned from master code. So eventually what worked for me was creating a local branch from the masters remote branch and then copy paste the changed code.

But that resulted in a rather messed up view once I try to compare the two. Since it shows everything as deleted and added.

So my question is: Once I merge my branch with the master at Github and for some reason it doesnt work out well, is there a way to go back to master prior my merge (without big difficulties)

Thank for all answers. Got it figured out thanks to the suggestions and first batch went live :)

4
  • Have you published (pushed) master since then?
    – marekful
    Commented Jan 17, 2018 at 4:06
  • I pushed to a remote branch git push origin petroskali which is currently 2 commits ahead of master (both mine)
    – Petroskali
    Commented Jan 17, 2018 at 4:20
  • @Petroskali Yes you can undo a merge in Git. But it's best to avoid it if possible. A mark will be left most likely. Commented Jan 17, 2018 at 4:23
  • Possible duplicate of Undo a particular commit in Git that's been pushed to remote repos
    – phd
    Commented Jan 17, 2018 at 10:18

2 Answers 2

3

Given that the merge commit is already live in master, the safest bet here would be to revert that merge commit:

git checkout master
git revert -m 1 <commit hash of merge commit>

To find the commit hash for the merge commit in master, just type git log from the bash and find the commit.

This approach makes a new commit on top of master which functionally undoes the merge commit. You may push your master branch as usual. If going through a pull request to master, there also should be no hang ups with this approach.

Note: The -m 1 option used above in git revert tells Git to use the first parent, i.e. master as the track to follow. We could also have used -m 2, which would follow the feature branch which is the source of the merge.

6
  • I have not merged my remote branch into remote master. Id like to do it as github offers "the automatic" way. Will it then have my version of the flles? And if there is something not working well then I can revert to the premerge state? Sorry, the questions might be a bit dumb, but I am very new to this part :)
    – Petroskali
    Commented Jan 17, 2018 at 4:17
  • I forget what GitHub's automatic revert does. Make sure it doesn't remove any commits. You can handle this entirely from your local setup if you want. Commented Jan 17, 2018 at 4:18
  • local sounds good. How do I push to master then (so it could be reverted) At the moment these are my last commands: git add ../client/controllers/bronCtrl.js git add ../client/partials/landing.html git add ../client/css/main.css git commit -m "Real commit" git push origin petroskali
    – Petroskali
    Commented Jan 17, 2018 at 4:26
  • Well if you haven't merged to master yet, then there is nothing to worry about. If you have merged, then use my answer. Commented Jan 17, 2018 at 4:28
  • When reverting a merge you usually have to tell git which parent to use as the mainline with the -m option Commented Jan 17, 2018 at 5:02
0

For going back once committing the changes, you can use the command:

               $git revert <commit hash code>

You can see the see the commit hash code by using the command:

               $git log --oneline

This will show the commit hash code and the commit message.

By using the revert command only the lastest commit can be reverted.

After using the revert command in the git bash, if there is a conflict then you can abort the revert(in case you don't want to revert after seeing the conflict) by using the command:

              $git revert --abort

After resolving the conflict if you want to continue, then use the command:

              $git revert --continue

This process will revert the latest commit.

Not the answer you're looking for? Browse other questions tagged or ask your own question.