Christian Schlensker
Christian Schlensker

Reputation: 22488

Git switch to master and bring untracked files along

While I was working on a feature_branch I ended up fixing a bug for the whole site. I want to commit the bug fix to master, not my feature_branch. The fix involved adding some new files so when I try to checkout master it aborts and warns me that the untracked files would be overwritten.

How can I switch to master and bring untracked files with me?

Upvotes: 11

Views: 19079

Answers (3)

brianjhong
brianjhong

Reputation: 374

Easiest solution i can think of is make a local copy of your untracked/modified files. Then checkout your master and reupload and add commit from there. If there are any conflicts, git will notify you and you can merge at that point.

Upvotes: 0

Jeremy Salwen
Jeremy Salwen

Reputation: 8418

Something's not right. Normally when you switch branches the untracked files are brought along with you without any notice. That's why they're called "untracked". However, you say that the untracked files would be overwritten. This implies that you have created an untracked file in the new branch which already exists in the master branch. This means that you have to decide what you want to do: which one to keep. This isn't a matter of getting git to do something, it's a matter of you not being clear in what you want it to do.

Assuming you want to somehow resolve the conflict between the two copies of the same file, you can do

git checkout feature_branch
git stash
git checkout master
git stash pop

And then resolve the conflicts that will arise.

Upvotes: 27

Greg Hewgill
Greg Hewgill

Reputation: 993981

One approach to this problem is to stash your changes, then restore them on master.

  1. Make sure you've committed everything else that's unrelated to this bug fix
  2. Run git stash save "bug fix for master"
  3. git checkout master
  4. git stash pop
  5. Resolve any conflicts resulting from the stash pop
  6. Commit this fix to master
  7. Switch back to your feature branch

Upvotes: 4

Related Questions