news
Serverspace Technologies in the UAE: Launch of Falconcloud
VB
October 11, 2023
Updated October 9, 2023

The base features of branches in git

Git

Git's description and advantages

Git is the famous development platform. It becomes, actually, industrial standard due to main advantage - git allows working on different versions of source code. The main idea of branching is to deviate from the main code and continue working independently of it. It is also convenient for testing specific functionality because it allows working on a new part of the code without worrying about breaking something in the production version. We will describe how to work with git branches further.

Branch is independent sequence of commits, last approved change is just a "pointer" which saves changes history as a steps, related to each other. Default branch usually called as "main" or "master".

Branch creation

Before create the new branch in console mode you should initialize git instance via this command:

mkdir /root/gitrepo && cd /root/gitrepo && git init && touch firstfile && git add firstfile && git commit -m "initial commit"

Then create the branch itself:

git branch <new_branch_name>

Branch creation - step 1

Branch has been created but you should "set" the pointer to this branch before you could work:

git checkout <created_branch_name>

Branch creation - step 2

Let's check our branch is really created:

git branch

Branch creation - step 3

Send changes to the branch

Let's make some changes in out project:

echo "First changes" > newfile.one

Then commit this changes:

git add newfile.one

git commit -m "First changes commit"

Send changes to the branch

Compare branches

To see differences between branches use this command:

git diff <one_branch>...<another_branch>

Compare branches

We will see that one file was added, this file permissions and commit message.

Branches merging

To send changes from dev branch to production we will use "merge" function. Just go to master branch, merge it with branch we created and commit our changes:

git checkout master && git merge mynewbranch && git commit -m "merges one"

Branches merging

Conflicts resolution

If two branches has a file with identical name but differ content, merge-conflict will appear. Lets simulate and solve this situation.

Next step I'll create new file into two branches, make a differ changes of this file in the branches:

echo "original content" > conflict.file  # Create new file

git add conflict.file   # Add this file to the master branch's index

git commit -m "Conflict simulation - master, step 1"    # Commit changes in the master branch

git checkout -b conflictbranch    # Create new branch and checkout into

echo "changed content" > conflict.file    # Change the file

git add conflict.file   # Add this file to the other branch's index

git commit -m "Conflict simulation - conflictbranch, change 1"    # Commit changes in the alternative branch

git checkout master    # Go back to the master branch

echo "re-changed content" > conflict.file    # Make changes again

git add conflict.file   # Add this file to the master branch's index again. You should do this everytime, othervice an error will appear

git commit -m "Conflict simulation - master, step 2"    # Commit last changes

Conflicts resolution - step 1

If we try to merge this branches - we will see an error:

Conflicts resolution - step 2

To solve the issue we should edit the file via editor. After "arrows" symbol we can see branch where changes are palced:

Conflicts resolution - step 3

After that we can make merge successfully.

Conflicts resolution - result

Branch removing

To delete branch you should use this command:

git branch -d <branch_name>

Branch removing

NOTE: You couldn't remove the branch where are you "staying" now (you should checkout other branch first). Also, you will get warn if branch has non-commited changes (may be avoided via git branch -D <branch_name>) command.

Conclusion

In this instruction we're disclosed main git advantages and showed some base branch features.

Vote:
4 out of 5
Аverage rating : 4.3
Rated by: 3
1101 CT Amsterdam The Netherlands, Herikerbergweg 292
+31 20 262-58-98
700 300
ITGLOBAL.COM NL
700 300

You might also like...

We use cookies to make your experience on the Serverspace better. By continuing to browse our website, you agree to our
Use of Cookies and Privacy Policy.