Github And Git Commands For Beginners
One of the main tools that every web developer should know and use is Git.
Why it is so important and what are the most important basics that the beginner is advised to know we will see in this article.
Why is it important to know Git?
When I first started to learn about Git, to be honest, I was completely lost and I really didn’t get anything. I was confused about what button to push and how actually this can help me in my work.
In simple language, Github (and actually Gitlab) are used to save your code in the kind of cloud and also help in working in a team on the same project without “disturbing” each other.
Git Vocabulary and First Steps
Let’s see the main git commands and some words that are used in Git environment.
👉 Repository
It is actually your project. The collection of your files and components.
On GitHub you can make it either private or public and after pushing “create a new repository” you will have a clear explanation about all the further steps.
👉 Branch
Each project is consisting of different branches or may consist even with only one branch (main branch).
The first and the main branch (which can be called also master branch) is actually the project itself.
Meaning that when you would like to launch your project, in the beginning, Github will create this main branch for you.
So why do we need other branches?
Imagine you are in a team with 5 developers and all of you are working on the same project. You have different tasks but the main project is the same.
In this situation, you will have to create branches for each of the tasks to prevent any code errors or any modifications of the main project branch.
That means that you are working on the project on your local environment and before “pushing” (sending) your code to the main branch, nobody would even see what you are doing and it gives you the possibility to do modify the code on your branch however you want.
👉 Checkout
Checkout means actually moving to the other branch. For example if you only start to work on the project you would need to create a new branch and to move on it.
git checkout -b branch-name
The command above allow you to create a new branch “-b” and to to move on it directly “checkout”.
When creating a new branch be sure to do it when you are on the main branch if no other requirements were told.
git checkout main
Makes you move to any branch you want. In this case — we move back to the main branch.
👉 Commit, push, pull
After finishing your work on your separate branch you would want your lead developer to actually see what you have done and to review the code.
In order to do that you can follow the basic commands:
git add .
It will add/stage all your modified files (the “.” means all).
git commit -m "My first commit"
This command will commit your files (it will be conserved but not sent yet) and with “-m” you can add a comment about your work (recommended).
git push
Yes! Your work has been sent to the main branch and your lead developer can review it for further steps.
git pull
Pull update your branch with the main branch. For example, if your colleagues did any changes, you have to pull your branch to get those modifications on your local environment.
👉 Merge, merge requests and merge conflicts
Imagine that you have already finished your task on your branch.
What happens next?
You need to “send” (push) your branch back to the main branch in order your lead developer to review it. In this case, you do all the previous actions (add,commit,push) and you can ask for a merge request.
Merge is an action that will connect a newly created branch to the main branch and a merge request… well it is a request to do this action. 😊
That means that when a developer ask for a merge requests, the branch is ready to be “connected” with the main branch and to become a part of the project.
However, it can come that meanwhile another developer’s branch has been merged and affected your branch. In this case we can have a merge conflict.
Merge conflicts happen when one developer (the branch of who has already been merged) made some modifications at the same place than you did.
In order to proceed to the next steps you will have to solve these conflicts and to agree with the other developer about which of the modification should stay and which ones should not.
👉 Issues
When working on the project we can have some bugs or features that need to be fixed. If anyone want you or any other developer to pay attention on this problem they can create an issue on it.
An issue is created on Github or Gitlab platforms and represent a kind of task for you to do.
So when you are ready to work on this issue, you open it on the platform and create a new branch with the name of the issue.
In order to have all the newly created branches of the project (including the one connected with the issue) you can use this command:
git fetch
Some advices
If your are new to Git commands and you feel that you are really lost, don’t worry, everything will come with the experience.
If you are already on the project and you worry to do any mistake, here is the command that made me feel more confident about it.
git stash
git stash apply
The first command will remove all your modifications from the branch and the second command will apply it to the other branch.
You can use it if you started to work, for example, of the main branch and you didn’t checkout on your new branch.
And here is a quick resume of your first steps:
Hope this article was useful for you and do not hesitate to write me in comments if there is anything else that you do not understand and I didn’t mention.
Thank you 😊