First, start by forgetting everything you know about branch names. :-) The problem here is that these names are almost a sort of hint. Anyone can change them, any way they like, any time. A branch name just helps you (and Git) get started in terms of finding commits. The names don't matter; it's the commits that matter.
(If people are careful and disciplined about the way they use the names, the hints will never mislead you. Are you, and your team, and any people not on your team who you're also relying upon, all very careful and disciplined? If the answer is "no" even one day of the year, you'll want to pay attention to commits. Starting from the hints is fine, it's like believing that the sugar bowl for the coffee really does contain sugar. But if you have an office prankster and the coffee goes weird, maybe the sugar bowl is now full of salt.)
Now that you know what not to trust: how to view commits
You would think git log
would be the way to view commits ... and it is, but by default it doesn't show you everything. This is on purpose: "everything" is usually way too much. In my opinion, though, what it shows you by default often isn't enough. This may be one reason Git GUIs are so popular.
There is no perfect viewer. See Pretty git branch graphs and Visualizing branch topology in git for many possibilities. Gitk by default shows you more than git log
. Both, however, by default start from HEAD
. What I personally do most often is to get help from a dog: git log --all --decorate --oneline --graph
. (I actually have this as an alias, git lola
, but "a dog" is even more memorable! Instead of --all
, you might want --branches
or --branches --remotes
, but --all
is the only way to get A DOG out of it.) You get the same thing, but in graphics, with gitk --all
.
Anyway, the point is that this kind of view, that (a) includes the graph and (b) shows you all starting-points (including all branch and remote-tracking branch names), gives you a great deal more information. You will be able to see commits you have that they don't, and commits they have that you don't.
If you want to discard what you have
Sometimes you want to throw away everything you have in favor of everything they have. That's easy enough:
git reset --hard origin/master
(while on your own master
). Be careful with this command as it does something you cannot undo! In particular, the --hard
step discards uncommitted work-tree changes and you cannot get those back. The git reset
step also says "I don't want my current branch name (whatever that is, er, I think it's master
) to save my commits any more, I want it to remember only their origin/master
commits." So now your current branch name (you think it's master
) only remembers the same commits as origin/master
. Better check and make sure your current branch name really is master
, eh?
(If it's not, those commits are recoverable for a while, unlike unsaved work-tree work.)
Note that when you do this git reset
, you have just moved your own master
in a potentially-undisciplined way. Maybe you're the office prankster. :-)