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”).

  • Is it safe to publish an app on Heroku that has api keys on there?
  • Push local Git branch to origin
  • Git: pathspec 'path' did not match any file(s) known to git
  • Change first commit of project with Git?
  • Deploy Rails app to Heroku without using git?
  • git forward commit one by one to find the error
  • 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?

  • Where does the ssh client in git bash store it's known_hosts file on windows?
  • Elastic Beanstalk pull application files locally
  • Git pushing to the past fix
  • Rebase a merged commit
  • 1 out of 1 hunk failed when applying path from Git Repo to Perforce Repo
  • phpstorm looking for ssh key in wrong directory
  • 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.

    git stash

    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

    Hack away

    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!)

    Git Baby is a git and github fan, let's start git clone.