简体   繁体   English

Git:从分支推送会导致不需要的文件被提交

[英]Git:push from branch caused unwanted files to be committed

I am stuck with a weird problem which i dont seem to understand. 我陷入一个奇怪的问题,我似乎不明白。 I had two local git branches which both pointed to the same remote. 我有两个本地git分支,它们都指向同一远程。 Local branches are master and tenant and i have the following config in .git of my ws 本地分支是主节点租户 ,我在ws的.git中具有以下配置

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@egitrepo:project.git
[branch "master"]
    remote = origin
    merge = refs/heads/master
[branch "tenant"]
    remote = origin
    merge = refs/heads/master

I did this so i can do git pull easily on both the branches without specifying the remote on the command line. 我这样做是为了可以轻松地在两个分支上进行git pull ,而无需在命令行上指定remote。

I am working on the tenant branch while my master branch was not updated and old. 我在租户分支上工作,而我的分支未更新且旧。 I made changes in my tenant branch, did git pull and tried to push the changes to remote. 我在租户分支中进行了更改,进行了git pull并尝试更改送到远程。

Doing git push from the tenant branch gave me the fast-forward error. 租户分支执行git push给了我快速转发错误。

To git@egitrepo:milkyway.git
    ! [rejected]        master -> master (non-fast-forward)
  error: failed to push some refs to 'git@egitrepo:milkyway.git'

At this point, i looked around and found that this was due to git push trying to push all my local branches to the remote ie both my tenant and master (non-updated). 在这一点上,我环顾四周,发现这是由于git push试图将我的所有本地分支推送到远程,即我的租户主服务器 (未更新)。

I found a solution to this problem by setting 我通过设置找到了解决此问题的方法

 git config --global push.default upstream

which tells git to push only the current branch to its matching upsteam branch. 这告诉git只将当前分支推送到与其匹配的upsteam分支。 This solved my problem and my push from tenant branch went successful. 这解决了我的问题,而我从租户分支的推动成功了。

Now comes the problem part, when i looked at the commit history by using git diff to see my commit, i found a number of other files which SHOULD NOT have been a part of the commit. 现在出现了问题部分,当我使用git diff查看提交历史记录时,我发现了许多其他文件,这些文件不应该成为提交的一部分。 I had added only 8 files for commit and looked at git status very carefully before doing the commit and it showed only the 8 correct files under the changes to be commited section . 我只添加了8个文件进行提交,并且在进行提交之前非常仔细地查看了git status ,它在要提交的更改部分下仅显示了8个正确的文件。

How did all those other files went into my commit? 所有其他这些文件是如何进入我的提交的呢? Were they added from the master as part of the first failed git push i tried? 在我尝试的第一个失败的git push中,他们是从主服务器添加的吗? I say this because these are some of the files i changed in the master branch. 我说这是因为这些是我在master分支中更改的一些文件。 Why did git not show them to me in the correct section of git status.Can anybody explain this? 为什么git在git status的正确部分中没有向我显示它们?有人可以解释吗? Also how can i revert? 另外我该如何还原?

You can review the commit history and reflog to determine what happend. 您可以查看提交历史记录和刷新日志以确定发生了什么。

Use git-reflog and I bet you will see a merge from master to tenant. 使用git-reflog,我敢打赌,您会看到从主到租户的合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM