【发布时间】:2020-03-17 16:51:26
【问题描述】:
寻找导致意外行为的以下情况的“为什么” - 特别是使用 unix 命令“rm”删除我的 git 存储库中项目分支上的文件也从主分支中删除了该文件。下面我给出命令的摘要,然后给出完整的控制台。
命令摘要:
- git 初始化
- 触摸文件1.txt文件2.txt
- git add *.txt
- git commit -m "添加file1.txt和file2.txt"
- git checkout -b myBranch
- rm file1.txt
- git status => 显示已删除的 file1.txt 未暂存以进行提交
- git checkout master
- git status => 显示已删除的 file1.txt 未暂存以进行提交
- ls => 显示 file1.txt 已从目录(主目录)中删除
- git checkout myBranch
- git rm file1.txt
- git commit -m "删除 file1.txt"
- git checkout master
- ls => 显示 file1.txt 在目录中(master)
上述总结中的关注点:第 9、10 行文件在 master 中被删除,但又回到第 15 行。
控制台详情(注意,可能有额外的显示条目)
ec2-user:~/environment/TestGit $ git init
Initialized empty Git repository in /home/ec2-user/environment/TestGit/.git/
ec2-user:~/environment/TestGit (master) $ touch file1.txt file2.txt
ec2-user:~/environment/TestGit (master) $ git add *.txt
ec2-user:~/environment/TestGit (master) $ git commit -m "Add file1.txt and file2.txt"
[master (root-commit) 531ed48] Add file1.txt and file2.txt
Committer: EC2 Default User <ec2-user@ip-172-31-37-27.ec2.internal>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file1.txt
create mode 100644 file2.txt
ec2-user:~/environment/TestGit (master) $ git checkout -b myBranch
Switched to a new branch 'myBranch'
ec2-user:~/environment/TestGit (myBranch) $ ls
file1.txt file2.txt
ec2-user:~/environment/TestGit (myBranch) $ rm file1.txt
ec2-user:~/environment/TestGit (myBranch) $ ls
file2.txt
ec2-user:~/environment/TestGit (myBranch) $ git status
On branch myBranch
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: file1.txt
no changes added to commit (use "git add" and/or "git commit -a")
ec2-user:~/environment/TestGit (myBranch) $ git checkout master
D file1.txt
Switched to branch 'master'
ec2-user:~/environment/TestGit (master) $ ls
file2.txt
ec2-user:~/environment/TestGit (master) $ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: file1.txt
no changes added to commit (use "git add" and/or "git commit -a")
ec2-user:~/environment/TestGit (master) $ git checkout myBranch
D file1.txt
Switched to branch 'myBranch'
ec2-user:~/environment/TestGit (myBranch) $ git status
On branch myBranch
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: file1.txt
no changes added to commit (use "git add" and/or "git commit -a")
ec2-user:~/environment/TestGit (myBranch) $ git rm file1.txt
rm 'file1.txt'
ec2-user:~/environment/TestGit (myBranch) $ git status
On branch myBranch
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: file1.txt
ec2-user:~/environment/TestGit (myBranch) $ git commit -m "Remove file1.txt"
[myBranch 6585980] Remove file1.txt
Committer: EC2 Default User <ec2-user@ip-172-31-37-27.ec2.internal>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email you@example.com
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 file1.txt
ec2-user:~/environment/TestGit (myBranch) $ git status
On branch myBranch
nothing to commit, working tree clean
ec2-user:~/environment/TestGit (myBranch) $ ls
file2.txt
ec2-user:~/environment/TestGit (myBranch) $ git checkout master
Switched to branch 'master'
ec2-user:~/environment/TestGit (master) $ ls
file1.txt file2.txt
【问题讨论】:
-
怎么了?您描述的配方和您在控制台上显示的内容都符合预期。
-
步骤 14 恢复工作目录以匹配
master。这些文件位于master,因此结帐会创建它们。这是意料之中的。 -
第 9 行和第 10 行不“从主文件中删除文件”。他们从工作目录中删除文件。在第 13 行,您从 MyBranch 中删除了 file1。您永远不会将它们从 master 中删除。
-
但文件最初是创建并提交给主服务器(2、3、4),然后创建了 MyBranch。这些文件已从 MyBranch (6) 中删除,然后我切换回 master (8)。 (9) 和 (10) 表明该文件不再是 Master 的一部分。这可以在提供的控制台中看到
-
@SParker 您提供的输出显示完全相反。当它显示
deleted: file1.txt时,它告诉您file1.txt已从工作目录中删除,但它仍在主分支中。
标签: git