Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git switch to master and bring untracked files along

Tags:

git

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?

like image 346
Christian Schlensker Avatar asked Feb 19 '11 21:02

Christian Schlensker


People also ask

Can I switch branch with untracked files?

Take away: changing branches does not touch/change/remove untracked or checked in files. Remember that the working directory and index are not 'cleared' before the branch content is loaded into it!

How do I keep my git files untracked?

Stash Untracked Files Using “git add” You have to add the untracked files of the repository by using the “git add” command and run the “git stash” command to save the untracked file and clean the current directory for working by removing the untracked file from the repository folder.

Does git pull affect untracked files?

@AayushNeupane git is not going to do anything with untracked files, they are untracked means git is not tracking them, so no git command can do anything to the untracked files.

How do I stash tracked and untracked files?

In order to stash untracked files, add the “–include-untracked” option to your “git stash” initial command. Alternatively, you can simply use the “-u” which is equivalent to the untracked longer version.


2 Answers

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.

like image 165
Jeremy Salwen Avatar answered Oct 20 '22 16:10

Jeremy Salwen


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
like image 44
Greg Hewgill Avatar answered Oct 20 '22 16:10

Greg Hewgill