Today I will be sharing some of the important git commands that I used daily as a developer. I won't be covering all of the git commands in this blog because most of the time you won't be using all of them. I have to admit that I have no clue about git command when I first started the job as a developer. The only command I know is git commit. But once you start to work in a real team on a real serious project, Git command is essential. You will not be the one who works on the project so if you don't know Git then you can't work with the team. Of course that you can learn on the spot but it's good to know beforehand.
What is Git?
The short and simple way to answer this question is its version control. To emphasise this it's the tool that helps you and your team not yelling at each other why you redo what I have done. Or why you are touching my code while I'm not finished with it. Hopefully, this makes sense.
Git in a team environment
Git used to add/remove/updated the code that was in the project. If you are working alone on your project it won't be concerned that much in using Git as the only thing that you will do is add and commit. However, when you are working on a project with many developers involved. Your changes will be a concern. Your code will need to approve before you can merge(we will talk about that later). Simply say, you can't just change whatever you want to change. There is a conventional way of doing things which will depend on how your team setup. Some time your change might affect the whole project and other people will get conflict.
Step by step git command.
git add .
Git add will add whatever changes that you did on your current branch. With git add . (git add + dot) means you will add everything that you make changes on all of the files in that project. If you want to specify the file you want to add you can follow git add by the file name.
git commit -m 'this is comment'
Git commit. This command will commit whatever you add to the branch that you are on. Every time that you make a commit you will have to comment on "what you have done". This is where things get a bit tricky. In some developer teams, this part is important as when other developers come to check your branch they want to know what you have done. If some error needs to get fix then they know where they can find those changes. If your comment is not meaningful then no one would understand what you did. Not to mention it will be painful for others to figure things out. So it is a very good practice to give a descriptive action of what you did.
git push
Git push. This will push all of the changes that you commit to the remote branch. To note, when I say remote branch I mean that branch that you are working on lived on the Github site already not just on your local.
Create new branch
You will want to make sure you don't work in the main branch ever. By adding some feature or fixing bugs, you will have to create a new branch to work on. Even that changes will contain 1 letter.
To create new branch
git checkout -b this-is-my-new-branch
This command will create new branch for you and move you to that branch directly so you don't have to run git checkout
.
Update your local branch
Let's say you are working on your branch fixing some bug, you spend way longer than expected and other developers have added some features to the main branch. You might want to update your branch before continuing. To do that.
git pull origin main
When you run this command, Git will pull the main branch into your current branch that you are working on. Now you have the latest update from your team. But after you merge the main branch with your current branch, you will have to push those merges back to your branch as well.
The point here is to try to pull the update from the main branch as often as you can to avoid the conflict that might happen. Fixing conflict is not fun.
Key points
- Do not work on main branch ever. The only thing you can do on main branch is "git pull". The reason is if you make only 1 change and accidentally push that change up, no one will able to approve your code. It will get push directly to live project if that project hook to auto deploy branch. If that unintentionally changes that you made crash the site, then you won't even notice until customer come to you and said my site is down.
- Update main branch before you create new branch to work on. And occasionally pull changes from main to your current branch. Before you make a pull request to main branch don't forget to update the branch one last time.
- Add meaningful comment when you commit things, it make your life easier when you have to look back and find what changes you made that crash things.
That's all my main git command that use every day as a developer. I know there is more than this when it comes to command. I will make an update if I seem that some other command will be useful.
Thanks for reading my blog!
Always hit me up on Twitter if you want to chat!
Top comments (0)