git分支 - 如何使当前主分支,然后将主恢复到以前的版本?

时间:2022-07-25 20:05:50

This is probably quite simple but I'm currently a git noob and haven't quite got my head round the git branching model yet.

这可能很简单,但我现在是一个git noob,还没有完全围绕git分支模型。

Suppose I currently have no branches other than master, but now I've made some changes since my last commit that I've decided I don't want to keep (note: the changes are not committed yet). I don't want to get rid of these changes just yet though - I'd like to put them in their own branch (called e.g. experimental_stuff) and then continue development from my previous commit. So I guess the steps are:

假设我目前除了master之外没有其他分支,但是现在我已经做了一些更改,因为我上次提交后我已经决定不想保留(注意:更改尚未提交)。我现在不想摆脱这些变化 - 我想把它们放在他们自己的分支(称为experimental_stuff)中,然后从我之前的提交继续开发。所以我猜步骤是:

  • make current master a branch (git branch experimental_stuff ?)
  • make current master a branch(git branch experimental_stuff?)
  • go back to previous commit (git checkout <last_commit> ?)
  • 回到之前的提交(git checkout ?)
  • make this my new master branch so that future commits continue from here (git ??? ?)
  • 使这个我的新主分支,以便将来提交从这里继续(git ????)

Is this the right approach and what git command do I need for the last part (if any) ?

这是正确的方法,我最后一部分需要什么git命令(如果有的话)?

[Note: this is just a local git repository for my sole use, if that make any difference.]

[注意:这只是我自己使用的本地git存储库,如果这有任何区别。]

2 个解决方案

#1


7  

you are almost done.

你差不多完成了。

Assume you have made a commit on your development files. Then..

假设您已对开发文件进行了提交。然后..

git branch experimental_stuff

git branch experimental_stuff

git reset --hard HEAD^ (go back one previous commit of your master branch to continue your development)

git reset --hard HEAD ^(返回主分支的前一次提交以继续开发)

Assume you have not made a commit on your development files. Then.. you need to save your current changes to a temporary directory

假设您尚未对开发文件进行提交。然后..您需要将当前更改保存到临时目录

git stash

git stash

git checkout -b experimental_stuff (create and change branch to experiental_stuff)

git checkout -b experimental_stuff(创建并更改分支到experiental_stuff)

git stash pop (populate the temporary directory into experimental branch)

git stash pop(将临时目录填充到实验分支中)

git checkout master (return back to master, and no need to go back the previous commit this time as you don't have that commit)

git checkout master(返回master,并且这次没有必要返回上一次提交,因为你没有提交)

#2


2  

Suppose I currently have no branches as such

假设我目前没有分支机构

Just to correct you: You always have at least one branch in Git. Even master is a branch, with the same features as any other branches. And Git doesn't handle a branch named master different than another branch. It is just a convention, that most developers name their main branch "master", but you don't need to. You could even delete your master branch and create branches like development, release etc. For your question itself: The answer of Kit Ho is good.

只是为了纠正你:你总是在Git中至少有一个分支。即使master也是一个分支,具有与任何其他分支相同的功能。并且Git不处理与另一个分支不同的名为master的分支。这只是一个惯例,大多数开发人员将其主要分支命名为“master”,但您不需要。您甚至可以删除您的主分支并创建分支,如开发,发布等。对于您自己的问题:Kit Ho的答案很好。

#1


7  

you are almost done.

你差不多完成了。

Assume you have made a commit on your development files. Then..

假设您已对开发文件进行了提交。然后..

git branch experimental_stuff

git branch experimental_stuff

git reset --hard HEAD^ (go back one previous commit of your master branch to continue your development)

git reset --hard HEAD ^(返回主分支的前一次提交以继续开发)

Assume you have not made a commit on your development files. Then.. you need to save your current changes to a temporary directory

假设您尚未对开发文件进行提交。然后..您需要将当前更改保存到临时目录

git stash

git stash

git checkout -b experimental_stuff (create and change branch to experiental_stuff)

git checkout -b experimental_stuff(创建并更改分支到experiental_stuff)

git stash pop (populate the temporary directory into experimental branch)

git stash pop(将临时目录填充到实验分支中)

git checkout master (return back to master, and no need to go back the previous commit this time as you don't have that commit)

git checkout master(返回master,并且这次没有必要返回上一次提交,因为你没有提交)

#2


2  

Suppose I currently have no branches as such

假设我目前没有分支机构

Just to correct you: You always have at least one branch in Git. Even master is a branch, with the same features as any other branches. And Git doesn't handle a branch named master different than another branch. It is just a convention, that most developers name their main branch "master", but you don't need to. You could even delete your master branch and create branches like development, release etc. For your question itself: The answer of Kit Ho is good.

只是为了纠正你:你总是在Git中至少有一个分支。即使master也是一个分支,具有与任何其他分支相同的功能。并且Git不处理与另一个分支不同的名为master的分支。这只是一个惯例,大多数开发人员将其主要分支命名为“master”,但您不需要。您甚至可以删除您的主分支并创建分支,如开发,发布等。对于您自己的问题:Kit Ho的答案很好。