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

Git正在删除合并时的.gitignore文件?,git,git-merge,gitignore,Git,Git Merge,Gitignore,我正试着遵循这一点。 但我有问题。交易如下: 我用master跟踪一些文件开始回购。 我立刻分支成了一个发展分支。 后来我了解到这些文件不应该被Wordpress核心文件跟踪……我正在学习,所以我将它们添加到了.gitignore文件中。 现在,我已经为发布做好了准备,所以我将分支到一个发布中进行一些微调 所以情况是这样的:Master仍然在跟踪文件,它只有一次提交。而release/1.0没有跟踪这些文件 现在我正试图将我的发布分支合并回master,但当我这样做时,它会删除我所有未跟踪的文件

我正试着遵循这一点。 但我有问题。交易如下: 我用master跟踪一些文件开始回购。 我立刻分支成了一个发展分支。 后来我了解到这些文件不应该被Wordpress核心文件跟踪……我正在学习,所以我将它们添加到了.gitignore文件中。 现在,我已经为发布做好了准备,所以我将分支到一个发布中进行一些微调

所以情况是这样的:Master仍然在跟踪文件,它只有一次提交。而release/1.0没有跟踪这些文件

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

这就是我正在尝试的:

git checkout master
git merge release/1.0

您的一次提交删除了这些文件。因此,简单的合并只会应用此提交并删除文件。相反,你可以这样做

git checkout master
git merge --no-ff --no-commit release/1.0
git checkout HEAD <deleted-files>
git commit
这样,您可以在提交之前强制合并提交并停止。 也许你也应该从.gitignore中删除这些文件

现在,这些文件只会被主分支再次跟踪,并且每次后续合并都会正常工作。

据我所知。gitignore只是指定git不应在git状态输出中提出未跟踪或修改的被忽略文件。如果您手动添加它们,它们将像往常一样被跟踪。反之亦然:如果删除分支中的一些文件,然后尝试将该分支合并回其原始位置,我认为这就是您所处的情况,那么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'
更改有效负载文件并将更改提交到分支

执行对主服务器的提交


正如您在最后一步中看到的,您将删除文件中的file.bad和预期冲突。因此,您可以在合并前将file.bad存储在某个位置,例如cp file.bad file.bad.copy,然后在合并后将其还原。

以下是我所做的工作:

在master中,我修改了.gitignore文件,以反映/1.0版和提交版中的.gitignore文件。 从跟踪中删除了我添加到.gitignore的相同文件,使用git rm-cached并再次提交。 然后我成功地合并了。 更新:在更好地了解了整个情况后,我能够更精确地在谷歌上搜索,并在这里找到了一个措辞更为恰当的问题和答案,因此:


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

我认为这些文件也应该不再在master上跟踪,-因此我认为git rm-cached-或者,不是签出,而是在这里为每个文件执行git show HEAD$path>$path。
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)
     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'
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