iksemyonov
iksemyonov

Reputation: 4196

How to limit file modifications to a single branch in Git

I'm starting to learn Git, reading the ProGit book from time to time. I've heard that the most powerful feature of Git is the branches, so I tried to use them. I'm hacking on the KDE project, so there is a remote server and a local copy.

So here's my situation. I've coded a bugfix, but the developer responsible for that code area has gone offline without having given me a shipit, so I decided to do a different fix in the meantime. I've heard that branches can (and more importantly, should) be used for such situations. OK, I created a local branch

git branch bugfix

then switched to that branch

git checkout bugfix

ad then discovered that the files I had modified for the original fix were still modified. (Of course, I needed a clean directory to be able to push only the second bugfix without the first one.) Well, no problem, I thought, let's reset if that's what git status tells me to do. I did a reset and indeed got a clean dir. But hey, after I switched back to master

git checkout master

the modified files were no longer modified there! It was a clean dir.

Now what's the point of branches? Can't have two versions of a file, modified in one branch and unmodified in another one? I know about git stash, but if I do that, unstashing the changes will kill the second bugfix, because IIRC stash simply replaces one file with another one, no merging is done.

What am I doing wrong here? Why is it impossible to have the file modifed in one branch and unmodified in another?

Upvotes: 2

Views: 338

Answers (3)

Dietrich Epp
Dietrich Epp

Reputation: 213847

So, here's what happened. You started off with a commit A from the original developer. You fixed some bugs and now you're in B. There's a remote branch pointing to A called origin/original-branch, and there's a local branch pointing to B called my-changes. I don't know what they're actually called (use git branch -a to list them).

....->  A  ->  B
        ^      ^
        |      +-- my-changes
        |
        +-- origin/original-branch

If you're on B and you make a branch and start making changes, you'll get this:

....->  A  ->  B  ->  C
        ^      ^      ^
        |      |      +-- bugfix
        |      |
        |      +-- my-changes
        |
        +-- origin/original-branch

This is not what you want. You want this:

               +-- my-changes
               v
....->  A  ->  B
         \
           ->  C
        ^      ^
        |      +-- bugfix
        |
        +-- origin/original-branch

So you have to make your new branch start where the other developer's branch is.

git branch bugfix origin/original-branch
git checkout bugfix

That specifies that the new branch starts from the other developer's work. If you do this instead:

git branch bugfix # not what you want

This will cause the bugfix branch to start from wherever you are right now.

Upvotes: 5

Andy
Andy

Reputation: 46544

You have to commit the files onto a branch in order for them to be on that branch.

In your case, you made some modifications to some files, but didn't commit them in to any branch.

What you could have done was:

  1. git checkout -b bugfix
  2. git commit -am "Fixed some bugs"
  3. git checkout master
  4. git checkout -b bugfixtwo
  5. At this point, your original work will be on master, your first changes will be bugfix, and you will be ready to do new work on bugfixtwo

git checkout -b bugFix creates and checks out a branch in one step.

Upvotes: 0

Alexander Pavlov
Alexander Pavlov

Reputation: 32306

In order to "remember" a changed file on a branch, you need to commit your changes (added, removed, or modified files): git commit -a (the -a argument stands for "all changes") but you definitely need to understand git branches. This book is very useful.

Upvotes: 0

Related Questions