301

What are the Git commands to do the following workflow?

Scenario

I cloned from a repository and did some commits of my own to my local repository. In the meantime, my colleagues made commits to the remote repository. Now, I want to:

  1. Check whether there are any new commits from other people on the remote repository, i.e. origin?

  2. Say there were three new commits on the remote repository since my last pull, I would like to diff the remote repository's commits, i.e. HEAD~3 with HEAD~2, HEAD~2 with HEAD~1 and HEAD~1 with HEAD.

  3. After knowing what changed remotely, I want to get the latest commits from the others.

My findings so far

For step 2: I know the caret notation HEAD^, HEAD^^ etc. and the tilde notation HEAD~2, HEAD~3, etc.

For step 3: That is, I guess, just a git pull.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lernkurve
  • 20,203
  • 28
  • 86
  • 118

8 Answers8

295

You could git fetch origin to update the remote branch in your repository to point to the latest version. For a diff against the remote:

git diff origin/master

Yes, you can use caret notation as well.

If you want to accept the remote changes:

git merge origin/master
Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
  • 41
    The diff looks reversed. I find it easier to use `git diff HEAD origin/master` so the diff shows what will be applied if I accept the remote changes. – cbliard Jul 30 '13 at 07:41
  • 2
    "git fetch origin" and "git show-branch *master" were useful to me. – Léa Massiot May 16 '17 at 17:31
177
git remote update && git status 

Found this on the answer to Check if pull needed in Git

git remote update to bring your remote refs up to date. Then you can do one of several things, such as:

  1. git status -uno will tell you whether the branch you are tracking is ahead, behind or has diverged. If it says nothing, the local and remote are the same.

  2. git show-branch *master will show you the commits in all of the branches whose names end in master (eg master and origin/master).

If you use -v with git remote update you can see which branches got updated, so you don't really need any further commands.

Community
  • 1
  • 1
Rajani Karuturi
  • 3,450
  • 3
  • 27
  • 40
  • Not enough. I have to do a `git pull ` afterwards as soon as I need to push, because the tip of my local branch was behind the remote counterpart. – Overdrivr Sep 05 '16 at 07:02
  • 6
    @Overdrivr the question asks for a way to check changes before getting the commits to the local branch. so, yes, you have to update your local branch after checking for changes. – Rajani Karuturi Sep 06 '16 at 04:14
  • Is this for remote being origin or upstream ? – vikramvi Sep 29 '17 at 08:54
  • 1
    That -v option doesn't work. For `git remote update -v` I got `error: unknown switch \`v'` – Shad Mar 19 '18 at 18:49
  • @vikramvi `git remote update` will update all the remotes. `git status` will check for changes in this branch to the configured remote. You could use `git remote -v` to see all the configured remotes and `git branch -vv` to see which remote this branch is tracking – Rajani Karuturi Oct 01 '18 at 10:14
  • 3
    @Shad you should do `git remote -v update` not `git remote update -v` – Rajani Karuturi Oct 01 '18 at 10:15
39

A good way to have a synthetic view of what's going on "origin" is:

git remote show origin
jag
  • 880
  • 5
  • 6
  • 11
    But that command doesn't show me how many commits there have been on "origin" since my last pull, does it? The way I understood it "git remote show origin" is a local operation and does not go over the network to fetch information. – Lernkurve Mar 25 '10 at 12:05
  • 2
    The output ***doesn't contain any*** information about the *content* of the remote. – Peter Mortensen Oct 25 '20 at 04:26
  • @Lernkurve The remote repository is now queried by default, but there is a switch (`-n`) for using the remote's cached status: https://git-scm.com/docs/git-remote#Documentation/git-remote.txt-emshowem – mzuther Sep 19 '22 at 10:01
35

I just use

git remote update
git status

The latter then reports how many commits behind my local is (if any).

Then

git pull origin master

to bring my local up to date :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paul 501
  • 707
  • 7
  • 14
22

My regular question is rather "anything new or changed in repo" so whatchanged comes handy. Found it here.

git whatchanged origin/master -n 1
iceCode
  • 321
  • 2
  • 3
11

One potential solution

Thanks to Alan Haggai Alavi's solution I came up with the following potential workflow:

Step 1:

git fetch origin

Step 2:

git checkout -b localTempOfOriginMaster origin/master
git difftool HEAD~3 HEAD~2
git difftool HEAD~2 HEAD~1
git difftool HEAD~1 HEAD~0

Step 3:

git checkout master
git branch -D localTempOfOriginMaster
git merge origin/master
Community
  • 1
  • 1
Lernkurve
  • 20,203
  • 28
  • 86
  • 118
4

git status does not always show the difference between master and origin/master even after a fetch.

If you want the combination git fetch origin && git status to work, you need to specify the tracking information between the local branch and origin:

# git branch --set-upstream-to=origin/<branch> <branch>

For the master branch:

git branch --set-upstream-to=origin/master master
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jackal
  • 1,143
  • 17
  • 32
2

I simply use

git fetch origin

to fetch the remote changes, and then I view both local and pending remote commits (and their associated changes) with the nice gitk tool involving the --all argument like:

gitk --all
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
raphael
  • 2,159
  • 17
  • 20
  • 1
    The question specified a git command. There are many git tools that can be used to check a remote branch status. Promoting a particular one is not very helpful. I realize this is a tool documented in the official git documentation, but it is still independently maintained, hence my disagreement. – prufrofro Apr 08 '21 at 19:34