Is there a way to copy an untracked folder to another branch?
I know you can copy tracked folders to another branch by doing something like this:
git checkout mybranch
git checkout master -- myfolder
But is there a way to copy a folder that isn't tracked on master, but is tracked on the branch I want to copy to?
I'm trying to do this for GitHub pages and I'm following this guide, but he commits to master and pushes it to upstream gh-pages. I don't want to do that. I just want my build to generate the docs and copy the untracked docs to another branch and then push them upstream.
What you have here is a situation that you have some untracked files which conflict with tracked files in another branch. You'd like to switch to that branch, but
checkout
won't let you.The "first level" solution in git is to promote these untracked files to the index:
Now you still cannot check out to the branch. However, you are presented with a new possibility: you can
git stash save
the changes:Now you can switch to the branch, and do a
git stash pop
. At this point you can deal with merge conflicts, if any, followed by agit reset
and you're done.[Update: the
git add
is not necessary becausegit stash
has an option to include untracked files!]Let's work through a complete example, involving a single file called
topicfile
which exists only in a branch, and is created as a working file while onmaster
, but with different contents:At this point we can commit our
topicfile
changes to thetopic
branch, and the file is still not tracked onmaster
.Because there were conflicts in
git stash pop
, the stash still exists. We can clean that away withgit stash drop
.The alternative to all this is to not only
git add
the untracked files to the index, but togit commit
that to make a proper commit. Then we can cherry pick the commit into the branch. If we don't want those files tracked onmaster
, that is okay: we can latergit reset --hard HEAD^
on master to eliminate that commit.