# git 指令介紹
## Repository (數據庫)
### What is "Repository" ?
The repository tracks all changes made to files in your project,
building a history over time.

--
### The categories
* Remote repository - everyone with the permission could use it
* Local repository - a user use in the local computer

--
### The intro of the Working tree and index

#### What is "Working tree" ?
The working tree (working directory), consists of files that you are currently working on.
#### What is "index" ?
The index (staging area), is where commits are prepared.
The index compares the files in the working tree to the files in the repository.
___
## Git init and basic commands: commit, push, clone and pull
### First step of the git flow
1. create a directory
```
mkdir dir_local
// create a directory
```
2. go to the directory ( where we need to put our local repository in)
```
cd dir_local
// go to the directory
```
3. **initiate our local git repository**
```
git init
// Initialized empty Git repository in "dir_local"/.git/
```
### Commit
#### Introduction
Commit set a message about the changes you were done. The commit also saves a revision of the code and you can revert the code to any version anytime in one click.

#### The steps
1. use add before commit
```
git add "certain file"
// if we need to change the status of certain file (modify/add/delete)
git add .
// if we need to change all of the file we edited (modify/add/delete)
```
2. commit our file
```
git commit -m "<The details fo the revision>"
// we need to also send a message
```
3. check the status of the current action [optional]
```
git status
// check the status of "commit"...
```
4. check the histrory
* command:
`git log`
* output:
```
commit b6c3c771cd8939bcd25a8c50089fdf0cd3eab98d (HEAD -> master)
Author: 您的姓名 <您的 Email>
Date: 您的版本更新時間
新增網頁
```
### Push

#### Introduction
To upload local repository content to a remote repository.
#### Command
1. prepare where (what remote repository) you want to push to
```
git remote add <nickname of the remote repository> <remote url>
// remote repository with nick-name
```
2. upload (push) local repository to remote repository
```
git push origin master
// origin is the nickname of the remote repository
// master is the default branch name of the remote repository
```
### Clone
#### Introduction
To target an existing repository and create a clone, or copy of the target repository.
**Timing:** when we see this project for the first time (never download from the remote repository )

#### Command
1. select directory
`cd <dir_clone>`
2. clone
`git clone <remote copied url>`
### fetch
#### Introduction
Downloads commits, files, and refs from a remote repository into your local repository.
**Timing:** when we want to update our local repository to the latest version.
Before:

After:

#### Command
`git fetch`
### Pull
* pull = fetch + merge
#### Introduction
To fetch and download content from a remote repository and immediately update the local repository to match that content. And it will also merge the content with the local repository.
#### Command
merge(fast-forward) version:
`git pull`
merge(no fast-forward) version:
`git pull --no-ff`
rebase version:
`git pull --rebase`
* Hint: if you don't know what is merge or rebase, you could check the next topic down below
___
## Branch

### The categories
* Integration branch - the released version
* Topic branch - Fix bugs / add functions ...
### Head

#### Introduction
The pointer to the current branch reference
#### use reset with HEAD
reset : means "go to"
* ^ means previous
```
git reset HEAD^
// reset what previous HEAD point to
```
* ~ means "specify the parents of the first generation"
(指第幾代的父代)
```
git reset HEAD~1
// = git reset HEAD^
```
### Merge

#### Introduction
Lets you take the independent lines of development created by git branch and integrate them into a single branch.
#### The categories
* Fast forward:
merge one commit with a commit that can be reached by following the first commit’s history (update the HEAD pointer in such a way that its new value is a direct descendant of the prior value. )
**Timing:** when you want to simplify the "commits"
* no fast forward:
creates a new commit with multiple parents, and provides a better history tracking. Otherwise
**Timing:** when you want to have a clear notion of your feature branch.
#### Command
1. change to another branch
`git checkout another_branch`
2. merge the branch -v1 (fast forward by default)
`git merge want_to_merge_branch`
3. merge the branch -v2 (no fast forward)
`git merge want_to_merge_branch --no-ff`
### Rebase

#### Introduction
The process of moving or combining a sequence of commits to a new base commit. (its like copy the sequence of commits to the a new base commit)
#### What is base?
The base of the branch is the commit it grew out of.
#### Command
1. change to another branch
`git checkout another_branch`
2. rebase the branch
`git rebase want_to_merge_branch`
___
Reference:
https://backlog.com/git-tutorial/tw/
https://w3c.hexschool.com/git/
https://gitbook.tw/
https://blog.yorkxin.org/posts/git-rebase.html
https://www.atlassian.com/git/tutorials
---
# Gitflow
參考資料:https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow