Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine master, origin, head

I find myself getting lost in git branches.

How can I determine what my origin/master/head is if I'm in an arbitrary git branch?

This way I can predict the effects of commands such as those shown here.

like image 563
jayunit100 Avatar asked Jan 11 '12 20:01

jayunit100


2 Answers

It's not clear from your question that you entirely understand what origin, master, and HEAD refer to - only one of them is actually a branch. (And it's HEAD, not head.) It's quite possible some of your confusion stems from a lack of understanding of a couple core things, so it really is a good idea for you to thoroughly read a references like the Git Book (which you linked to), starting from the beginning. You might also appreciate the Git Parable, which gives a friendly, story-like description of the basic ideas of Git.

HEAD refers to the currently checked-out commit. It normally does so via a branch; HEAD points to the branch, which points to the commit - so usually we just talk about what branch is checked out. So you seem to be asking either how to determine what branch you have checked out, or what commit that branch points to. You can use git branch to determine your current branch, and also list your other branches. If you want to know more about the commit that branch points to, you could use git show (equivalent to git show HEAD).

master is the default name given to the branch automatically created in a new repository. The convention (which is almost universally followed) is that the master branch is the stable branch, representing the canonical current content of the repository. This means that there is almost certainly a master branch in your repository, since you either created it or cloned it from somewhere that has a master branch. Again, it's unclear exactly what you need, but a commonly used command is git log master, which will show commits starting from the tip of the master branch, walking back in history. If you want to also see the changes the commits made, add the -p option. Adding the --decorate option will tell Git to annotate commits with any branches pointing to them (e.g. you'd see master on the top commit). Use --graph to see ASCII art history graph, handy for understanding merges. See the git-log manpage for more options - there are plenty. If you prefer graphical history viewing, try gitk master. To include all branches, including remote ones, you could use gitk --all.

origin is the name for the default remote. When you clone a repository, this is automatically set up for you. You can see some information about it using git remote show origin, which will show you the URL for the repo, its HEAD, the branches on the remote, and any local branches tracking those remote branches.

like image 192
Cascabel Avatar answered Oct 17 '22 20:10

Cascabel


you can always see what your tracking branches are doing with

git branch -r | xargs git log --decorate

add --graph if you want to see how they are related with branching and merging. Refresh the tracking branches with

git fetch
like image 22
Adam Dymitruk Avatar answered Oct 17 '22 20:10

Adam Dymitruk