0

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.
Community
  • 1
  • 1
Beginner
  • 2,643
  • 9
  • 33
  • 51

2 Answers2

1

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!

axiac
  • 68,258
  • 9
  • 99
  • 134
  • So I commited my files from `Branch2`. Now I switched over to `Branch1` and did `git fetch origin`. However `git log` shows the `HEAD` is still at an older commit (which no longer exists) – Beginner Oct 13 '16 at 23:08
  • `git fetch` doesn't change the current branch. In fact, it doesn't change anything regarding the local repo. I just gets from the remote repo the commits that are not present in the local repo. It also updates the information it caches about the branches of the remote repo. `git pull` does `git fetch` followed by `git merge` (or `git rebase` if the `--rebase` option is present in its command line). `git merge` and `git rebase` are the ones that change the local repo. – axiac Oct 14 '16 at 07:54
0

It seems that you are failing to commit your branch2 before switching back to branch1. You will carry all of your uncommitted data back to the previous branch. You have a couple of options: either commit first, run git checkout . to undo them, or run git stash before switching.