git,更改本地分支会影响其他本地分支吗?

时间:2021-04-09 16:27:20

I have a master branch

我有一个主分支

now for some testing other things I made a branch A

现在为了一些测试,我做了一个分支A

I checkout branch A modify the file and when I checkout master again the changes are there as well.

我结帐分支A修改文件,当我再次结账时,更改也在那里。

On other repositories I have the right behaviour

在其他存储库中,我有正确的行为

2 个解决方案

#1


16  

Uncommitted changes will move from one branch to other. To keep them separated, you must stash those changes before moving to another branch. When you return to your branch, you can apply those changes to retrieve them.

未提交的更改将从一个分支移动到另一个分支。要使它们分开,您必须先存储这些更改,然后再转移到另一个分支。返回分支时,可以应用这些更改来检索它们。

As seen below:

如下所示:

>$ git status
On branch branch_1
Your branch is up-to-date with 'origin/branch_1'.

modified:   dir/file.rb 
>$ git stash
>$ git checkout <branch_2>

>$ git checkout <branch_1> #after finishing your tasks in branch_2 you can go back to branch_1
>$ git stash apply

Now you will get back the changes you have done earlier in branch_1

现在,您将获得之前在branch_1中所做的更改

#2


6  

I checkout branch A modify the file and when I checkout master again the changes are there as well.

我结帐分支A修改文件,当我再次结账时,更改也在那里。

The changes that are not committed do not belong to any branch. They are present only in the work tree (and in the index if they were added).

未提交的更改不属于任何分支。它们仅存在于工作树中(如果已添加,则存在于索引中)。

It's a good practice to have a clean working tree when switch branches to avoid troubles when the changes in the work tree conflict with the differences between the switched branches.

切换分支时使用干净的工作树是一种很好的做法,以避免在工作树中的更改与切换的分支之间的差异发生冲突时出现问题。

Because branch A was just created and you didn't commit anything on it and neither on master, the branch A points to the same commit as master and switching between A and master does not require changes in the work tree. This is why you can switch the branches without reaching into conflict.

因为分支A刚刚创建并且您没有在其上提交任何内容,也没有在master上提交任何内容,因此分支A指向与主服务器相同的提交,并且在A和主服务器之间切换不需要在工作树中进行更改。这就是为什么你可以切换分支而不会发生冲突。

In order to put the changes you just did in a branch (let's say the checked out branch is A) you have to add the to the index then commit them:

为了将您刚刚在分支中执行的更改(假设已检出的分支为A),您必须将其添加到索引然后提交它们:

git add .
git commit

Read more about git add and git commit.

阅读更多关于git add和git commit的信息。

#1


16  

Uncommitted changes will move from one branch to other. To keep them separated, you must stash those changes before moving to another branch. When you return to your branch, you can apply those changes to retrieve them.

未提交的更改将从一个分支移动到另一个分支。要使它们分开,您必须先存储这些更改,然后再转移到另一个分支。返回分支时,可以应用这些更改来检索它们。

As seen below:

如下所示:

>$ git status
On branch branch_1
Your branch is up-to-date with 'origin/branch_1'.

modified:   dir/file.rb 
>$ git stash
>$ git checkout <branch_2>

>$ git checkout <branch_1> #after finishing your tasks in branch_2 you can go back to branch_1
>$ git stash apply

Now you will get back the changes you have done earlier in branch_1

现在,您将获得之前在branch_1中所做的更改

#2


6  

I checkout branch A modify the file and when I checkout master again the changes are there as well.

我结帐分支A修改文件,当我再次结账时,更改也在那里。

The changes that are not committed do not belong to any branch. They are present only in the work tree (and in the index if they were added).

未提交的更改不属于任何分支。它们仅存在于工作树中(如果已添加,则存在于索引中)。

It's a good practice to have a clean working tree when switch branches to avoid troubles when the changes in the work tree conflict with the differences between the switched branches.

切换分支时使用干净的工作树是一种很好的做法,以避免在工作树中的更改与切换的分支之间的差异发生冲突时出现问题。

Because branch A was just created and you didn't commit anything on it and neither on master, the branch A points to the same commit as master and switching between A and master does not require changes in the work tree. This is why you can switch the branches without reaching into conflict.

因为分支A刚刚创建并且您没有在其上提交任何内容,也没有在master上提交任何内容,因此分支A指向与主服务器相同的提交,并且在A和主服务器之间切换不需要在工作树中进行更改。这就是为什么你可以切换分支而不会发生冲突。

In order to put the changes you just did in a branch (let's say the checked out branch is A) you have to add the to the index then commit them:

为了将您刚刚在分支中执行的更改(假设已检出的分支为A),您必须将其添加到索引然后提交它们:

git add .
git commit

Read more about git add and git commit.

阅读更多关于git add和git commit的信息。