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".
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 has been created but you should "set" the pointer to this branch before you could work:
git checkout <created_branch_name>
Let's check our branch is really created:
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"
To see differences between branches use this command:
git diff <one_branch>...<another_branch>
We will see that one file was added, this file permissions and commit message.
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"
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
If we try to merge this branches - we will see an error:
To solve the issue we should edit the file via editor. After "arrows" symbol we can see branch where changes are palced:
After that we can make merge successfully.
To delete branch you should use this command:
git branch -d <branch_name>
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.
In this instruction we're disclosed main git advantages and showed some base branch features.