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.
$ git branch dev
Now we created a new branch called dev.
Let’s confirm that we have created a new branch
$ 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:
$ 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:
$ echo "another line" >> a.txt
$ echo "A new file" >> b.txt
Let’s see the state of the current directory:
$ 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.txtis not tracked
So we need to add both files to the Staging Environment for this branch:
$ git add a.txt b.txt
We can see the state of the directory:
$ 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:
$ 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:
$ git checkout main
Let’s try to merge the main branch to dev:
$ 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:
$ 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
$ 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:
$ 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.
$ 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-branchtomain:
$ 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:
$ 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:
$ 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:
echo "a test long" > c.txt
We can then add the file and commit it (and delete the branch):
$ git add c.txt
$ git commit -m 'merge branch'
$ git branch -d fix-branch