git分支 - 如何使当前主分支,然后恢复主回到以前的版本? [英] git branching - how to make current master a branch and then revert master back to previous version?

查看:591
本文介绍了git分支 - 如何使当前主分支,然后恢复主回到以前的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能很简单,但我目前是git noob,并且还没有把我的头转过git分支模型。



假设我目前有除了 master 以外,没有任何分支,但现在自上次提交以来我做了一些更改,我决定不保留它们(注意:更改不是承诺尚未)。我不想摆脱这些变化 - 虽然我想把它们放在他们自己的分支(称为例如 experimental_stuff ),然后继续从我的开发先前的提交。所以我猜测的步骤是:
$ b


  • 使当前主人成为一个分支( git branch experimental_stuff
  • 返回上一个提交( git checkout< last_commit> ?)

  • 使这个新的主分支,以便将来的提交从这里继续( git ??? ?)



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

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

    解决方案

    几乎完成。



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


    git branch experimental_stuff



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


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

    lockquote
    git藏匿

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

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



    git checkout master(返回主人,不需要返回上次提交,因为您没有提交)


    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.

    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:

    • make current master a branch (git branch experimental_stuff ?)
    • go back to previous commit (git checkout <last_commit> ?)
    • make this my new master branch so that future commits continue from here (git ??? ?)

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

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

    解决方案

    you are almost done.

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

    git branch experimental_stuff

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

    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 checkout -b experimental_stuff (create and change branch to experiental_stuff)

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

    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分支 - 如何使当前主分支,然后恢复主回到以前的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆