如何阻止git在分支之间移动我的更改?

时间:2022-12-14 07:26:23

I am new to git, and coming from clearcase, I do not understand why the following happens:

我是git的新手,来自clearcase,我不明白为什么会发生以下情况:

Say I create a git private dev branch and do some changes to my files therein.

假设我创建了一个git private dev分支,并对其中的文件进行了一些更改。

Now, I need to do some other unrelated work and checkout the master branch.

现在,我需要做一些其他无关的工作并检查主分支。

Git automatically brings my changes over from my private branch to my local master.

Git自动将我的更改从我的私人分支转移到我的本地主人。

I find this very counter intuitive. The reason I had the private dev branch was because I didnt want my changes to go to other places till I was ready. Why is git moving them around without my saying so.

我发现这非常直观。我有私人开发分支的原因是因为我不希望我的更改去其他地方,直到我准备好了。为什么git在没有我这么说的情况下移动它们。

Yes, there is git stash, but once you have a couple of devs going in parallel or you need to switch often, its a chore to keep track of stashes. I would rather that the changes are not taken around with branch checkouts. And horrors befriend you if you forget to stash at a frenzied moment.

是的,有git stash,但是一旦你有几个dev并行或你需要经常切换,它是一个苦差事来跟踪stashes。我宁愿这些更改不会与分支机构结账一起使用。如果你忘记藏匿在疯狂的时刻,恐怖会成为你的朋友。

Am I missing some config that would avoid this?

我错过了一些可以避免这种情况的配置吗?

Thanks a much!

非常感谢!

3 个解决方案

#1


3  

The simplest thing to do would be to commit your changes to the private_dev branch, before checking out master.

最简单的方法是在检出master之前将更改提交到private_dev分支。

I would rather that the changes are not taken around with branch checkouts.

我宁愿这些更改不会与分支机构结账一起使用。

Or even having two repos:

甚至有两个回购:

  • one you never push (and in which you can commit in the private_dev branch)
  • 你永远不会推送的(并且你可以在private_dev分支中提交)
  • one in which you are pulling only the master branch (from the first local private repo), and then pushing to any remote you want.
  • 一个只拉动主分支(从第一个本地私有仓库),然后推送到任何你想要的远程。

That way, you never push by mistake your private_dev branch.

这样,你永远不会错误地推送你的private_dev分支。

#2


1  

Here's what I like to do:

这是我喜欢做的事情:

git config --global alias.temp '!git add -A && git commit -m "Temp"'
git config --global alias.pop 'reset HEAD~'

Now when you want to leave your existing work on one branch and switch to another:

现在,当您想将现有工作留在一个分支上并切换到另一个分支时:

git temp
git checkout other_branch

And when you want to switch back:

当你想要切换回来:

git checkout original_branch
git pop

#3


1  

My guess is that you don't commit the changes to your private branch, so git sees them as untracked changes to the working directory. When you check out another branch, such as master and the untracked changes do not cause any conflicts, git doesn't do anything to modify them. This is a good thing because otherwise you could overwrite work that you have been doing.

我的猜测是你没有将更改提交到你的私有分支,所以git将它们视为对工作目录的未经修改的更改。当你检查另一个分支,如master和未跟踪的更改不会导致任何冲突时,git不会做任何修改它们。这是一件好事,因为否则你可能会覆盖你一直在做的工作。

In order to avoid this, be sure to run git add and git commit before doing a git checkout to change branches. Alternatively, you can use git stash to stash your changes as a temporary commit. When you return to your private branch with a checkout, run git stash apply to retrieve your previous changes or git stash pop to retrieve them and remove them from the stash stack.

为了避免这种情况,请确保在执行git checkout更改分支之前运行git add和git commit。或者,您可以使用git stash将更改存储为临时提交。当您使用结帐返回​​私有分支时,运行git stash apply以检索先前的更改或git stash pop以检索它们并将其从存储堆栈中删除。

#1


3  

The simplest thing to do would be to commit your changes to the private_dev branch, before checking out master.

最简单的方法是在检出master之前将更改提交到private_dev分支。

I would rather that the changes are not taken around with branch checkouts.

我宁愿这些更改不会与分支机构结账一起使用。

Or even having two repos:

甚至有两个回购:

  • one you never push (and in which you can commit in the private_dev branch)
  • 你永远不会推送的(并且你可以在private_dev分支中提交)
  • one in which you are pulling only the master branch (from the first local private repo), and then pushing to any remote you want.
  • 一个只拉动主分支(从第一个本地私有仓库),然后推送到任何你想要的远程。

That way, you never push by mistake your private_dev branch.

这样,你永远不会错误地推送你的private_dev分支。

#2


1  

Here's what I like to do:

这是我喜欢做的事情:

git config --global alias.temp '!git add -A && git commit -m "Temp"'
git config --global alias.pop 'reset HEAD~'

Now when you want to leave your existing work on one branch and switch to another:

现在,当您想将现有工作留在一个分支上并切换到另一个分支时:

git temp
git checkout other_branch

And when you want to switch back:

当你想要切换回来:

git checkout original_branch
git pop

#3


1  

My guess is that you don't commit the changes to your private branch, so git sees them as untracked changes to the working directory. When you check out another branch, such as master and the untracked changes do not cause any conflicts, git doesn't do anything to modify them. This is a good thing because otherwise you could overwrite work that you have been doing.

我的猜测是你没有将更改提交到你的私有分支,所以git将它们视为对工作目录的未经修改的更改。当你检查另一个分支,如master和未跟踪的更改不会导致任何冲突时,git不会做任何修改它们。这是一件好事,因为否则你可能会覆盖你一直在做的工作。

In order to avoid this, be sure to run git add and git commit before doing a git checkout to change branches. Alternatively, you can use git stash to stash your changes as a temporary commit. When you return to your private branch with a checkout, run git stash apply to retrieve your previous changes or git stash pop to retrieve them and remove them from the stash stack.

为了避免这种情况,请确保在执行git checkout更改分支之前运行git add和git commit。或者,您可以使用git stash将更改存储为临时提交。当您使用结帐返回​​私有分支时,运行git stash apply以检索先前的更改或git stash pop以检索它们并将其从存储堆栈中删除。