1

I have one repository which is called test-repo.

Right now, I have two branches on that repository, called:

  1. master
  2. master-bkp

Both branches contains only one file called test.txt.

test.txt in master contains

hello I am here

and test.txt in master-bkp contains

hello I am there

Now, in my local repository I write:

git checkout master
git merge master-bkp

Now test.txt in master contains

hello I am here
hello I am there

Can someone explain how Git works internally when I trigger these two commands?

git checkout master
git checkout master-bkp

Note : Please don't give opinions on git checkout, I need an explanation about internal logic behind it.

bouteillebleu
  • 2,456
  • 23
  • 32
ritesh9984
  • 418
  • 1
  • 5
  • 17
  • 3
    You probably should give at least some indication of what you _already_ understand about the commands you performed. Otherwise you could be asking someone to explain everything about git from scratch. – khelwood Oct 31 '17 at 13:12
  • Read this: [Basic branching and merging](https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging) – bhansa Oct 31 '17 at 13:15
  • In the scenario you've described, you only ran `git checkout master` once, just before running the merge. You have not executed `git checkout master-bkp`, not even once. Please clarify your final question, or improve the example so we know why and in what context you are asking about two checkouts. – quetzalcoatl Oct 31 '17 at 13:20
  • 2
    Another thing is, why did you include `merge` in your example? You are asking about `checkout`. These two are unrelated. Are you sure you have not mistaken effects of `merge` for effects of `checkout`? – quetzalcoatl Oct 31 '17 at 13:21
  • I'm pretty sure his question is only about `checkout`, maybe I'm wrong. – scharette Oct 31 '17 at 13:23

1 Answers1

3

Git checkout is well documented, this is from the doc:

Updates files in the working tree to match the version in the index or the specified tree. If no paths are given, git checkout will also update HEAD to set the specified branch as the current branch.

Since I know you can read documentation here is how I see it. Git is a revision control system that provides many tools in order to manage your code history. Since you have many branches I will assume you understand correctly how they work. Checkout is only a command you use to move to others branches. The main goal is to updates files in the working tree so you get the data associated with a certain branch.

Behind the scenes

There is no magic. Has it said in the doc it only update HEAD so you are on the right branch. this is it. Also, as @ quetzalcoatl pointed out, it is important to know that git checkout performs a change detection which forbids changing branches while having uncommited work. Performing a git checkout can therefore be considered 'safe'.

Note

Sometimes beginners have a hard time understanding the difference between git checkout and git reset, even though they're completely different. The easiest way to see it in my opinion is :

What do you want to update ?

If the answer is my current branch go read the documentation on git reset. If your goal is to update your working tree based on another branch then git checkout is what you're looking for.

There is only one exception to this statement. If your goal is to overwrite one file, you can use git checkout, take a look at this answer.

scharette
  • 9,437
  • 8
  • 33
  • 67
  • 1
    `it only update HEAD` and also of course updates files in working copy to match the HEAD -- and in that moment it performs change-detection and nondestructive merges in case you have uncommitted changes in your working copy.. so in fact there's a bit of happening there – quetzalcoatl Oct 31 '17 at 13:24
  • Good point! it is missing the change detection I will add thank you !! – scharette Oct 31 '17 at 13:26
  • `git checkout and git reset, even though they're completely different` - consider `git checkout my/directory/foobar.cs` and compare it to the idea of `checkout` and `reset`. This corner case use of checkout IS a one-file reset, and believe me, I've seen a lot of git-for-beginners sites which show that use case of checkout command, claiming it's handy to get a fresh version of a file from current or different branch – quetzalcoatl Oct 31 '17 at 13:26
  • @quetzalcoatl I really appreciate your insight! but I must admit I don't agree with your second comment. That is not how I would recommend approaching such situation. Take a look at this [answer](https://stackoverflow.com/questions/5657451/how-to-get-a-fresh-copy-of-a-branch-from-the-remote-repository). Updated answer according to your first comment though. Thanks – scharette Oct 31 '17 at 13:32
  • @quetzalcoatl oh just noticed you said for one file! I will add a link – scharette Oct 31 '17 at 13:34