After I have git add, and git commit, my workspace, index cached, and local repository became the same. So how can I know when I "push", what differences are there in this patch set?
3 Answers
In conjunction with git-diff
git diff origin/master [ <local-branch-name> ]
you might be looking for
git push --dry-run --verbose
This will simulate a push and print all information you need on the way. From the git man page:
git-push:
-v
--verbose
Run verbosely.
-n
--dry-run
Do everything except actually send the updates.
You can try git diff origin/remote-branch-name..local-branch-name
or git log -p origin/branch-name..local-branch-name
.
If you want to compare the current branch you are on, then you can leave off the local-branch name i.e. git diff origin/remote-branch-name..
, git log -p origin/branch-name..
.
Rashmirathi's answer is correct (and upvoted) but there is a key point obscured here: When you run git push
, you don't send patches, you send complete commits.
Here's a simple example:
$ cd /tmp
$ git clone upstream.git downstream
Cloning into 'downstream'...
done.
$ cd downstream
$ echo 'add stuff to the readme file' >> README
$ git add README
$ git commit -m 'make a mistake'
[master 20caf72] make a mistake
1 file changed, 1 insertion(+)
Now we have a commit we don't want, so we un-do the change we did (I could simply git revert HEAD
but I want to do it a more complicated way).
$ ed README
48
$d
w
19
q
(this deletes the last line of file README
)
$ git add README
$ git commit -m 'un-make the mistake'
[master b0ebc79] un-make the mistake
1 file changed, 1 deletion(-)
(we are now ready to push)
$ git push origin master
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 440 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To [path]/upstream.git
49c7832..b0ebc79 master -> master
$
Even though there was no actual change at all, I still just pushed two commits. Let's go over to the upstream (bare) repository and see:
$ cd ../upstream.git
$ git log --oneline
b0ebc79 un-make the mistake
20caf72 make a mistake
49c7832 initial
Indeed, there they are.
The right question is thus not "what patches will I send" but rather "what commits will I send". Most of the time this does not make that much difference, but if you accidentally commit a 4 gigabyte video file, then commit a removal of that same file, the diff from "what they have" to "your newest commit" may be tiny, while the commits you will send include one very big one.
There's one other slightly obscure point, which is that your origin/master
(or origin/branch
if you are working on your branch
) is based on your Git's most recent knowledge about the other, upstream Git's branches. It's possible that something else has changed upstream. If so, do you want to see it, or do you want to ignore it? If you do want to see it, you will have to choose how you want to see it—and at some point you are likely to have to stop ignoring it.
To update your own Git's idea of what is on the other Git, you can run git fetch origin
, which brings over anything new they have that you don't, and then updates your origin/master
, origin/branch
, and so on. If they have nothing new, git fetch
brings over nothing and updates nothing. All of the origin/*
names exist just so that your Git can tell you "this is what I found in the other Git, the last time I talked with it."
When you run:
git log -p origin/master..master
you are instructing your Git to look at commits that are on your master
that are not on your origin/master
. That's your origin/master
—so it's only as up to date as your most recent git fetch
.
Key take-aways
Git sends and receives commits, not patches. Git can make patches for you, so that you can email them, but
fetch
andpush
work on commits.Use
git log
to view commits. Add-p
to view ordinary (non-merge) commits as patches: Git will make them for you, on the spot, just so that you can see them. Add--oneline
instead to just see the commit subject lines.The
log
command shows you commits starting from the place you name, or from yourHEAD
if you don't name anything. It keeps going until it runs out of commits, or stops when it hits a commit you've told it to stop at. That's whatorigin/master..master
is about. It tellsgit log
: "Start from mymaster
, and stop when you reach myorigin/master
." The two dots are required here.Use
git diff
to compare two commits:git diff origin/master master
, for instance. You can spell this asgit diff origin/master..master
if you prefer—but watch out, becausegit diff
simply compares the end points, skipping over all the commits in the middle. If you accidentally committed a big movie file, then removed it,git diff
will skip right over that (butgit log
won't!).Use
origin/master
and otherorigin/*
names to see what your Git remembers from the last time it got these from the other Git.Run
git fetch
(orgit fetch origin
) to get your Git to talk to the other Git again and pick up anything new. If the other Git is very active, you might need to do this often. If it's pretty quiet, or is your own personal GitHub repository, you might never need to do this (because your Git also updates your ownorigin/master
and the like on a successfulgit push
).