CheatSheet | Git


Introduction

  • Pull Request Summary

    1. Fork repo to your own repo

    2. clone, set remote

      • git remote add <remote_name> <URL>
    3. Generate branch

      • git checkout -b <feature/issue_number>
    4. Add, Commit, Push

      • git push <remote_name> <branch_name>
    5. Pull Request

    6. Code Review

    7. Merge

      • Squash and Merge : squash multiple commits into a new commit

      • Rebase and Merge : multiple commits will be merged

    8. Fetch upstream

      • Fetch upstream in local master branch

      • remove branch <feature/issue_number>



1. Setup Git Environment

# (1) create new git
$ git init # in the working dir, generates .git directory
$ git clone <URL> # cloning repo registers remote named origin automatically 
$ git clone --recursive <URL> # recursive clone including submodule
$ git clone -b <BRANCH> <URL> # without <BRANCH>, clone master branch

# (2) Add remote git 
$ git remote # show remotes for current project 
$ git remote add <remote_name> <URL> 

# (3) git user check
$ git config --list
$ git config --global --list

# (4) Add user 
$ git config user.name "Your Name" # only for this project
$ git config user.email you@example.com
$ git config --global user.name "Your Name" # for global usage
$ git config --global user.email you@example.com


2. Changes in source code : Add, Commit, Push

# (1) add
$ git add *  # add changes to index
$ git status  # Check git status (staged/unstaged)
$ git reset HEAD <FILE> # cancle add, unclearstage file. unstage all w/o <FILE 

# (2) commit
$ git commit -m "explain this commit" #  commit
$ git log  # show commit log

# (3) Change commit
$ git commit --amend -m "modify commit message"
$ git commit --amend --author="Author Name <email@address.com>" --no-edit

# (3) push
$ git push <remote_name> <branch_name> # changes from local to server

# (4) pull
$ git pull # download and merge
$ git fetch  # only download, not merge


3. Branch

  • The commited changes in BRANCH_A do not affect in BRANCH_B

  • Branch Naming Convention

    • feature : is for adding, refactoring or removing a feature

    • bugfix : is for fixing a bug

    • hotfix: is for changing code with a temporary solution and/or without following the usual process (usually because of an emergency)

    • test: is for experimenting outside of an issue/ticket

    • Git Branch Naming Convention - DEV Community

# (0) Show all branches
$ git branch

# (1) create new local branch
$ git branch <BRANCH>
$ git checkout -b <BRANCH> # Create new branch and change to it

# (2) change to the new branch
$ git checkout <BRANCH>

# (3) Delete branch
$ git branch -d <BRANCH> # (3.1) Delete local branch
$ git push origin --delete <BRANCH> # (3.2) Delete remote branch

# (4) Stash
$ git stash
$ git stash list
$ git stash pop
$ git stash apply # apply recent stash
$ git stash drop # remove recent stash


4. Pull, Merge, Rebase

  • git pull : fetch + merge

    • git pull --rebase <REMOTE> <BRANCH>
  • git merge : performs a 3-way merge between the two latest branch snapshots (C3 and C4) and the most recent common ancestor of the two (C2), creating a new snapshot (and commit).

  • git rebase : take the patch of the change that was introduced in C4 and reapply it on top of C3

  • Referenes



5. gitignore File

#  ignore every 'file.ext' in project
file.ext
# You cannot write annotation in define ignore-rule line (like below)
file.ext # not a comment

# ends with / will ignore directory only
bin/
# the line below will ignore both file named bin and directory named bin
bin

# You can use Glob pattern. e.g the line below will ignore build/ and Build
[bB]uild/
# You can ignore files with certain extension 
*.apk

# exception from ignore list with !
!.gitignore

[Reference] https://rogerdudler.github.io/git-guide/index.ko.html)