Lernkurve
Lernkurve

Reputation: 21522

How to check for changes on remote (origin) Git repository

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.

Upvotes: 308

Views: 377505

Answers (10)

Seamus
Seamus

Reputation: 213

This question appears to be a duplicate of another question here on SO.

I'm adding this answer b/c I feel that most of the answers here are (either) wrong/out-dated or too complicated. And strictly as an opinion, I'll opine that the authors/maintainers of git are suffering from something similar to what US President Joe Biden has! IOW, the fact that you cannot get a straight answer from git status spells dysfunction in git-world.

That said, my simplified answer to the Question "Check if pull needed in Git" is this:

## cd to git repo; e.g.
$ cd ~/blockhead.git
$ git pull --dry-run         # alternatively: 'git fetch --dry-run'
...
From thegitremote:/home/joe/git-srv/blockhead.git
   2acea0b..b797bb0  master     -> origin/master  # <=== THIS MEANS A CHANGE HAS BEEN MADE

IOW: If there is a diff between the local and remote repos, it will be expressed in the output of git pull --dry-run (similar to that shown above). If there is no diff the output will be null.

Upvotes: 0

iceCode
iceCode

Reputation: 351

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

git whatchanged origin/master -n 1

Upvotes: 25

fozzybear
fozzybear

Reputation: 147

Regarding 1), only checking for, but not fetching/pulling any changes from remote yet, you could use:

git fetch -j 4 --dry-run --porcelain | head -n 1

This will only look for and show any new remote commits, running on 4 parallel jobs (faster) and limiting the result to only the first line/commit.

It is easily adapted to your needs and can be used in scripts due to the simple format, helping you to quickly check, if any changes are present, which can be processed further as required.

To get all new available commits, leave the head part beginning from the pipe symbol.

In order to actually download them, also remove --dry-run from the command.

I like to do a separate fetch and then git merge --progress --strategy-option=patience instead of git pull, to have better control and more information about what will happen, when merging the lokal worktree with remote changes.

EDIT: I also commonly use -p (prune) with fetch, so dangling remote-tracking references to remotely non-existent branches get removed locally.

Upvotes: 1

raphael
raphael

Reputation: 2950

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

Upvotes: 2

jackal
jackal

Reputation: 1219

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

Upvotes: 4

Paul 501
Paul 501

Reputation: 727

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 :)

Upvotes: 37

Rajani Karuturi
Rajani Karuturi

Reputation: 3483

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.

Upvotes: 178

Lernkurve
Lernkurve

Reputation: 21522

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

Upvotes: 11

jag
jag

Reputation: 908

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

git remote show origin

Upvotes: 42

Alan Haggai Alavi
Alan Haggai Alavi

Reputation: 74202

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

Upvotes: 302

Related Questions