Git Commands

Git Commands

STARTING

git --version // check git version

// setup your email & name
git config --global user.email "id@email.com"
git config --global user.name "name"

git init // start using git

RECORDS

git log // view commit records (q: exit, j/k: scroll up/down)
git status
git diff // show difference of previous code & current code (q: exit, j/k: scroll up/down)

COMMIT

git add fileName // staging (put files in staging area)
git add fileName1 fileName2 //staging multiple files
git add . // staging entire file
git commit -m 'message' // add files to repository w/ commit message

git restore --staged fileName // cancel staged file
git restore fileName // Change current code to most recently committed code
git restore --source commitId fileName // Change current code to certain commitId time
git restore . //cancel all staged file

git revert commitId // cancel code changes done in commitId

git reset --hard commitId // reset to commit 2 (NOT RECOMMENDED)
git reset --soft commitId // commitId stays in staging area (now can commit)
git reset --mixed commitId // commitId is not staged (now can git add & commit)

BRANCH

git branch branchName // create branch
git switch branchName // switch from main to branchName
git switch main // switch to main branch (could be master, depending on your git setting)
git status // check which branch you are in
git log // check branch & commit records

- Delete branch

git branch -d branchName // deleting merged branch
git branch -D branchName // deleting not-merged branch

MERGE

  • Merging branch with main
  • If there's a conflict, change file -> git add -> git commit

    git switch main // first, switch to main branch
    git merge branchName
    git log // check if merge is successful
    
  • Type 1) 3-way merge

    • When there's more than 1 commit in main & new branch, 3-way merge is a standard merge type

    merge1-600x393.png

  • Type 2) Fast-forward merge

    • When there's new commit only in new branch

    그림3-4-768x362.png

    git merge --no--ff
    
  • Type3) Rebase and merge

    • Rebase : change new branch's starting point to main branch's recent commit

    merge3-768x200.png

      git switch newBranch
      git rebase main
    
      git switch main
      git merge newBranch
    
  • Type4) Squash and merge
    • Similar to 3-way merge. But only result is moved to main branch 그림2-768x463.png
      git switch main
      git merge --squash branchName
      git commit -m 'message'
      

Reference : codingapple.com