-
Notifications
You must be signed in to change notification settings - Fork 0
Home
git checkout master
git pull # make sure, you are branching from the latest and greatest version
git checkout -b add-foobar
git branch -m add-foobar add-barfoo
git checkout add-foobar # be on you branch
git push # and push it
If you are doing this for the first time, git will not yet know, where to put the branch. But (as often) it has a good suggestion in the reply:
fatal: The current branch add-foobar has no upstream branch.
To push the current branch and set the remote as upstream, usegit push --set-upstream origin add-foobar
so in that case, do as you are told:
git push --set-upstream origin add-foobar
! [rejected] add-foobar -> add-foobar (non-fast-forward)
error: failed to push some refs to 'git@github.molgen.mpg.de:mariux64/bee-files.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
You are welcome to force-push your own feature branch even if a pull request is open.
git push -f
If you are not happy with you commits, perhaps because someone suggested an improvement and you want to fix spelling errors, split one commit into two, remove a commit, and tidy up everything, the magic feature is git rebase -i
git checkout master
git pull # get the latest master
git checkout add-foobar # go to your branch
git rebase -i master # re-apply your changes on top of the current master. -i lets you edit them
Now you are in your editor where you can give orders, what to do with each commit. The template includes some help text.
git rebase --abort
or git merge --abort
git branch add-foobar # create the feature branch. Now add-foobar and master are equal
git reset origin/master # reset your branch "master" back
git checkout -f add-foobar # switch to your feature branch
Why the "-f" here? Typically you've added or changed some files. When we git reset origin/master
we only let our
"master" branch point to another commit. We did not change the work directory and the files you've added or modified are still there. git status
shows it.
Not if you checkout add-foobar
which contains commits for the new and changed files, the checkout would overwrite the files in you working directory which are not already committed to the master branch. (Why doesn't git detect, these have the same contents?). The "-f" says we are happy to overwrite these files.