# git 指令介紹 ## Repository (數據庫) ### What is "Repository" ? The repository tracks all changes made to files in your project, building a history over time. ![](https://i.imgur.com/QYS9I7Q.jpg) -- ### The categories * Remote repository - everyone with the permission could use it * Local repository - a user use in the local computer ![](https://i.imgur.com/tpXYowA.jpg) -- ### The intro of the Working tree and index ![](https://i.imgur.com/kQryKgk.jpg) #### 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. ![](https://i.imgur.com/bnA3XL5.png) #### 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 ![](https://i.imgur.com/vtPJQyO.jpg) #### 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 ) ![](https://i.imgur.com/zIh3lDa.png) #### 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: ![](https://i.imgur.com/y1eVChO.png) After: ![](https://i.imgur.com/jVhivcC.png) #### 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 ![](https://i.imgur.com/9GysnMJ.png) ### The categories * Integration branch - the released version * Topic branch - Fix bugs / add functions ... ### Head ![](https://i.imgur.com/sa1n59e.png) #### 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 ![](https://i.imgur.com/S9C2y3i.png) #### 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 ![](https://i.imgur.com/5adcKpY.png) #### 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