0

I have checked out a Git branch: git checkout branch_x. Than I made a branch from a branch: git checkout -b branch_y branch_x. I then work in new branch_y, make some changes, etc, than I want to pull changes that happened in original branch_x just to see what's going on. So while in branch_y I do:

git pull origin branch_x

But his fails with a message:

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

I expected that git pull origin branch_x would have absolutely no effect on my own new branch_y.

Can I avoid this automatic merge and pull the changes only into branch_x?

Danijel
  • 8,198
  • 18
  • 69
  • 133
  • `git pull` means *run `git fetch`, then run a second Git command.* The second command defaults to `git merge`. If you don't want to run `git merge`, don't run `git pull` at all: just run only the *first* command. Run it yourself: `git fetch`. This will update your `origin/branch_x` without touching your own `branch_x`. To make `git fetch` touch your own `branch_x`, see [fham_526's answer](https://stackoverflow.com/a/62083545/1256452). – torek May 29 '20 at 12:19
  • Note that if you don't plan to make any of your own *commits*, you do not need your own `branch_x` either. Just use `origin/branch_x` everywhere you would have used `branch_x`. Since you won't *have* a `branch_x` you won't feel any need to *update* it either. – torek May 29 '20 at 12:21

1 Answers1

1

You can bring changes from origin/branch_x into branch_x without checkout using:

git fetch origin branch_x:branch_x

as in this answer.

Danijel
  • 8,198
  • 18
  • 69
  • 133
fham_526
  • 121
  • 8
  • 1
    This fetched the changes but I don’t think it merges them. I would expect that a checkout and merge would to still need to happen. – evolutionxbox May 29 '20 at 11:13
  • 1
    that command will update your local `branch_x` to whatever is there in `origin/branch_x`, i.e. it will merge remote changes onto your local branch (but only if it's a fast-forward, otherwise you can force a non-fast-forward merge with `git fetch origin +branch_x:branch_x`). This works only if your current branch is anything other than `branch_x` – user2340612 May 29 '20 at 11:53
  • @evolutionxbox It doesn't merge, it fast-forwards. If it cannot fast-forward (there're not yet pushed commits in the branch locally) it fails loudly so the user knows she should merge or rebase. – phd May 29 '20 at 14:55