Table of Contents
Branching
Branches are just pointers to commits, this means that Git doesn't create (and shouldn't be created manually) a whole new set of files or folders.
Create a branch
The following steps are applied when it's requested to work on a separate branch.
- Create a new branch in the local repository:
git branch <name-branch>
- Switch to the new branch with:
git checkout <name-branch>
Info: from now on, new commits will affect only the new branch.
Push a branch
A new local branch, with its commits, can be push on a remote repository with the command:
git push <remote> <name-branch>
Info: in the previous command, <remote>
is origin
(if the original repository has been cloned without renaming it).
Get current branch name
To know in which branch one is working on:
git branch
Rename a branch
Two scenarios are distinguished.
If the branch already exists in the remote repository:
- rename the branch on the
<git-server>
; - update the local repository:
git branch -m <old-branch-name> <new-branch-name> git fetch origin git branch -u origin/<new-branch-name> <new-branch-name> git remote set-head origin -a
Else if the branch only exists in the local repository:
git branch -m <old-branch-name> <new-branch-name>
Merging branches
The next steps are intended when the work on a secondary branch is ready to be imported in the mainly one.
- To merge edits from
<name-branch>
intomaster
branch, switch to this one:git checkout master
- Merge the work:
git merge <name-branch>
- Push, to the remote repository, the merged commit:
git push origin master
If merge conflict happens it must be manually solved.
- Open the file that has a merge conflict
- Here changes are separated by:
<<<<<<< HEAD changes made in master ======= changes made in <branch-name> >>>>>>> <branch-name>
- Decide what changes to keep;
- Push the changes, to the remote repository, following the steps described in Push to remote repository (remember you are in
master
now).
To delete the branch:
git branch -d <name-branch>
Links
git branch -help