3

I have a remote git repository on bitbucket (it's private, so I can't share a link here, I'm afraid)... I have a couple of branches there, two of them are:

  • master
  • cert_creation

Yesterday I was in the branch *cert_creation* and I've done a

git add --all
git commit -m "changes blabla"
git push origin cert_creation

...and everything looked fine. When I had a look into the latest commit on the Bitbucket Website, all changes seemed to be saved.

However, when I opened some files today on my local computer, I thought, that they weren't up-to-date.

So I wanted to revert all changes on my local machine to the latest remote commit (which I knew was correct). I googled for reverting a local git repository to the latest remote commit. I used these commands:

git reset HEAD --hard
git clean -fd
git fetch origin
git reset --hard origin/master

(and a couple of others which didn't work). The problem now is, that I've messed up everything even more. Now I have a really old git commit on my local machine (which is from the last time I merged cert_creation to master).

Now the question is how can I "revert the revert", i.e. update my local git repository to the last remote commit. Probably this was asked 100 times already somewhere on the internet, but I cannot find the correct keywords :[] And as I messed up something already I really want to be sure to do the correct thing.

Thank you very much for your answer in advance, I really appreciate your help!

mozzbozz
  • 3,052
  • 5
  • 31
  • 44

2 Answers2

6

Well, I figured out how to do it now... In my case, I wanted to totally overwrite the local branch cert_creation with the remote branch cert_creation from bitbucket.

To do this, I check, in which branch I am currently in:

git branch

When I am in cert_creation currently, I have to leave it and go to master (or some other branch):

git checkout master

Now I delete the messed up branch locally:

 git branch -d cert_creation

...fetch the information about the correct remote branch:

git fetch origin cert_creation

...and finally rebuild the local branch based on the remote one:

git checkout -b cert_creation origin/cert_creation 

Also big thanks to this answer to some other question on stack overflow, which helped me to figure this out!

Community
  • 1
  • 1
mozzbozz
  • 3,052
  • 5
  • 31
  • 44
1

To get your local to the latest remote, just do git pull. git reset --hard sets your local HEAD to a particular commit and drops all successive changes. So you need to bring the changes down from the remote. You have done a hard reset so you should not have to worry about having local changes that would be merged in to your local.

FYI:

A revert is a commit that is the inverse of a previous commit. You have not done a revert so you don't need to "revert a revert".

UPDATE

Your problem with getting updates from the remote branch is because after you pushed your local branch to origin, you did not set your local branch to track. In order to push or pull, you need to specify the remote branch to push or pull from/to. To avoid this, you would run git branch -u origin/<branchname> <branchname>. This sets your local branch to track the remote and you will be able to do git pull without a problem. To see the tracking branches, run git branch -vv. This will show the branch name along with the latest commit and the remote branch (if any) that the branch is tracking.

Schleis
  • 41,516
  • 7
  • 68
  • 87
  • Thanks for your answer, but that was something I tried already (some minutes before your answer). However, I always got an error message when doing a git pull. ("You asked me to pull without telling me which branch you want to merge with, and branch.cert_creation.merge' in your configuration file does not tell me, either. Please [...]). So I thought that something is really messed up on my local branch. I found a solution for my problem, which I also posted as an answer. You posted your answer just when I wanted to post my solution. But thanks for your suggestion, it might help others! – mozzbozz Sep 26 '13 at 18:28
  • You got that error because after you pushed your branch to origin, you did not setup the local branch to track the remote. To avoid that in the future after you push your branch run `git branch -u origin/ `. Then you will be able to pull without a problem. – Schleis Sep 26 '13 at 18:32
  • Ok, thanks. So do I get it right, that I have to do that "git branch -u origin/ " every time after I do a push? Or only once? Because I haven't read about that yet, only about the three lines I posted in my question (add, commit, push)? – mozzbozz Sep 26 '13 at 19:04
  • When you first push your branch to the origin you need to do that command. Git doesn't make your local branch track the remote when you push it. That command tells your local branch to start tracking the remote so that later you can push and pull. – Schleis Sep 26 '13 at 19:06
  • 1
    Tried that command now: "git branch -u origin/cert_creation cert_creation", however I got the error message "error: unknown switch `u'"... Some google requests came up with [this](http://stackoverflow.com/a/2286030/1202500) nifty stackoverflow post :) As Ubuntu 12.04 comes with git 1.7.*, the -u syntax won't work and I had to choose the longer equivalent: git branch --set-upstream cert_creation origin/cert_creation - thanks again for your help and information, now I know a lot more about git! :D – mozzbozz Sep 26 '13 at 21:14