Prerequisite
Git commands
Git has several commands, the most important are:
Create or update the local repository
git clone
: you can clone the repository on your local machine, by just typing the command followed by the link obtained under the button code in the main page of the repository.
git pull
: once you have cloned the repository (you have a copy on your local machine), to get the new updates you need to pull the latest version of the repository.
Update the repository with changes done in local
When you have updated the code and you want to push the updates to the repository, you need to follow these steps:
git add
: With this command, you can decide which files to add and push to the repository. If you want to add all the files, just type git add ., otherwise you need to specify which files you want to add.
git commit
: this command should be run after an addition (git add
). It basically creates the message that is used to trace the updates. The general command run is the following: git commit -m "your message"
git push
: this command push what you have added and committed on the repository.
Other useful commands
git status
: launching this command, you can see the status of the local repository with respect to the repository online. It defines also in which branch you are working on.
git fetch
: this command checks if the local repository has all the branches of the repository and also checks if the branch where you are working on is updated or you need to do a pull.
git checkout
: this command allows you to move to a different existing branch. The general usage is the following: git checkout other_branch_
Branch management
The general structure is the following:
- main (the default), contains the code you are sure it is working
- develop, contains the code you are developing
- other branches, where you develop and test a specific part of the code. The name of these branches should be meaningful.
Create a new branch
It is possible to create a new branch both on your local machine and on the repository,
The easiest way to create a new branch is through the github repository interface.
In the code tab, if you click the main button, a dropdown with all the branches is displayed. To create a new branch, you just need to type the branch name and select the starting branch. It is a good practice that the branches for development start from the develop and will be closed in develop.
Merge branches
When you are done with your work in your branch, you can merge the changes in another branch. An example is the following: you have created a branch starting from develop, called very-cool-feature and you have finished your work. You have added, committed and pushed everything related to this feature in the repository and you want to add this new feature in the develop branch. The steps to perform this action are the following:
- Checkout to the target branch (
git checkout develop
).
- Merge the branch in the current branch (
git merge very-cool-feature
) and you are done!
It could be that there are some conflicts, and you need to solve them. The editor helps you to show the difference and resolve the conflicts.