将所有更改提交到临时分支,同时保持对主分支的不提交

我想知道在以下情况下正确的方法是什么。

I am working on master branch locally and I have multiple uncommited changes that I don't want to commit to origin/master yet.

I would like to commit my current changes to some newly created origin/temporary branch (so someone else can quickly run the current stage of my workspace on his own) while keeping all the changes in local master as they are, to be able to commit them to origin/master later at some point.

I assume I have to use git stash in some order, but I am not sure...

评论
  • WOW
    WOW 回复

    这是一种可能性:

    # from current master
    git add .
    git commit -m 'WIP: temporary commit'
    git branch temporary
    git reset --mixed HEAD~1
    

    For an explanation, the above is first adding all files to the index, and making a commit on master. You ultimately don't want that commit there, but we'll come back to that later. From this point, we branch off a new branch temporary, without actually switching to that branch. Then, from master we do a mixed reset back one commit. This will leave your index empty, with all your original changes in the working directory. Note that the temporary commit is now not part of the master branch, but is the HEAD of the new temporary branch.