DEV Community

Zaki Arrozi Arsyad
Zaki Arrozi Arsyad

Posted on

git : basic usage

First thing first, we need to make sure if we already have git installed in our machine, by running one of these commands

  • git
  • git --verison

Cloning a repo

I prefer to use SSH instead of HTTP.
If you haven't set up the key, please read this post

$ git clone git@github.com:zakiarsyad/js-snake.git
Enter fullscreen mode Exit fullscreen mode

Check remote repository

Remote repository is versions of our project that are hosted on the Internet.

$ git remote -v
origin git@github.com:zakiarsyad/js-snake.git (fetch)
origin git@github.com:zakiarsyad/js-snake.git (push)
Enter fullscreen mode Exit fullscreen mode

By default we will work with the origin remote.

We can have more than one remote, if we need to pulling from other repositories.


Check status

$ git status
On branch master
nothing to commit, working tree clean
Enter fullscreen mode Exit fullscreen mode

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.


Create a new branch

$ git checkout -b "our-branch"
Enter fullscreen mode Exit fullscreen mode

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.


Add changes

$ git add <file>
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Or use git restore --staged . to unstage all changes.


Commit changes

$ git commit -m "put-our-message-here"
Enter fullscreen mode Exit fullscreen mode

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.


View log

$ git log
Enter fullscreen mode Exit fullscreen mode

We can easily track our changes by using this command. All changes in our branch are listed with its commit ID and message.


Push changes

$ git push origin our-branch
Enter fullscreen mode Exit fullscreen mode

We'll need to put our code in our online repository.


Create a Pull Request

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.


Merge a Pull Request

If everything is ok, then it time to update the base branch by merging our changes.

Top comments (0)