简体   繁体   English

git 的新手,为什么我在开发分支上看到我的更改,即使我将它们暂存到功能分支上?

[英]New to git, why I see my changes on develop branch even if I stage them on feature branch?

I'm new to git.我是 git 的新用户。

I'm following the git-flow branching model so I have the branches master and develop and every time I need a new feature I open a new feature branch.我正在关注 git-flow 分支 model,所以我有分支 master 和 develop ,每次我需要一个新功能时,我都会打开一个新功能分支。

I did some changes to a couple of files in the feature branch and I staged them, not committed yet since the changes are not tested yet:我对功能分支中的几个文件进行了一些更改,并暂存了它们,但尚未提交,因为更改尚未经过测试:

$ git status
On branch feature/ParamConfigFaultCollection
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   IOLSE_User_Objs.h
        modified:   data.c

Now I need to work on the branch develop, so I switch to it, I use Sourcetree as git GUI and I double click on develop:现在我需要在分支开发上工作,所以我切换到它,我使用 Sourcetree 作为 git GUI,然后双击开发: 在此处输入图像描述

and I was expecting that the changes in the stashed files on feature branch would not be visible to develop, but it seems that the changes are present in develop too:我原以为功能分支上隐藏文件的更改对开发不可见,但似乎这些更改也出现在开发中:

$ git status
On branch develop
Your branch is up to date with 'origin/develop'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   IOLSE_User_Objs.h
        modified:   data.c

What am I doing wrong?我究竟做错了什么?

The changes that you have Staged aren't stashed.您暂存的更改不会被隐藏。 They are prepared for the commit.他们为提交做好了准备。 When switching between branches any uncommitted or not-stashed changes will stay in your working area and when Git can perform the switch to another branch without overwriting your changed files, it will allow you to do so.在分支之间切换时,任何未提交或未隐藏的更改都将保留在您的工作区域中,并且当 Git 可以执行切换到另一个分支而不覆盖您更改的文件时,它将允许您这样做。

If you don't want to take your changes with you, you will have to either:如果您不想随身携带更改,则必须:

  • best option: commit them to your feature branch first to clean your work area最佳选择:首先将它们提交到您的功能分支以清理您的工作区域
  • not a bad alternative, but as some drawbacks: stash them and clean your work area不错的选择,但有一些缺点:将它们藏起来并清洁您的工作区域
  • worst case: create a new temporary branch, commit your changes there and cherry-pick them back later.最坏的情况:创建一个新的临时分支,在那里提交您的更改,稍后再挑选回来。

Then switch to the other branch.然后切换到另一个分支。

Some clients will offer you to do the stash on your behalf.有些客户会请您代您保管。 I prefer not to do that, since it's easy to forget about the stashed changes when I switch back.我不想那样做,因为当我切换回来时很容易忘记隐藏的更改。

What I'd do instead of stashing, is to commit my changes on the feature branch, preferably with a very clear commit message explaining these aren't ready yet, then, when I come back to that branch I can either:我要做的不是隐藏,而是在功能分支上提交我的更改,最好有一个非常清晰的提交消息来解释这些还没有准备好,然后,当我回到那个分支时,我可以:

  • Amend the commit to add additional changes to it prior to pushing them修改提交以在推送它们之前向其添加其他更改
  • Do an interactive rebase of the changes together with additional changes to clean up the local history prior to pushing对更改进行交互式 rebase 以及其他更改,以在推送之前清理本地历史记录
  • Do a git reset --soft HEAD~1 to undo the temporary commit, but keep it's changes, then work some more on it and commit them again later in a new commit.执行git reset --soft HEAD~1以撤消临时提交,但保留其更改,然后对其进行更多处理并稍后在新提交中再次提交。

As long as you don't push your changes to the remote server, you can tweak your local history until it looks the way you want it.只要您不将更改推送到远程服务器,您就可以调整本地历史记录,直到它看起来像您想要的那样。

暂无
暂无

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

相关问题 如果我忘记推送提交并从本地开发分支创建功能分支,如何解决 GIT 冲突? - How to resolve GIT conflict if I forgot to push the commits and created a feature branch from my local develop branch? 将开发更改移动到新功能分支 - Move develop changes to a new feature branch 将更改从分支开发拉到我的功能 - Pull changes from branch develop to my feature Git:如何将分支功能重新建立到分支开发基础上减去分支MyLocalChanges - Git: How do I rebase Branch Feature back onto Branch Develop minus Branch MyLocalChanges git:我不小心将功能分支合并到master中,而不是进行合并 - git: I accidentally merged feature branch into master instead of develop 为什么在 git fetch --all 之后我在本地主机上看不到新的远程分支? - why i can't see the new remote branch on my localhost after git fetch --all? 如何将远程分支合并到我当前的分支中,但只暂存更改,以便我可以根据需要评估和取消暂存它们? - How to merge a remote branch into my current branch, but only stage the changes so I can evaluate and unstage them as I want? 如果我将推送新创建的分支,主分支是否会发生变化(请参阅详细信息)? - Will master branch have changes if I will push my new created branch(see details)? 在创建新分支后也需要从master到本地本地仓库的git pull安全吗?我也需要在该分支上查看最新更改? - Is it safe to do a git pull from master to my local repo after creating a new branch on which I need to see the latest changes too? 为什么当我重新设置我的功能分支以开发分离头的结果时? - Why when I rebase my feature branch to develop result in detached head?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM