首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将未跟踪的文件夹复制到另一个分支

将未跟踪的文件夹复制到另一个分支
EN

Stack Overflow用户
提问于 2014-12-18 02:09:45
回答 2查看 1.7K关注 0票数 7

是否有方法将未跟踪的文件夹复制到另一个分支?

我知道您可以通过这样的操作将跟踪文件夹复制到另一个分支:

代码语言:javascript
运行
复制
git checkout mybranch
git checkout master -- myfolder

但是,是否有一种方法可以复制不在主目录上跟踪的文件夹,但在我要复制到的分支上进行跟踪?

我试图为GitHub页面做这件事,我正在跟踪这个指南,但是他承诺要掌握并将它推到上游的h页。我不想那样做。我只想让我的构建生成文档,并将未跟踪的文档复制到另一个分支,然后将它们推向上游。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-18 22:02:34

这里的情况是,有些未跟踪的文件与另一个分支中的跟踪文件发生冲突。你想转到那个分支,但checkout不让你去。

git中的“第一级”解决方案是将这些未跟踪的文件提升到索引中:

代码语言:javascript
运行
复制
git add folder

现在你还是不能去分店结账。但是,您有一种新的可能性:您可以git stash save更改:

代码语言:javascript
运行
复制
git stash save

现在您可以切换到分支,并执行git stash pop。此时,您可以处理合并冲突(如果有的话),然后是一个git reset,您就完成了。

更新:不是必要的,因为git stash有一个选项可以包含未跟踪的文件!

让我们研究一个完整的示例,其中包含一个名为topicfile的文件,该文件仅存在于一个分支中,在master上作为工作文件创建,但内容不同:

代码语言:javascript
运行
复制
~$ 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^来消除该提交。

票数 3
EN

Stack Overflow用户

发布于 2014-12-18 02:34:19

如果文件夹未被跟踪,那么您可以简单地checkout另一个分支,并且未跟踪的目录不会被触及。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27538151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档