简体   繁体   English

为什么在git checkout上它会自动在我的新分支中添加更改的文件

[英]why on git checkout it automatically adds changed files in my new branch

I have to checkout from one branch of the project to the other. 我必须从项目的一个分支签到另一个分支。 When I do it, it adds all the added files in the changed branch also. 当我这样做时,它还将所有添加的文件也添加到更改的分支中。 I don't want to commit those files from this branch. 我不想从该分支提交那些文件。 What should I do and how should I avoid this thing in future 我应该怎么做以及将来如何避免这种情况

A   ui/.gitignore
A   ui/README.md
A   ui/babel.config.js
A   ui/package-lock.json
A   ui/package.json
A   ui/public/favicon.ico
A   ui/public/index.html
A   ui/src/App.vue
A   ui/src/assets/logo.png
A   ui/src/components/HelloWorld.vue
A   ui/src/main.js
Already on 'added_more_notification_channels'

It doesn't. 没有。

Instead, what Git does is simpler: 相反,Git所做的更简单:

  • You already modified these files. 您已经修改了这些文件。
  • You didn't commit these files. 您没有提交这些文件。
  • Therefore, these changes exist only in your work-tree . 因此,这些更改仅存在于您的工作树中

You then asked Git: Please switch from the commit at the tip of master to the commit at the tip of added_more_notification_channels . 然后您问Git: 请从master端的提交切换到added_more_notification_channels端的added_more_notification_channels (I had to guess at one branch name, but it does not really matter much here.) (我不得不猜测一个分支的名称,但是在这里并没有多大关系。)

In some cases, Git will say: Sorry, I can't do that: if I did that I'd destroy your updated files by replacing them with copies from the commit at the tip of added_more_notification_channels . 在某些情况下,Git会说: 对不起,我不能这样做:如果这样做,我将通过用added_more_notification_channels尖端的提交副本替换更新的文件来销毁您的更新文件。

In other cases, Git will say: OK, I did that ... but IMPORTANT NOTE: I didn't destroy your updated files, I left them alone. 在其他情况下,Git会说: 好的,我做到了……但是重要的提示:我没有销毁您的更新文件,我把它们留了下来。 They now don't match the commit at the tip of added_more_notification_channels , just like they didn't match the commit at the tip of master before. 现在,它们与added_more_notification_channels尖端的提交不匹配,就像它们之前与master尖端的提交不匹配一样。 If you had meant to add and commit them to master , you can now git checkout master again to get back to where you were. 如果您打算将它们添加并提交给master ,那么现在可以再次git checkout master来回到原来的位置。 If not, you're good where you are, with your modified files still modified. 如果没有,那么您的位置就很好,而修改后的文件仍会被修改。

If you're curious about why sometimes Git says: OK, I've switched branches! 如果您想知道为什么 Git有时会说: 好的,我已经切换了分支! (I took your modified files along for the ride!) and sometimes says Whoops, sorry, I can't do that! (我随身带走了您修改过的文件!) ,有时会说, 哎呀,对不起,我不能这样做! That would destroy your modified files! 那会破坏您的修改文件! , see Checkout another branch when there are uncommitted changes on the current branch . 当当前分支上有未提交的更改时 ,请参阅签出另一个分支

(Actually, it looks like you asked Git: Please switch from added_more_notification_channels to added_more_notification_channels , which is no switch at all, and therefore always works!) (实际上,您似乎向Git询问: 请从added_more_notification_channels切换到added_more_notification_channels ,这根本没有任何切换,因此始终有效!)

If you add/modify/delete some files then you can get those files at another branch. 如果add/modify/delete某些文件,则可以在另一个分支获取这些文件。 So if you want to checkout another branch first you have to commit the changed files. 因此,如果要首先签出另一个分支,则必须提交更改的文件。 Then you can checkout another branch. 然后,您可以签出另一个分支。 And if you don't commit, then you will get those file to the checkout branch. 而且,如果您不提交,则将那些文件发送到checkout分支。 So you should follow these steps - 因此,您应该按照以下步骤操作-

  • Add the changed files to git 将更改的文件添加到git
  • Commit this changes 提交此更改
  • Push if needed (Optional) 根据需要推送(可选)
  • Checkout another branch 结帐另一个分支

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

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