0

When working on two workstations (say one from the office, one from home) on the same git project, after committing/pushing from one and trying to pull from the other (say the next day), branch seems always to be out of sync after

git pull --all

and the solution proposed here is needed.

Why is that? (I never do a forced push as implied in the above post).

musiKk
  • 14,751
  • 4
  • 55
  • 82
pkaramol
  • 16,451
  • 43
  • 149
  • 324
  • 1
    When you say "out of sync," what do you mean? Could you give an example? – David Deutsch Jun 09 '15 at 12:06
  • Different contents in the remote than in the local branch (which tracks the remote!). The local has not incorporated the changes after `git pull --all`. For example I see directories in the remote (when accessing it through the web of github or bbucket) which I do not see locally. These changes were committed when working in my previous workstation (e.g. the day before). – pkaramol Jun 09 '15 at 16:54
  • Hmmm, that is a strange one. You are sure that your local is tracking the remote? When you do `git branch -vv` does it show it as tracking? Does this happen on both of your machines? – David Deutsch Jun 09 '15 at 17:58

1 Answers1

1

The safest way to handle git repositories from getting corrupted due to some dangling heads or forced push is to use the following:

git fetch
git rebase origin/your_branch

git pull by default fetches and then merges instead of rebasing.

Difference between merge and rebase can be found here: https://www.atlassian.com/git/tutorials/merging-vs-rebasing/conceptual-overview

What you should do now is remove the repo from both of your workstations(obviously after committing and pushing all the local changes). Clone it again. Create a local tracking branch for 'your_origin_branch'. And follow the above procedure to make changes henceforth.

Animesh Sharma
  • 3,258
  • 1
  • 17
  • 33
  • Small addition: If you fetch your changes from one workstation, you can also `git pull --ff --ff-only` to "pull && merge --ff-only`. This, of course, does only work if there are no (unpublished) changes in your local repo on the current branch. – musicmatze Jun 09 '15 at 09:44