Abu Sayed
Musician | Singer | Lyricist | Composer | Music Producer | Web | Game | XR & Blockchain Developer
Git and Github Tutorial: Newbie to Superior (Half 3)

Desk of Content material
π Introduction
π Git Terminologies
π Setup of Git and Github on System
π Concept of Github and Git
π Staging
π Merging Conflicts
π Resolving Conflicts
π Some Practices to avoid Conflicts
π Pull Request
π Thank you
Necessary Word βΌ
Whats up fellow readers, it is a going to be third a part of the collection Git and GitHub Tutorial: Newbie to Superior. Are you a brand new reader?
Here is the hyperlink to earlier elements of the collection –
π Part 1
π Part 2
Okay now let’s transfer ahead with the subject with out losing anymore moments.
Merging Conflicts
I do know most of you guys are conversant in merging and is even conversant in the command that’s used to merge two branches. Now’s lets dig just a little bit deeper over the subject. I can assure you, it could value it.
Whereas merging of two branches there are two kinds of state of affairs’s that occur-
- No Conflicts :
On this case the adjustments are on totally different recordsdata so Git just isn’t confused about merging them and Auto-merging is completed by Git itself π.
Output:
Merge made by the ‘recursive’ technique.
src/parts/testComponent.js | 2 +-
1 file modified, 1 insertion(+), 1 deletion(-)
- Conflicts :
On this case two developers π¨βπ» have edited the identical file with two various things. Resulting from this cause when the merge is completed it’s touchdown right into a battle as Git cannot perceive which adjustments are supposed to be saved and which of them to be discarded π.
However don’t fret ! Git is a brilliant system and it is aware of it must ask the person in such circumstances. Let’s examine the way it looks-
Output :
On terminal :
Auto-merging src/App.js
CONFLICT (content material): Merge battle in src/App.js
Computerized merge failed; repair conflicts after which commit the outcome.
Resolving Conflicts π€
Now that you’ve got confronted a battle on the codebase , let’s learn to repair the identical.
βWord: I’m utilizing VS Code whereas taking some screenshots. In case you are on a distinct code editor, it might fluctuate a bit. The editor is only a device, so don’t fret an excessive amount of if you’re on a distinct one, the ideas are vital and all different issues can be similar.
There are two methods to repair this-
- Handbook : let’s take the hostile case for an occasion that somebody is utilizing notepad for coding. I do know that is considerably humorous π ! however for that individual the editor wouldn’t make issues simpler πββοΈ.
Here is the format of the battle by way of git on codebase-
<<<<<<< Your present department
—————-current department code—————–
======
—————-merging department code—————–
>>>>>>> Merging department
Simply choose the one you need to hold and take away the others. Let’s take you need to go together with the present department code.
So, change this to –
——————–current department code————–
- Utilizing Editor : In case you are utilizing an excellent editor than your editor can be serving to you fixing such conflicts. Let’s examine a demo picture the way it seems on an editor.
The recordsdata which have conflicts are at Staged adjustments so you possibly can both listing them, or see all of the recordsdata that are Pink coloured to get an concept of these recordsdata with conflicts.
Secondly with the usage of these buttons you possibly can choose the code of which department you need to transfer ahead with and it could take away all different pointless stuffs for you on it is personal.
As you might be achieved with eradicating all such issues simply add all of the recordsdata with git add .
after which similar process of commit and push ( git commit -m "commit_message"
+ git push
)
Some Practices to keep away from Conflicts β
Dealing with too many conflicts on a codebase is usually a tiring process to deal with. Here is some good practices to keep away from such conflicts which might make your life simpler –
- Attempt to observe microservice mannequin for giant initiatives.
- Make use of some framework to cut back reductant code and for getting
controllers
,middleware
,web bundle
and many others. - Attempt to divide the work correctly so no two developers discover the necessity of engaged on similar file.
- A good and lively communication are all the time vital which helps in resolving or avoiding such conflicts very simply .
I do know this part is one thing which most of you guys will suppose as not so vital , however which you’d be working over a codebase you’ll know why we want such stuffs.
Pull Request
I do know some individuals would have heard it fairly continuously whereas working in an organization or open supply initiatives. In case you have not don’t fret ! I’m right here to introduce you to this idea π.
Let’s attempt to perceive it by way of a diagram –
Whereas working in a group π’, there want some management over what issues can go to the vital branches. Like dev
department which is generally second vital department the place all of the issues associated to subsequent launch are built-in.
Foremost
department is without doubt one of the department that group makes use of for CI CD pipeline with their hosted web site. So, something in that department will go dwell in entrance of the customers π.
So, sure just some individuals ought to be capable to push any code to those crucial branches. In any other case issues can go very dangerous.
That is the place Pull Request ( PR ) comes into play. Each developer after they need to merge their code with any of those crucial branches. They make a pull request by way of GitHub GUI and the authenticated individual will verify all of the code and whether it is appropriate, permits the merge to occur.
Let’s examine the way to create one Pull Request by way of GitHub GUI.
- Go to GitHub > Repository > Pull Request > New Pull Request
- Choose each branches ( Format: To <- From ) > Create Pull Request
- Write PR title > PR Clarification ( It is vital ) > Create Pull Request
- Bonus Step π’( Battle on Pull Request )
Are you might be reviewer ? And obtained a PR like this ? Fear not let’s cowl this problem for you in an effort to work with none worries.
To repair this use these command ( GitHub GUI won’t show you how to right here !)
Instructions :
git checkout base_branch_of_PR
git merge compare_branch_of_PR
**clear up conflicts as mentioned in merging conflicts**
git push
Superior Git Instructions β
I do know many individuals had been ready for this set of cheat sheets. So, right here we are-
π Create and Checkout Department Single Command
Now you realize all of the newbie instructions to do the identical. Why not speedup this course of with a single command ?
git checkout -b <name_of_branch>
Output:
Switched to a brand new department ‘name_of_branch’
π Command to listing all current labored branches
Now you realize all of the newbie instructions to do the identical. Why not speedup this course of with a single command ?
git for-each-ref --count=10 --sort=-committerdate refs/heads/ --format="%(refname:brief)"
Output:
testbranch1
testbranch3
principal
testbranch
feat/001
π Delete origin with no Remotes
Many occasions we’ve got a department deleted from the repository however we did not seen it and it is native reference remains to be there. Use this to delete them in a single go.
git fetch --prune
Output:
No Output
π Delete all Native and Distant branches
Are you achieved with a venture. Need to clear all branches and native code saved on it ? Use this –
git reset --hard
Output:
No Output
π Logs of Git
Need to get a view of all of the adjustments which are achieved to Git. Sure, it is potential in Git to view historical past.
git log
Faucet Enter
for extra and q
to exit.
Output:
commit ba40fd60c55b00ef828d6f5490d7b3d014a9d33 (HEAD -> testbranch3, origin/testbranch1, testbranch1)
Creator: Your_Name < your_email >
π Abstract of Logs of Git
Cannot get a good suggestion with simply the title ? Use this command to get an excellent detailed temporary over all of the logs
git log --summary
Output:
commit 8ba40fd60c55b00ef828d6f5490d7b3d014a (HEAD -> testbranch3, origin/testbranch1, testbranch1)
Creator: your_detail
Date: Thu Jun 9 16:51:35 2022 +0530commit for PR
commit dc98f62f53deee69fcc9a1088b606e49fc6d3
Creator: your_detail
Date: Thu Jun 9 11:10:01 2022 +0530
π Evaluate two branches
Are you going to create a PR . What if I let you know , you possibly can evaluate two branches on the terminal itself and get and concept what number of adjustments are there between the branches. It is cool proper ?
git diff <source_branch> <main_branch>
Output:
diff –git a/src/App.js b/src/App.js
index 57beeed..c3b970f 100644
— a/src/App.js
+++ b/src/App.js
@@ -7,7 +7,7 @@ perform App() {
<header className=”App-header”>
<img src={brand} className=”App-logo” alt=”brand” />
<p>
– Change2
+ Change12
</p>
π Delete all Native and Distant branches
Need to delete a folder or file ? Use this command to attain it ! Most helpful whereas you could execute such instructions by way of terminal of Cloud.
git rm -r [filename/foldername]
Output:
rm ‘file_location’
Thanks
You may have made it until the tip of this complete collection π€. Extra such collection are on the road .
It will be encouraging if a small remark can be there on the weblog. I’m going by way of every considered one of them so do remark π.
If you wish to get a notification π when it could be printed , remember to faucet on the observe button β.
And ultimately I need to say π
Hold coding #οΈβ£ , hold rocking π