How to let git check for updates on the master server?
I have very poor knowledge about git and would like to ask for help.
I have a linux(-only) application which shall only be "downloaded" (i.e. cloned) with git. On startup, the app shall ask the git "master server" (github) for whether there are updates.
Does git offer开发者_StackOverflow社区 a command to check for whether there is an update (without really updating - only checking)? Furthermore, can my app read the return value of that command?
If you do not want to merge, you can just git fetch yourremote/yourbranch
, the remote/branch specification usually being origin/master
. You could then parse the output of the command to see if new commits are actually present. You can refer to the latest fetched commit as either yourremote/yourbranch
or possibly by the symref FETCH_HEAD
.
Note: I was reminded that FETCH_HEAD
refers to the last branch that was fetched. Hence in general you cannot rely on git fetch yourremote
with FETCH_HEAD
since the former fetches all tracked branches, thus the latter may not refer to yourbranch
. Additionally,
- you end up fetching more than strictly necessary.
- also refer to Jefromi's answer to view but not actually downloaded changes
- the following are not necessarily the most compact formats, just readable examples.
That being said, here are some options for checking for updates of a remote branch, which we will denote with yourremote/yourbranch
:
0. Handling errors in the following operations:
0.1 If you attempt to git fetch yourremote
, and git
gives you an error like
conq: repository does not exist.
that probably means you don't have that remote-string defined. Check your defined remote-strings with git remote --verbose
, then git remote add yourremote yourremoteURI
as needed.
0.2 If git
gives you an error like
fatal: ambiguous argument 'yourremote/yourbranch': unknown revision or path not in the working tree.
that probably means you don't have yourremote/yourbranch
locally. I'll leave it to someone more knowledgeable to explain what it means to have something remote locally :-) but will say here only that you should be able to fix that error with
git fetch yourremote
after which you should be able to repeat your desired command successfully. (Provided you have defined git remote yourremote
correctly: see previous item.)
1. If you need detailed information, git show yourremote/yourbranch
and compare it to the current git show yourbranch
2. If you only want to see the differences, git diff yourbranch yourremote/yourbranch
3. If you prefer to make comparisons on the hash only, compare git rev-parse yourremote/yourbranch
to git rev-parse yourbranch
4. If you want to use the log to backtrack what happened, you can do something like git log --pretty=oneline yourremote/yourbranch...yourbranch
(note use of three dots).
If you really don't want to actually use bandwidth and fetch new commits, but just check whether there is anything to fetch, you can use:
git fetch --dry-run [remote]
where [remote]
defaults to origin
. You'll have to parse the output, though, which looks something like this:
From git://git.kernel.org/pub/scm/git/git
2e49dab..7f41b6b master -> origin/master
so it's really much easier to just fetch everything (git fetch [remote]
), and then look at the diff/log e.g. between master
and [remote]/master
.
I'd say git fetch is a potential solution. It only updates the index, not working code. In cases of large commit sets, this would involve a download of compressed files/info, so it may be more than you want, but it is the most useful download you can do.
精彩评论