Git branch examples ¶
Create a new branch
First create the branch locally and then push it to the remote repo
git checkout -b new_branch_name git push origin new_branch_name
Finally, add the following to ".git/config":
[branch "new_branch_name"] remote = origin merge = refs/heads/new_branch_name
or execute the following commands:
git config branch.new_branch_name.remote origin git config branch.new_branch_name.merge refs/heads/new_branch_name
Merge remote master to local branch
git fetch git checkout test
If you have multiple remotes, use:
git checkout -b test remote-name/test
Provare anche (untested):
git fetch origin git checkout --track origin/branch_name
Branch from a previous commit using git
git branch branchname <sha1-of-commit>
or
git checkout -b your_new_branch
then cleanup the master branch
git checkout master git reset --hard HEAD~x
List branches
git branch -a ... shows all local and remote branches git branch -r ... shows only remote branches
or use the following to displays plenty of information about the remote in general and how it relates to your own repository:
git remote show origin
Merge back into master branch
git checkout master git merge new_branch_name
... resolve conflicts (TODO: add more here)
Remove the unused branch
Locally:
git branch -d new_branch_name
On remote server:
git branch -a * master remotes/origin/HEAD -> origin/master remotes/origin/new_branch_name remotes/origin/master git push origin :new_branch_name To ssh://host/home/git/... - [deleted] new_branch_name git branch -a * master remotes/origin/HEAD -> origin/master remotes/origin/master
Wooo ! many thanks to http://gitready.com/beginner/2009/02/02/push-and-delete-branches.html
If that fails because the remote branch has already been deleted, update your local list of remote branches:
git remote update origin --prune
Another interesting approach (untested):
# Create the remote branch $ git push origin origin:refs/heads/some_branch # Make sure everything is up-to-date $ git fetch origin # Then you can see that the branch is created. $ git branch -r # This should show ‘origin/new_feature_name’ # Start tracking the new branch $ git checkout --track -b new_feature_name origin/some_branch # This means that when you do pulls that it will get the latest from that branch as well. # Make sure everything is up-to-date $ git pull
Thanks to http://snipplr.com/view/16957/create-and-track-a-remote-branch-in-git/
Other example with a submodule:
cd src/forum git status # Not currently on any branch. git checkout master # Previous HEAD position was 0816d91... Updated locales # Switched to branch 'master' git pull git checkout -b assiweb git push origin assiweb
Local cleanup after remote branch removal
git remote update origin --prune
Create a new branch with git and manage branches
See: