Python basics
  • Tutorial
  • Exercices
  • About
  1. Git
  2. Git branch
  • Introduction
    • Python
  • Fundamentals
    • 0-Basic Syntax
    • 1-Variables
    • 2-Data types
    • 3-Numbers
    • 4-Strings
    • 5-Lists
    • 6-Booleans
    • 7-Loops
    • 8-Functions
    • 9-Dictionary
  • Advanced
    • 10-Numpy
    • 11-Matplotlib
    • 12-DataFrame
  • CLI
    • Exercices
    • Solutions
  • Git
    • Git introduction
    • Git branch
    • Git exercices
  • SQL
    • SQL Exercices
    • SQL Solutions

On this page

  • Git branch
  • Git merge
  • Git conflict
  1. Git
  2. Git branch

Git branch

Git branch

Let’s modify the a.txt and we will create a new branch to work without disturbing other people that might want to work on the project too.

Command
$ git branch dev

Now we created a new branch called dev.

Let’s confirm that we have created a new branch

Command
$ git branch
dev 
*main

We can see the new branch with the name “dev”, but the * beside master specifies that we are currently on that branch.

checkout is the command used to check out a branch. Moving us from the current branch, to the one specified at the end of the command:

Command
$ git checkout branch
Switched to branch 'dev'

Now we have moved our current workspace from the master branch, to the new branch.

We can modify the file, and maybe add other files:

Command
$ echo "another line" >> a.txt
$ echo "A new file" >> b.txt

Let’s see the state of the current directory:

Command
$ git status
On branch dev
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   a.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        b.txt

no changes added to commit (use "git add" and/or "git commit -a")

So let’s go through what happens here:

  • There are changes to our a.txt, but the file is not staged for commit
  • b.txt is not tracked

So we need to add both files to the Staging Environment for this branch:

Command
$ git add a.txt b.txt

We can see the state of the directory:

Command
$ git status
On branch dev
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   a.txt
        new file:   b.txt

Let’s commit the changes:

Command
$ git commit -m 'add(new file)`

Now we have a new branch, that is different from the master branch.

Git merge

We have the emergency fix ready, and so let’s merge the main and emergency-fix branches.

First, we need to change to the main branch:

Command
$ git checkout main

Let’s try to merge the main branch to dev:

Command
$ git merge dev
Updating 6ca36b6..6521b7f
Fast-forward
 a.txt | 1 +
 b.txt | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 b.txt

We can now delete the dev branch:

Command
$ git branch -d dev
Deleted branch dev (was 6521b7f).

Git conflict

Let’s create a conflict. To do so, we will create a branch, modify a file, then go back to the main branch and modify also this same file.

  • Creation of the file and commit to main branch
Command
$ echo "a test" > c.txt
$ git add c.txt
$ git commit -m 'first version'
  • Create a new branch a commit the file to another content:
Command
$ git checkout -b fix-branch
$ echo "a test short" > c.txt
$ git add c.txt
$ git commit -m 'fix-branch version'
  • Go back to main and modify this same file.
Command
$ git checkout main 
$ echo "a test long" > c.txt
$ git add c.txt
$ git commit -m 'main last version'
  • Let’s try to merge the branch fix-branch to main:
Command
$ git merge fix-branch
Auto-merging c.txt
CONFLICT (content): Merge conflict in c.txt
Automatic merge failed; fix conflicts and then commit the result.

The merge failed, as there is conflict between the versions for c.txt. Let us check the status:

Command
$ git status
On branch main
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   c.txt

no changes added to commit (use "git add" and/or "git commit -a")

We need to fix that conflict. Open the file in our editor:

Command
$ cat c.txt
<<<<<<< HEAD
a test long
=======
a test short
>>>>>>> fix-branch

We can see the differences between the versions and edit it like we want.

Let’s say we want this version:

Command
echo "a test long" > c.txt

We can then add the file and commit it (and delete the branch):

Command
$ git add c.txt
$ git commit -m 'merge branch'
$ git branch -d fix-branch
Git introduction
Git exercices
 

Copyright 2023, Clément Bernard