---
# System prepended metadata

title: Git 101
tags: [cheatsheet, git]

---

# Git 101
###### tags:`git` `cheatsheet`
## Install
* yum, apt
* Tool
    * Windows: SourceTree
    * Linux: gitk, tig
* display branch on prompt
    * add to .bashrc
    ```shell
    git_branch() {
      git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
    }

    export PS1="[\u@\h \W]\$(git_branch)\$ "

    ```
## Config
* get global config
    ```
    $ git config --list
    $ cat ~/.gitconfig
    ```
* set user info
    ```
    $ git config --global user.name "name"
    $ git config --global user.email "mail"
    ```
* set editor
    ```
    $ git config --global core.editor vim
    ```
* alias
    ```
    $ git config --global alias.co checkout
    ```

### Repository
* create
    ```
    $ mdir <proj>
    $ cd <proj>
    $ git init
    $ ll .git
    ```
### Working Dir, Staging, Repository
Working Dir (Untracked) -`add`-> Staging -`commit`-> Repository
### Baisc Operation
* add
    ```
    $ git add --all
    $ git add .
    ```
* commit
    ```
    $ git commit
    ```
* status
    ```
    $ git status
    $ git log
    $ git log --oneline
    $ git log <file>
    ```
* reset
    reset to specific commit
    ```
    $ git reset <commit>
    ```
    * mixed
    * soft
    * hard
* remove file
    ```
    $ rm test_file
    $ git add test_file
    ```
    ```
    $ git rm test_file
    ```
* untracked
    ```
    $ git rm test_file --cached
    ```
* rename
    ```
    $ git mv test_file hello
    ```
* ignore
    ```
    $ .gitignore
    $ git rm --cached
    $ git clean -fX
    ```
### Branch
* list
    ```
    $ git branch
    ```
* create
    ```
    $ git branch <name>
    ```
    * defualt: master
* rename
    ```
     $ git branch -m <name> <new_name>
    ```
* remove
    ```
    $ git branch -d <name>
    ```
    * `-D`: force
* create and checkout
    ```
    $ git checkout -b <name>
    ```
* merge
    * **merge**: merge specific branch into current one
        ```
        $ git merge <branch>
        ```
        *  fast-forward, non fast-forward
    * **rebase**: apply commits of current branch ahead of specific one
        ```
        $ git rebase <branch>
        ```
* conflict
    * merge
    * rebase
### Reference
[連猴子都能懂的Git入門指南](https://backlog.com/git-tutorial/tw/)
[為你自己學 Git](https://gitbook.tw/)