newkid101 newkid101 - 1 month ago 14
Git Question

Git pull before rebasing

So I asked a question yesterday about rebasing in git and got some good answers on what to do. However When I proceeded, I ran into issues that I dont understand 1 bit.

To give a quick overview:

I branched out (

Branch2
) from another branch -
Branch1
.
Branch1
is remote and had a number of commits AFTER I branched from it. All those commits are not squashed. Meanwhile I went about making changes in
Branch2
. Now Im done with my changes and have to rebase
Branch2
on top of
Branch1
.

When I do
git status
in
Branch2
it lists all the files that I have changed (which seems right). However when I do a
git checkout Branch1
and the
git status
it lists the same files again. I dont understand this, I was under the impression that each branch is like a localized environment and does not show changes to other branches.

Another thing that has my head spinning is that -
Branch1
has moved forward since I branched out from it. Before rebasing I wanted to update my local copy of
Branch1
so that my changes in
Branch2
get rebased on top of the the most recent commits of
Branch1
so I did
git checkout Branch1
and
git pull
. However I got :

error: Your local changes to the following files would be overwritten by merge:
file...
Please, commit your changes or stash them before you can merge.
Aborting


I dont understand:


  1. Why are changes done in
    branch2
    showing in
    git status
    of
    Branch1
    ?

  2. If I commit and push my changes on
    Branch1
    , then
    git pull
    , where will my commit be placed as all the previous commits including the commit where I had branched of of in
    Branch1
    have been squashed.


Answer

When I do git status in Branch2 it lists all the files that I have changed (which seems right).

A branch is a pointer to a commit. git status shows the files that are modified but not committed. Maybe it seems right to you but until you commit the changes, checking out a different branch is a risky operation.

However when I do a git checkout Branch1 and the git status it lists the same files again.

This is because the uncommitted changes are not in a branch and that means they are not in the repository. They are only in the working tree.

I dont understand this, I was under the impression that each branch is like a localized environment and does not show changes to other branches.

Your impression is correct.

Another thing that has my head spinning is that - Branch1 has moved forward since I branched out from it.

Since you didn't create any commit in Branch2, you technically didn't "branched out" from it. Branch2 is just a commit in the past of Branch1 and not a real branch. Commit your changes on Branch2 and it will branch out.

However I got :

error: Your local changes to the following files would be overwritten by merge:
   file...
Please, commit your changes or stash 

Well, git is trying hard to not destroy your work. It suggests you what to do in order to be safe.


If I understood correctly your desire, the commands you have to run are as follows:

# go back to Branch2
git checkout Branch2

# commit the changes in Branch2
git add .
git commit

# get the recent commits on Branch1 from the remote server
# there is no need to merge them
git fetch origin

# rebase Branch1 on top of the remote version of Branch1
# git pull produces the same result if you didn't commit anything on Branch1
git rebase origin/Branch1 Branch1

# rebase the commit(s) you created in Branch2 on top of Branch1
git rebase origin/Branch1 Branch2

# now you are on Branch2 and Branch1 is in the past of Branch2

Good luck!

Comments