So here is what i want to achieve:

When I clone a repository from Github and checkout some stable branch, and make some local changes in it, then I don't want to create a local feature branch nor do I want the changes to remain in the local stable branch that I have checked out.

I just want to make changes and push it to remote repo such that a feature branch is created there on remote, and my local stable branch remains clean.

How can this be done without creating a local branch and without the new changes to remain in local?


checkout the remote stable branch

git fetch <remote> <branch>

Your local repo will now have a read only copy of the remote tracking branch, now check out to the read only tracking branch

git checkout origin/<branch_name>

Your VCS (whatever you are using) will start show messages like:

  • HEAD is in a detached state
  • Previous HEAD position was c293198

And the current branch as a hash fragment like a697b40.
All detached HEAD means is that making a new commit, then moving away from it (by checking out something else) will leave you with no reference to that new commit.

checkout and create the new "feature" branch from the remote tracking branch

git checkout -b "feature_branch_intended_for_remote"

At this point any commits will be added to this totally new branch - whose history was the original stable.
This means, you got the data of the remote stable (which you could not modify) and at no point did you create a local working copy of the upstream stable - which you might accidentally push back upstream.
So now you can now

  • commit your work into this new branch
  • push it back to the origin as a completely new branch
git push [--set-upstream | --track | -u] <remote> <feature_branch_intended_for_remote>
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.