Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying an untracked folder to another branch

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.

like image 522
risto Avatar asked Dec 18 '14 02:12

risto


People also ask

How do I move untracked files to another branch?

Create a new branch called (say) "edge" Move all the changed / untracked files on master to edge (such that master is unchanged from when I started the bug fix) Finish my work on edge, merge back into master.

What happens to untracked files when switching branches?

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 copy a file from one branch to another?

Copies a file from another branch to the current branch. Use git checkout <branch> <file> to copy the specified <file> from the specified <branch> .

How to copy files from one branch to another without merging?

Sometimes we may want to copy just one or two files from dev branch without merging the whole branch with master branch. Below commands will do the same for us. (assuming you are in another branch than dev) Copy One or two files from one branch to other. git checkout dev -- path/to/your/file. Copy folder from one branch to other.

How to take a file from one branch to another in Git?

It depends on where we want to take a file from (a local branch, a commit, or a remote branch). We can check through the command git status on which branch we are. After, we will create a file and commit it to another branch. Now, we will switch again to the master branch. We will check out the file from the other branch to copy the file.

What is an untracked file in Git?

For untracked files, we can say that untracked files are the opposite of tracked files. All the other files in the Git working folder are untracked. These files are recently added and committed to Git and are ready to be staged to the Git repository, and Git does not know of it.

Will I lose untracked files when changing Git branches?

Will I lose Untracked Files when changing git branches? You have created some new files in your git repository, some have been checked in (using git add) and some are untracked. It is quite normal for developers to need to swap to another branch to do some ad-hoc work on another project.


1 Answers

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:

git add folder

Now you still cannot check out to the branch. However, you are presented with a new possibility: you can git stash save the changes:

git stash save

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 a git reset and you're done.

[Update: the git add is not necessary because git 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 on master, but with different contents:

~$ mkdir gittest
~$ cd gittest/
~/gittest$ git init
Initialized empty Git repository in /home/kaz/gittest/.git/
~/gittest$ touch emptyfile
~/gittest$ git add emptyfile
~/gittest$ git commit -m "starting repo"
[master (root-commit) 75ea7cd] starting repo
 0 files changed
 create mode 100644 emptyfile
~/gittest$ git branch
* master
~/gittest$ git checkout -b topic
Switched to a new branch 'topic'
~/gittest$ cat > topicfile
a
b
c
d
e
~/gittest$ git add topicfile
~/gittest$ git commit -m "topicfile"
[topic 875efc5] topicfile
 1 file changed, 5 insertions(+)
 create mode 100644 topicfile
~/gittest$ git checkout master
Switched to branch 'master'
~/gittest$ ls
emptyfile
~/gittest$ cat > topicfile
@
a
b
c
d
e
f
g
h
~/gittest$ git add topicfile
~/gittest$ git stash save topicfile
Saved working directory and index state On master: topicfile
HEAD is now at 75ea7cd starting repo
~/gittest$ git checkout topic
Switched to branch 'topic'
~/gittest$ git stash pop
Auto-merging topicfile
CONFLICT (add/add): Merge conflict in topicfile
~/gittest$ cat topicfile
<<<<<<< Updated upstream
=======
@
>>>>>>> Stashed changes
a
b
c
d
e
<<<<<<< Updated upstream
=======
f
g
h
>>>>>>> Stashed changes
~/gittest$ cat > topicfile  # resolve in favor of stashed changes:
@
a
b
c
d
e
f
g
h
~/gittest$ git add topicfile
~/gittest$ git reset
Unstaged changes after reset:
M       topicfile
~/gittest$ git diff
diff --git a/topicfile b/topicfile
index 9405325..bea0ebb 100644
--- a/topicfile
+++ b/topicfile
@@ -1,5 +1,9 @@
+@
 a
 b
 c
 d
 e
+f
+g
+h

At this point we can commit our topicfile changes to the topic branch, and the file is still not tracked on master.

Because there were conflicts in git stash pop, the stash still exists. We can clean that away with git stash drop.

The alternative to all this is to not only git add the untracked files to the index, but to git 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 on master, that is okay: we can later git reset --hard HEAD^ on master to eliminate that commit.

like image 169
Kaz Avatar answered Sep 26 '22 02:09

Kaz