Git workflow for beginner

This post is for the git-beginner(perhaps a coder) who

1. Get/update a git repository from the remote
2. Implement a function/task of the project which contains only a few commits(~10)
3. Push those commits to the remote finally

There are a lot of tools for using Git, in this post we will only focus on the Command Line(or Terminal in macOS).

## Get/update a remote git repository

You must get a remote git repository by cloning it to the local environment(your PC) at the first time.(You will only need to do this once in the life time of this repository)
Run the following command in the Terminal or the command line.

`$ git clone https://github.com/suvt-gshu/dev-sample.git`

If you get the result similar as below, it means that you've copy the remote git repository to local successfully. Otherwise, check the URL you provided in the command.

```
Cloning into 'dev-sample'...
remote: Counting objects: 16, done.
remote: Compressing objects: 100% (6/6), done.
Unpacking objects: 100% (16/16), done.
remote: Total 16 (delta 1), reused 15 (delta 0), pack-reused 0
```

After you have the local repository, all you have to do before start a task is to update it by running:

`$ git pull <Repository> <Branch>`

For example, if you want to update the local master with remote ones of the origin repository: 

`$ git pull origin master`

If you get the result similar as below, it means that you've update the repository successfully.

```
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/suvt-gshu/dev-sample
   45679e5..efbb1c4  master     -> origin/master
```

## Implement a function/task

Before you start to implement a function or task, make sure:

1. You are on the right branch
2. Create a `feature` branch if you follow [git-flow](http://danielkummer.github.io/git-flow-cheatsheet/index.ja_JP.html) practice

#### Check the branch where you are on

`$ git branch`

Run the command above to see which branch you are on now:(i.e. master)

`* master`

Moreover, in case you are not on the branch you want, you can change to the branch you want(if it exists) by running:

```
$ git checkout develop
Switched to branch 'develop'
```

#### Create `feature` branch

If you follow the git-flow practice, make sure you create a `feature` branch derived from `develop` branch. Another word, check out to `develop` branch(if you are not on) then create a `feature` branch.
Pay attention that the name of the `feature` branch should comply to your team's rule, such as `feature_12345` where 12345 is the ticket number in [Redmine](http://redmine.jp), for example.

```
$ git checkout -b feature_12345
Switched to a new branch 'feature_12345'
```

#### Do the work and then commit

After you create a feature branch successfully, it is time to implement the function you want or the task you've been assigned.
After you've finished the work, a commit is necessary to be done in Git so that you can upload it to the remote, aka share it to your teammate.
A commit is made by:

```
$ git add --all
$ git commit -m "a short description of the task"
```

Then you will get a bunch of messages if your commit is successfully made:

```
[feature_12345 45e8305] a short description of the task
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 testFile2
```

Repeat the steps above if you want to do several commits in a task.

## Push to the remote

There you made some commits and now you are ready to upload it to the remote repository by:

```
$ git push --set-upstream origin feature_12345
```

If get the following result, it means you've pushed the commits to the remote successfully:

```
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 273 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/suvt-gshu/dev-sample.git
 * [new branch]      feature_12345 -> feature_12345
Branch feature_12345 set up to track remote branch feature_12345 from origin by rebasing.
```