Question

I started using Git a few months ago and I wanted a GUI client so I am using SmartGit, which I find great.

The only thing is I get very confused when I try to go back to a previous committ; for instance recently I discovered a bug which had gone undetected in my project. I used branch>>checkout in SmartGit to change to an earlier version. This worked fine however SmartGit recommended that I should switch to a local branch to check out the committ. I have a few questions (see below) about this but my main problem is that when I read up on the documentaion for each they seem to be saying slightly different things. Git's documentation appears to say that "checkout" is for

Checking out a branch or paths to the working tree

whereas smartgit says that you use checkout to

switch the working tree to a certain commit.

  • So are they two different things (ie different uses of the word checkout) and if not why does one refer to branch and the other to commit?
  • How does it work if I want to move to an earlier commit in the same branch?
  • Why does smartgit recommend that I start a new branch for this, and is it important?
  • If I must set up a new branch every time then should I set up a special branch for this so that I don't end up with a lot of random branches?
  • What is the signifignance of "local" in the local branch sugesstion, why would it have to be local, does this mean that it should not be synced with my bitbucket account?
Was it helpful?

Solution

No

I don't use smartgit but these descriptions are equivalent.

Branch confusion

A branch in git is just a pointer to a commit. So when you checkout a branch, you're updating your working copy to a specific commit (it just has a nice name like "master").

If you want to checkout a commit (it doesn't matter at all where in your repo history it is) - you can just check it out by hash. If you are only checking it out to look around, no it's not necessary to create a new branch. Depending on what you're doing however, it might be a good idea to do so. You can do that before or after checking out a commit - to git at least - it doesn't matter.

If you do things with git's cli you'll see something like this:

(master) $ git checkout c5d12604219ac1d6fa0f7d8ff4be98c8a3eee0da
Note: checking out 'c5d12604219ac1d6fa0f7d8ff4be98c8a3eee0da'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at c5d1260... Added CakeResponse::file() for serving files as response
((no branch))]$

I.e. git will also prompt you, with an example command, to optionally create a branch for the commit you've checked out and explain why (to retain commits you create).

Local branches

A local branch just means a branch that exists in your local working copy (it appears in .git/config, it shows up when you do git branch). A local branch may or may not have an upstream remote branch (may or may not have something to push to) - that's your choice and doesn't affect whether the branch is local or not - it's defined in your repo, therefore it is local.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top