基于服务器创建新的GIT分支,无需进行本地更改

时间:2022-03-07 23:45:51

I think I'm missing some basic idea about GIT branches.

我想我错过了一些关于GIT分支的基本想法。

Let's say I've cloned a repository on my machine, and started working (on local "master").

假设我已经在我的机器上克隆了一个存储库,并开始工作(在本地“master”上)。

Now someone have told me about a bug in production.

现在有人告诉我生产中的一个错误。

I want to stop everything I'm doing (modified filed and un-pushed commits), create a feature branch that is based on the server without the changes I made locally, fix the bug, commit and push, and then get back to master, rebase and keep working.

我想停止我正在做的一切(修改已归档和未推送的提交),创建一个基于服务器的功能分支,而不是我在本地进行的更改,修复错误,提交和推送,然后返回到主,改变并继续工作。

I thought that the the following would work:

我认为以下内容可行:

$ git clone <some repo>
$ vim text.txt
$ git checkout -b bugFix

The problem is that the branch "bugFix" includes the change I made in master in it's status.

问题是分支“bugFix”包括我在master中对其状态所做的更改。

How can I create a clean branch?

我怎样才能创建一个干净的分支?

3 个解决方案

#1


2  

Cleanest way is to commit your current work to a local feature-branch (keeping master in releasable state), then switch to master and make the bugfix there (or on a branch based on master).

最干净的方法是将您当前的工作提交到本地功能分支(保持master处于可释放状态),然后切换到master并在那里进行bugfix(或者在基于master的分支上)。

git checkout -b my-feature
git commit -m "add my feature"
git checkout master
git pull --rebase
git checkout -b bugFix
... do some work ...
git checkout my-feature

If you aren't ready to commit (locally) your work, you can use stash to stash your changes:

如果您还没准备好(本地)提交您的工作,您可以使用存储来存储您的更改:

git stash
git fetch
git checkout origin/master
git checkout -b bugFix
... do some work ...
git checkout master
git stash pop

#2


0  

First, stash what you are doing.

首先,隐藏你在做什么。

git stash

Your repository has a remote-tracking branch matching what you last got from origin; switch to that branch.

您的存储库有一个远程跟踪分支,与您最近从原点获得的分支相匹配;切换到那个分支。

git checkout remotes/origin/master

You will be in a detached-head state, since you don't want to commit directly to a remote-tracking branch. So create a bug fix branch based on the remote-tracking branch.

您将处于独立状态,因为您不希望直接提交到远程跟踪分支。因此,基于远程跟踪分支创建一个错误修复分支。

git checkout -b bugFix

Hack away

劈开

#3


0  

Assuming the remote is named 'origin', the remote branch is called 'production' and you are currently on master - you would do the following:

假设远程命名为“origin”,远程分支称为“production”,您当前正在使用master - 您将执行以下操作:

$ git stash   # move your current uncommitted stuff out of the way
$ git checkout -b bugFix remotes/origin/production
# now fix the bug, and commit (to your bugFix branch)
$ git push origin production # back to production
$ git checkout master
$ git stash pop

At this point you can delete your local bugFix branch, if you care to, with:

此时,您可以删除本地bugFix分支,如果您愿意,可以:

$ git branch -d bugFix

and you are thus back to where you started (and the company hero besides!)

因此,你回到了你开始的地方(除了公司英雄!)

#1


2  

Cleanest way is to commit your current work to a local feature-branch (keeping master in releasable state), then switch to master and make the bugfix there (or on a branch based on master).

最干净的方法是将您当前的工作提交到本地功能分支(保持master处于可释放状态),然后切换到master并在那里进行bugfix(或者在基于master的分支上)。

git checkout -b my-feature
git commit -m "add my feature"
git checkout master
git pull --rebase
git checkout -b bugFix
... do some work ...
git checkout my-feature

If you aren't ready to commit (locally) your work, you can use stash to stash your changes:

如果您还没准备好(本地)提交您的工作,您可以使用存储来存储您的更改:

git stash
git fetch
git checkout origin/master
git checkout -b bugFix
... do some work ...
git checkout master
git stash pop

#2


0  

First, stash what you are doing.

首先,隐藏你在做什么。

git stash

Your repository has a remote-tracking branch matching what you last got from origin; switch to that branch.

您的存储库有一个远程跟踪分支,与您最近从原点获得的分支相匹配;切换到那个分支。

git checkout remotes/origin/master

You will be in a detached-head state, since you don't want to commit directly to a remote-tracking branch. So create a bug fix branch based on the remote-tracking branch.

您将处于独立状态,因为您不希望直接提交到远程跟踪分支。因此,基于远程跟踪分支创建一个错误修复分支。

git checkout -b bugFix

Hack away

劈开

#3


0  

Assuming the remote is named 'origin', the remote branch is called 'production' and you are currently on master - you would do the following:

假设远程命名为“origin”,远程分支称为“production”,您当前正在使用master - 您将执行以下操作:

$ git stash   # move your current uncommitted stuff out of the way
$ git checkout -b bugFix remotes/origin/production
# now fix the bug, and commit (to your bugFix branch)
$ git push origin production # back to production
$ git checkout master
$ git stash pop

At this point you can delete your local bugFix branch, if you care to, with:

此时,您可以删除本地bugFix分支,如果您愿意,可以:

$ git branch -d bugFix

and you are thus back to where you started (and the company hero besides!)

因此,你回到了你开始的地方(除了公司英雄!)