First thing first, we need to make sure if we already have git installed in our machine, by running one of these commands
I prefer to use SSH instead of HTTP.
If you haven't set up the key, please read this post
$ git clone firstname.lastname@example.org:zakiarsyad/js-snake.git
Remote repository is versions of our project that are hosted on the Internet.
$ git remote -v origin email@example.com:zakiarsyad/js-snake.git (fetch) origin firstname.lastname@example.org:zakiarsyad/js-snake.git (push)
By default we will work with the origin remote.
We can have more than one remote, if we need to pulling from other repositories.
$ git status On branch master nothing to commit, working tree clean
This command tells us which branch we are in. If there are any changes in our code, those will be displayed here as well, as long as we don't commit the changes yet.
The main branch in git is master. Basically we don't work directly in master branch.
We might need another branch like
development or other environment to test the code before we put our code in the main branch. We also might need a new branch every time we build a new feature or fixing a bug.
$ git checkout -b "our-branch"
Run this command to create a new branch from branch we are in previously.
This command will create a branch from our origin remote repository. If we We also can branch out directly from other remote repository.
$ git add <file>
After creating changes in our code, we need to add it to be committed in git. So that we can track our changes later.
We can add the file one by one, or just run
git add . to add all changes we have made.
As long as we don't commit the changes yet, we can unstage our changes to be committed by running this command.
$ git restore --staged <file>
git restore --staged . to unstage all changes.
$ git commit -m "put-our-message-here"
Commit is like when we save our changes. Every time we save, git creates an unique ID that tells us what changes were made when and by who. We also need to put the message which is a brief description of what changes were made.
The commit ID or commit hash is a unique identifier for changes that have made. If needed, in the future we can easily revert the code to any version anytime in one click.
$ git log
We can easily track our changes by using this command. All changes in our branch are listed with its commit ID and message.
$ git push origin our-branch
We'll need to put our code in our online repository.
To combine our changes to master branch or other base branch, we need to create a Pull Request.
Open a pull request from our branch to the base. Put title and brief description about what we have done. Then ask others to review.
If everything is ok, then it time to update the base branch by merging our changes.