Reinstalling the problem
Here you start work with master , but we can choose any start (local) branch B that has some upstream U installation. (In your particular case, the origin/master upstream.) You started with a task that you think , was quick and easy, and now you realized that it is not so fast and easy in the end, so you would like to "twist" turn it off "in your thread.
Thus, we can notice that we want to save the name of the current branch, and then change Git the concept of “current branch” to a newly created branch, indicating the current commit, and then adjust the saved branch to point to the saved branch upstream.
Background
The names of the branches are just labels indicating some commit. Git is reasonable enough here: git branch allows you to create a new label that points to any existing commit, or move anything other than the current branch to any existing commit. By default, a new branch is created that points to the current commit.
The tricky part is that the current branch cannot be moved this way, and the reason for this is that the current branch must match the index and the work tree, except for any active changes that you are currently making. There is only one user ("china") command to change the Git concept of the "current branch", which is git checkout .
Fortunately, git checkout also has the -b flag (create new branch), which works the same as git branch : by default, it creates a new branch at the current commit. In addition, he avoids touching the index and the tree as much as possible - like any other git checkout - so if we create a new branch with the current commit, she will never have to touch the index or the work tree at all. As a result, he always succeeds and leaves us on a newly created branch. (He can create a new branch that points to a specific commit, but it can also fail, but we don’t need this function here, so we can use a mode in which it does not fail. Fail "until the new name branches really is new, at least.)
Therefore, the solution
The solution still should be a few lines of script, but we can write this as a small script called git-spinoff and put it in our $ PATH (I would use $HOME/scripts/git-spinoff for this). We could do this as a function of a shell alias, but I think scripts are better at all (easier to understand, debug, etc.).
To make this script reliable, let's actually check our prerequisite: we are on some branch (therefore HEAD not "disconnected") and that this branch has an upstream set. Then we can create our new branch and use the git branch -f redirect points, i.e. Without using git reset , another branch:
#! /bin/sh
This last team can really be improved, as there is a “plumbing team” that does what we want. To use it, we must save the full ( refs/heads/ style) name of the source branch and select the message. This particular post can be improved, therefore, this is just a sample:
fullbranch=$(git symbolic-ref -q HEAD) || die ... branch=${fullbranch#refs/heads/} ... same as before ... git update-ref -m \ "git spinoff: re-point $branch to ${branch}@{upstream}" \ $fullbranch $upstream
With this git-spinoff script in our way, we can now run git spinoff .
Edit: this is now verified and included in https://github.com/chris3torek/scripts (as https://github.com/chris3torek/scripts/blob/master/git-spinoff ).