Search code examples
gitgit-remote

How to synchronize with the remote if local is ahead of the remote?


I've recently started to use Git. I've pulled a project from GitHub server and started to work on it in the develop branch (on my local develop branch that is tracking origin/develop remote). I've just added a new file and did not track it with Git yet.

Meanwhile another person reverted the origin/develop branch and removed the latest 6 commits.

How can I force-synchronize my branch with the remote? I want to revert back my local branch so that it will be consistent with the remote. I've tried forced-pull but it didn't work:

$ git pull origin +develop
From https://***ADDRESS***
 * branch            develop    -> FETCH_HEAD
Already up to date.

$ git status
On branch develop
Your branch is ahead of 'origin/develop' by 6 commits.
  (use "git push" to publish your local commits)

Solution

  • You can

    $ git checkout origin/develop
    

    to get their copy of the code in your working directory. Then, perhaps, cherry-pick your change on top (this command cherry-picks only the last commit from the specified branch). This should be clean if you're only adding a new file:

    $ git cherry-pick develop
    

    Then, maybe point your local branch to reflect the remote one (this does not change any files, but moves your local develop branch pointer to your current state):

    $ git checkout -B develop
    

    Read about each of these commands in the Git docs if you're unsure of what they do.