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:
Then create the branch itself:
Branch has been created but you should "set" the pointer to this branch before you could work:
Let's check our branch is really created:
Send changes to the branch
Let's make some changes in out project:
Then commit this changes:
git commit -m "First changes commit"
Compare branches
To see differences between branches use this command:
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:
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:
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.
Branch removing
To delete branch you should use this command:
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.