Create a new GIT branch based on server without local changes
I think I’m missing some basic idea about GIT branches.
Let’s say I’ve cloned a repository on my machine, and started working (on local “master”).
- git equivalent for --all for all non-remote branches?
- How do I delete a Git branch both locally and remotely?
- SSH and Git Clone
- block push of trivial merge to git server
- Git push to repo's default branch
- “git push --force” from live server to central repository
Now someone have told me about a bug in production.
I want to stop everything I’m doing (modified filed and un-pushed commits), create a feature branch that is based on the server without the changes I made locally, fix the bug, commit and push, and then get back to master, rebase and keep working.
I thought that the the following would work:
$ git clone <some repo> $ vim text.txt $ git checkout -b bugFix
The problem is that the branch “bugFix” includes the change I made in master in it’s status.
How can I create a clean branch?
3 Solutions collect form web for “Create a new GIT branch based on server without local changes”
Cleanest way is to commit your current work to a local feature-branch (keeping master in releasable state), then switch to master and make the bugfix there (or on a branch based on master).
git checkout -b my-feature git commit -m "add my feature" git checkout master git pull --rebase git checkout -b bugFix ... do some work ... git checkout my-feature
If you aren’t ready to commit (locally) your work, you can use stash to stash your changes:
git stash git fetch git checkout origin/master git checkout -b bugFix ... do some work ... git checkout master git stash pop
First, stash what you are doing.
Your repository has a remote-tracking branch matching what you last got from origin; switch to that branch.
git checkout remotes/origin/master
You will be in a detached-head state, since you don’t want to commit directly to a remote-tracking branch. So create a bug fix branch based on the remote-tracking branch.
git checkout -b bugFix
Assuming the remote is named ‘origin’, the remote branch is called ‘production’ and you are currently on master – you would do the following:
$ git stash # move your current uncommitted stuff out of the way $ git checkout -b bugFix remotes/origin/production # now fix the bug, and commit (to your bugFix branch) $ git push origin production # back to production $ git checkout master $ git stash pop
At this point you can delete your local bugFix branch, if you care to, with:
$ git branch -d bugFix
and you are thus back to where you started (and the company hero besides!)