1

My group has a policy that only one person can change the master branch on GitHub. He is a Git guru and does complicated commit management on origin/master.

Others have been told to use (after INSURING I'm on the master branch):

git pull --rebase origin master

to insure that we are mirroring origin perfectly. However, I know I've forgotten the --rebase occasionally. Now I'm wondering, is my local master out of sync with the one on origin?

Does the above command only succeed if they are in sync? If not what can I do?

David H
  • 40,852
  • 12
  • 92
  • 138
  • 2
    I recommend avoiding `git pull` entirely. Run `git fetch`, then look at what got fetched, then decide what you want to do. In this case it's always `git rebase`, which makes the decision easy, and makes it possible to use `git pull` directly, but that's kind of a bad habit, in my opinion: it's like assuming your gun is never loaded, and using the trigger to take it out of the holster. :-) – torek Mar 30 '18 at 21:34
  • so, you never commit to master locally? – Yuri G. Mar 30 '18 at 22:21
  • Correct - locally master should be readonly – David H Mar 30 '18 at 22:35
  • @DavidH nice to see you again (from iOS IP address) but this time with git rebase :D. Care to check my question https://stackoverflow.com/questions/49894883/rebase-once-commit-some-changes-then-rebase-again-some-unexpected-conflicts – Qiulang Apr 18 '18 at 08:38

2 Answers2

1

If you do not specify --rebase, you will merge the master branch of upstream origin (i.e. origin/master) into your currently checked branch. If you specify --rebase, your changes will be replayed on top of origin/master. The end result should be identical.

If you tend to forget to specify --rebase, then you can also enable it with a config option: git config pull.rebase true. With this option enabled, each time you pull, your changes will be rebased instead of merged.


To answer the title of your question: run git log HEAD..origin/master (after syncing histories with git fetch). If it does not show any commits, your work is up-to-date with origin. If it shows commits, then there are commits in origin/master which are not yet integrated into your local working copy.

knittl
  • 246,190
  • 53
  • 318
  • 364
1

Checking for updates

The simplest way to see if your local branch is up to date with its remote counterpart is to use git fetch followed by git status.

git fetch updates the information about remote branches, but doesn't update their local counterparts.

git status shows information about current condition of a branch and working tree. If it is up to the date with the remote, it should say

On branch x
Your branch is up to date with 'origin/x'.

where x would be master in your specific case.

On the other hand, if your branch is not up to date, because you added a single commit, for example, it will say

On branch x
Your branch is ahead of 'origin/x' by 1 commit.

What does git pull --rebase do

git pull, is actually a shortcut for git fetch followed by git merge. With --rebase modifier, instead of git merge git rebase is used.

In short, git merge and git rebase both apply changes from one branch to another, but they differ in the way they do that. You might want to check out the documentation or the following SO answers for more info: git merge, git rebase.

Wiktor Czajkowski
  • 1,623
  • 1
  • 14
  • 20