Git -- Version control system --


Overview:

Git is a version control system for tracking changes in computer files and coordinating work on those files among multiple people.


Git workflow

CO: Branch check out
MG: Merge / Rebase
PR: Pull request
--------------------------------------------------------------------
Git-Flow:
                                       > tag/deploy> tag/deploy
                                                   
    [ master ]─────────────────────────┼──┬─────────┼──>
                                     MGCO       MG
                            [release ]  [hotfix ]
                          CO        MG          MG
    [develop ]──┬──────────┬┴──────────┴────────────┴──>
              CO        MG
                [feature ]

--------------------------------------------------------------------
GitHub-Flow:
                           > tag/deploy > tag/deploy
                         PR           PR
    [ master ]──┬──────────┼──┬──────────┼──>
      (fork)  CO        MGCO        MG
                [feature ]  [ hotfix ]

--------------------------------------------------------------------
GitLab-Flow:
                           > tag/deploy > tag/deploy
                                        
    [product ]─────────────┼─────────────┼──>
                         MG           MG
    [ master ]──┬──────────┼──┬──────────┼──>
              CO        MGCO        MG
                [feature ]  [ hotfix ]

Initialize

$ git init [OP] [DIR]

    <DIR>
    └── .git
        ├── hooks
        ├── info
           └── exclude
        ├── objects
           ├── info
           └── pack
        ├── refs
           ├── heads
           └── tags
        ├── config
        ├── description
        └── HEAD

Quick Start

Create Repository:
  - Local:
    - $ mkdir <DIR>
    - $ cd <DIR>
    - $ git init                                  # skelton
    - $ git commit --allow-empty -m Initialized   # empty commit

    # Set remote
    - $ git remote add origin <URL>               # origin = remote url
    - $ git push -u origin master                 # set upstream

    # Add config
    - $ touch .gitignore
    - $ touch .gitattributes
    - $ git add -A                          # add all
    - $ git commit -m "[add] .gitignore"
    - $ git push

  - Clone from remote:
    - $ mkdir <DIR>
    - $ cd <DIR>
    - $ git clone <URL>                     # download

Create branch:
  - $ git branch <BRANCH>                   # create
  - $ git checkout <BRANCH>                 # set current

Create File:
  - $ vi <FILE>                             # edit
  - $ git add <FILE>                        # add to index
  - $ git commit -m <MESSAGE>               # commit

Merge:
  - $ git checkout master                   # change to master
  - $ git merge <BRANCH>                    # marge
  - $ git push                              # push to remote

Delete branch:
  - $ git branch -d <BRANCH>
  - $ git push --delete origin <BRANCH>     # remote

Pull from remote:
  - $ git pull                              # git fetch + git merge

add / commit / reset

Add:
  - $ git add <FILE>
  - $ git add -A                        # add all
  - $ git reset HEAD <FILE>             # cancel ADD

Commit:
  - $ git commit -m <MESSAGE>
  - $ git commit -am <MESSAGE>          # -am: add all & commit
  - $ git commit --amend --no-edit      # Overwrite the last commit
  - $ git commit --amend -m <MESSAGE>   # Change the last commit message

Reset:
  - $ git reset --hard HEAD             # Reset (add & data)
  - $ git reset HEAD                    # Reset (add)
  - $ git reset --hard HEAD^            # Reset (last commit & add & data)
  - $ git reset HEAD^                   # Reset (last commit & add)
  - $ git reset --soft HEAD^            # Reset (last commit)

  - $ git reset "HEAD~2"                # Reset (last 2 commit & add) for windows

  - $ git reset --hard <HASH>           # 任意の状態
  - $ git reset --hard ORIG_HEAD        # 最新の状態

merge / tag

Merge:
  - $ git merge <BRANCH>                # marge
  - $ git reset --hard HEAD^            # Reset (last marge)

Tag:
  - $ git tag <TAG> [COMMIT]
  - $ git tag -a <TAG> -m <MESSAGE> [COMMIT]

  # rename
  - $ git tag <NEWTAG> <TAG>
  - $ git tag -d <TAG>

  - $ git show <TAG>

  # check out
  - $ git checkout -b <BRANCH> refs/tags/<TAG>  # as branch
  - $ git checkout refs/tags/<TAG>              #

Stash:
  - $ git stash
  - $ git stash pop

Reference logs:
  - $ git reflog

Combine commits

  - $ git rebase -i HEAD~3
  - edit:
    pick   <HASH1> <MESSAGE1>   # no change
    squash <HASH2> <MESSAGE2>   # pick -> squash/s
    squash <HASH3> <MESSAGE3>   # pick -> squash/s


  - $ git rebase --abort