Whether you are a newcomer to programming, or an experienced one, you have to know how to use Git. Most of the projects that a small or big group of developers work on are done through GitHub or GitLab.
It makes working with other developers so much more exciting and enjoyable. Just by creating a new branch, adding all your brilliant ideas to the code that can help the project, committing it, and then pushing it to GitHub or GitLab. Then after the PR(pull request) has been opened, reviewed, and then merged, you can get back to your code and continue adding more awesome stuff. After pulling the changes from the main/master branch, of course.
If what you just read doesn't make any sense to you, don't worry. Everything will be explained in this post!
This post will show you the basics on how to start using Git and try to help you get more comfortable with it. It does look a bit scary in the beginning, but don't worry. It's not as frightening as it seems, and hopefully, after reading this post, you can get a bit more comfortable with Git.
If you are wondering what the GitHub flow is, just go back to the Introduction. We already explained it. It goes more or less like this:
- Clone the repository with the git clone command:
git clone <reposatory URL>
- Create a new branch to add your code to with the git checkout -b command:
git checkout -b <branch-name>
If your branch name has multiple words, they have to be separated with a dash (my-first-branch)
- Add the files with the
git add <branch name>
If you want to add all of the files, then write '
git add .'.
- Commit your changes and give a short description of the changes that you have made with the git commit command:
git commit -m 'descriptive message'
The '-m' is a flag for a message to set the commits with the content where the full description will be included
- Push the changes to GitHub with the git push command:
git push origin branch-name
Then open a Pull Request on GitHub and wait for your code to get reviewed and discussed.
Merge your branch to main/master branch with the git merge command:
git merge branch-name
- And finally once your changes have been merged to the main branch, make sure to pull the changes to your machine with the git pull command:
git pull origin branch-name
So this is The GitHub Flow and the main commands you will use. It may seem complicated at first, but after going through it a couple of times, you will get used to it.
Helpful Git commands
There are quite a lot of useful Git commands out there. They are super helpful, and you are probably going to want to use them all the time. Here is a list of a few of these commands:
- The git diff command shows you the file differences which are not yet saved and committed. I would recommend that you use this command as much a possible because it helps you be more organized with your branches and all the files you have changed.
- The git status command lists all of the files that have to be committed but have not yet been. I recommend using this command as it is convenient to use and makes working easier.
- The git rm command deletes the file from your working directory.
git rm branch-name
- The git log command is used to list the changes that have occurred for the current branch.
- The git branch show you all the branches in the repository that you are currently in.
Learning Git is essential for every programmer. Even some of the biggest companies use GitHub for their projects. Remember that the more you use it, the more you're going to get used to it.
I hope that this post has helped you understanding how Git works, and I hope you create some amazing things using it.