git : basic usage
Tags: git, cli
Published: 2020-06-22 17:23:33

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](https://dev.to/zakiarsyad/git-getting-started-with-git-31m6)

```bash
$ git clone git@github.com:zakiarsyad/js-snake.git
```

---

# Check remote repository

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

```bash
$ git remote -v
origin git@github.com:zakiarsyad/js-snake.git (fetch)
origin git@github.com: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.

---

# Check status

```bash
$ 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. 

---

# Create a new branch

```bash
$ 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.

---

# Add changes

```bash
$ 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. 

```bash
$ git restore --staged <file>
```

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

 

---

# Commit changes

```bash
$ 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.

---

# View log

```bash
$ 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.

---

# Push changes

```bash
$ git push origin our-branch
```

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.