User Tools

Site Tools


software:git:branch

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.

  1. Create a new branch in the local repository:
    git branch <name-branch>
  2. 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:

  1. rename the branch on the <git-server>;
  2. 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.

  1. To merge edits from <name-branch> into master branch, switch to this one:
    git checkout master
  2. Merge the work:
    git merge <name-branch>
  3. Push, to the remote repository, the merged commit:
    git push origin master

If merge conflict happens it must be manually solved.

  1. Open the file that has a merge conflict
  2. Here changes are separated by:
    <<<<<<< HEAD
    changes made in master
    =======
    changes made in <branch-name>
    >>>>>>> <branch-name>
  3. Decide what changes to keep;
  4. 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
software/git/branch.txt · Last modified: 2023/11/22 20:09 by tormec