By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,706 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,706 IT Pros & Developers. It's quick & easy.

How Does Branching Work in Git?

pbmods
Expert 5K+
P: 5,821
While researching SCM software, I came across Git . It looked phenomenal, especially the part about branching. Or at least, the way I thought it handled branching looked phenomenal.

I'm curious if anyone out there is familiar enough with Git to shed some light on this issue:

At first, I got really excited because I thought Git was moving my files around behind the scenes so that I could maintain different versions of my filesystem, but use the same path to access them.

E.g.,
Expand|Select|Wrap|Line Numbers
  1. > cat testfile.txt
  2.     Hello, 
  3. > git branch newbranch
  4. > echo 'newbranch!' >> testfile.txt
  5. > cat testfile.txt
  6.     Hello, newbranch!
  7. > git checkout master
  8. > cat testfile.txt
  9.     Hello, 
  10. > git checkout newbranch
  11. > cat testfile.txt
  12.     Hello, newbranch!
  13.  
The idea here is that I thought Git would store a separate clone for each branch, and then manipulate a symlink so that my filesystem would appear to mimic the current branch.

I suppose I could set up hooks to accomplish this, but back to my question... :P

Okay, so it doesn't do that. My next idea was that I can use branches to 'categorize' my changes.

For example:
Expand|Select|Wrap|Line Numbers
  1. > git add file1.txt file2.txt file3.txt
  2. > git status
  3. # On branch master
  4. # Changes to be committed:
  5. #   (use "git reset HEAD <file>..." to unstage)
  6. #
  7. #    modified: file1.txt
  8. #    modified: file2.txt
  9. #    modified: file3.txt
  10. > git checkout newbranch
  11. > git add file4.txt
  12. > git status
  13. # On branch newbranch
  14. # Changes to be committed:
  15. #   (use "git reset HEAD <file>..." to unstage)
  16. #
  17. #    modified: file4.txt
  18. #
  19. # Changes on branch master:
  20. #
  21. #    modified: file1.txt
  22. #    modified: file2.txt
  23. #    modified: file3.txt
  24.  
Or something to that effect.

Instead, this is what I get:
Expand|Select|Wrap|Line Numbers
  1. > git add file1.txt file2.txt file3.txt
  2. > git status
  3. # On branch master
  4. # Changes to be committed:
  5. #   (use "git reset HEAD <file>..." to unstage)
  6. #
  7. #    modified: file1.txt
  8. #    modified: file2.txt
  9. #    modified: file3.txt
  10. > git checkout newbranch
  11. > git add file4.txt
  12. > git status
  13. # On branch newbranch
  14. # Changes to be committed:
  15. #   (use "git reset HEAD <file>..." to unstage)
  16. #
  17. #    modified: file1.txt
  18. #    modified: file2.txt
  19. #    modified: file3.txt
  20. #    modified: file4.txt
  21.  
This is not exactly desirable, under any circumstances that I can determine.

What's branching supposed to do in Git?

Thanks for your time.
Jun 1 '08 #1
Share this Question
Share on Google+
2 Replies

RedSon
Expert 5K+
P: 5,000
It looks like branching works the same in git as it does in svn. How it works behind the scenes I have no idea. Are you asking about the technical details of git or about branching in general?
Jun 5 '08 #2

pbmods
Expert 5K+
P: 5,821
Heya, RedSon.

Thanks for your response. I figured out what was causing the confusion.

When you checkout a brach in Git, it uses the HEAD of that branch to determine what files have changed.

Here's where it gets tricky.

If you have any *uncommitted* changes, git doesn't touch them.

So if I were to modify the first example in my OP, this will work as expected:
Expand|Select|Wrap|Line Numbers
  1. > cat testfile.txt
  2.     Hello,
  3. > git branch newbranch
  4. > echo 'newbranch!' >> testfile.txt
  5. > cat testfile.txt
  6.     Hello, newbranch!
  7. > git add testfile.txt
  8. > git commit -m 'Personalized testfile.txt'
  9. > git checkout master
  10. > cat testfile.txt
  11.     Hello,
  12. > git checkout newbranch
  13. > cat testfile.txt
  14.     Hello, newbranch!
  15.  
I don't know why I remember it being different in SVN. Probably because I just copied my repository into its own directory every time I created a separate branch, so I never ran into this problem.
Jun 7 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.