Git在合并时删除.gitignore文件?

时间:2015-04-15 21:15:45

标签: git git-merge gitignore

我正在尝试遵循此workflow。 但我遇到了问题。这是交易: 我用主人跟踪一些文件开始我的回购。 我立刻进入了一个开发分支。 后来我才知道不应该跟踪那些文件(Wordpress核心文件......我正在学习)所以我将它们添加到.gitignore文件中。 现在,我已准备好发布,所以我分发了一个版本进行微调。

所以情况就是这样:Master仍在跟踪文件(它只有一次提交)。而release / 1.0不跟踪文件。

现在我正在尝试将我的发布分支合并到master中,但是当我这样做时,它会删除所有未跟踪的文件,而不是仅仅将它们留下来。

这就是我正在尝试的:

git checkout master
git merge release/1.0

3 个答案:

答案 0 :(得分:0)

您的一个提交已删除这些文件。因此,直接合并将仅应用此提交并删除文件。相反,你可以做

git checkout master
git merge --no-ff --no-commit release/1.0
git checkout HEAD <deleted-files>
git commit

这样就可以强制合并提交并在提交之前暂停 也许您还应该再次从.gitignore删除文件。

现在这些文件只会被master分支再次追踪,每次连续合并都应该可以正常工作。

答案 1 :(得分:0)

据我记得.gitignore只是指明git不应该在git status输出中提出未被跟踪或修改的被忽略文件。如果您手动添加,将照常跟踪。反之亦然:如果您删除分支中的某些文件,然后尝试将该分支合并回其原点(我认为这就是您所处的情况),那么git将在合并时删除这些文件。

以下是例子:

alex@galene ~/tmp/tmp5 $ git init
Initialized empty Git repository in /home/alex/tmp/tmp5/.git/
alex@galene ~/tmp/tmp5 $ echo "initial" >file    
alex@galene ~/tmp/tmp5 $ echo "some" >file.bad
alex@galene ~/tmp/tmp5 $ git add file*
alex@galene ~/tmp/tmp5 $ git commit -m "initial"
[master (root-commit) c400ed7] initial
  2 files changed, 2 insertions(+)
create mode 100644 file
create mode 100644 file.bad
alex@galene ~/tmp/tmp5 $ git checkout -b branch
Switched to a new branch 'branch'

更改&#34;有效负载&#34;归档并将更改提交到branch

alex@galene ~/tmp/tmp5 $ echo "c" >file
alex@galene ~/tmp/tmp5 $ git commit -m "branch commit" file
[branch 3eba064] branch commit
  1 file changed, 1 insertion(+), 1 deletion(-)
alex@galene ~/tmp/tmp5 $ echo "*.bad" > .gitignore
alex@galene ~/tmp/tmp5 $ echo "branch change" >file.bad 
alex@galene ~/tmp/tmp5 $ git status 
On branch branch
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

请注意,file.bad仍在跟踪

     modified:   file.bad

  Untracked files:
   (use "git add <file>..." to include in what will be committed)

     .gitignore

  no changes added to commit (use "git add" and/or "git commit -a")
alex@galene ~/tmp/tmp5 $ git add .gitignore 
alex@galene ~/tmp/tmp5 $ mv file.bad file.bad.copy; git rm -f file.bad; mv file.bad.copy file.bad
rm 'file.bad'
alex@galene ~/tmp/tmp5 $ git status 
On branch branch
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

       new file:   .gitignore
       deleted:    file.bad

 alex@galene ~/tmp/tmp5 $ git commit -m "remove file.bad from version control but leave in the worktree"
 [branch b4c1d42] remove file.bad from version control but leave in the worktree
   2 files changed, 1 insertion(+), 1 deletion(-)
   create mode 100644 .gitignore
   delete mode 100644 file.bad
alex@galene ~/tmp/tmp5 $ git checkout master
Switched to branch 'master'

执行master

的提交
alex@galene ~/tmp/tmp5 $ echo "master change" >file
alex@galene ~/tmp/tmp5 $ git commit -m "master change" file
[master 78142c8] master change
  1 file changed, 1 insertion(+), 1 deletion(-)
alex@galene ~/tmp/tmp5 $ git merge --no-commit branch
Removing file.bad
Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.
alex@galene ~/tmp/tmp5 $ git status 
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")

Changes to be committed:

         new file:   .gitignore
         deleted:    file.bad

 Unmerged paths:
  (use "git add <file>..." to mark resolution)

         both modified:   file

正如您在最后一步中看到的那样,您可以在file.bad中删除file和(预期)冲突。因此,您可以在合并之前存储file.bad某处(例如cp file.bad file.bad.copy,然后在合并后将其恢复)。

答案 2 :(得分:0)

这就是我所做的工作:

  1. 在master中我修改了.gitignore文件以反映release / 1.0中的.gitignore文件并提交。
  2. 使用git rm --cached <file>删除了我添加到.gitignore的跟踪中的相同文件并再次提交。
  3. 然后我成功完成了合并。
  4. 更新:在更好地理解了整个情况之后,我能够更准确地进行谷歌搜索,并在SO上找到了一个更好的问题和答案:Git: How to remove file from index without deleting files from any repository

    虽然它正在谈论不同的回购而不是我处理过的不同分支,但我相信问题是一样的。

相关问题