是否有方法将未跟踪的文件夹复制到另一个分支?
我知道您可以通过这样的操作将跟踪文件夹复制到另一个分支:
git checkout mybranch
git checkout master -- myfolder但是,是否有一种方法可以复制不在主目录上跟踪的文件夹,但在我要复制到的分支上进行跟踪?
我试图为GitHub页面做这件事,我正在跟踪这个指南,但是他承诺要掌握并将它推到上游的h页。我不想那样做。我只想让我的构建生成文档,并将未跟踪的文档复制到另一个分支,然后将它们推向上游。
发布于 2014-12-18 22:02:34
这里的情况是,有些未跟踪的文件与另一个分支中的跟踪文件发生冲突。你想转到那个分支,但checkout不让你去。
git中的“第一级”解决方案是将这些未跟踪的文件提升到索引中:
git add folder现在你还是不能去分店结账。但是,您有一种新的可能性:您可以git stash save更改:
git stash save现在您可以切换到分支,并执行git stash pop。此时,您可以处理合并冲突(如果有的话),然后是一个git reset,您就完成了。
更新:不是必要的,因为git stash有一个选项可以包含未跟踪的文件!
让我们研究一个完整的示例,其中包含一个名为topicfile的文件,该文件仅存在于一个分支中,在master上作为工作文件创建,但内容不同:
~$ 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此时,我们可以将我们的topicfile更改提交到topic分支,并且master上仍然没有跟踪该文件。
由于git stash pop中存在冲突,所以存储仍然存在。我们可以用git stash drop把它清除掉。
替代所有这些的方法不仅是将未跟踪的文件git add到索引,还可以通过git commit来进行适当的提交。然后我们就可以在树枝上挑选提交人了。如果我们不希望在master上跟踪这些文件,这是可以的:稍后我们可以在主服务器上使用git reset --hard HEAD^来消除该提交。
发布于 2014-12-18 02:34:19
如果文件夹未被跟踪,那么您可以简单地checkout另一个分支,并且未跟踪的目录不会被触及。
https://stackoverflow.com/questions/27538151
复制相似问题