Git basic

How to stacommit git

git init #make Local Repository .gitํด๋” ์ƒ์„ฑ(hidden folder)
git branch -M main(master)
# master ๋ฅผ main์œผ๋กœ ๋ณ€๊ฒฝ

  1. Working directory

  • ๋‚ด๊ฐ€ ์‹ค์ œ๋กœ ์ž‘์—…ํ•˜๊ณ  ์žˆ๋Š” ๋””๋ ‰ํ† ๋ฆฌ

  1. Staging Area

  • commit์œผ๋กœ ๋‚จ๊ธฐ๊ณ  ์‹ถ์€, ํŠน์ • ๋ฒ„์ „์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์‹ถ์€ ํŒŒ์ผ์ด ๋ชจ์—ฌ์žˆ๋Š” ๊ณณ

git add {file name} # add to Staging Area
        # addํ•˜์ง€ ์•Š์€ file์€ untracked  ์ƒํƒœ
        # addํ•œ file์€ tracked ์ƒํƒœ
git add {file name1} {file name2} # ์—ฌ๋Ÿฌ๊ฐœ์˜ ํŒŒ์ผ add
git add . #ํ˜„์žฌ folder๋‚ด์˜ ๋ชจ๋“  file๋“ค์„ Staging Area๋กœ add
  1. Local Repository

  • Commit๋“ค์ด ์ €์žฅ๋˜๋Š” ๊ณณ

git commit     
gir commit -m "commit message" # Staging Area to Local Repository(committed)
                               # -m์„ ์“ฐ๋ฉด ํ•œ์ค„์งœ๋ฆฌ commit message

  • Commit convention

    • ์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€๋ฅผ ์ž‘์„ฑํ•  ๋•Œ ์ผ์ •ํ•œ ๊ทœ์น™์„ ๋”ฐ๋ฅด๋Š” ๊ฒƒ

<type>(<optional scope>): <description>

[optional body]

[optional footer(s)]

ํ•„์ˆ˜ ์š”์†Œ

  1. <type>: ๋ณ€๊ฒฝ ์‚ฌํ•ญ์˜ ์ข…๋ฅ˜๋ฅผ ๋‚˜ํƒ€๋ƒ„.

    • feat: ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ ์ถ”๊ฐ€

    • fix: ๋ฒ„๊ทธ ์ˆ˜์ •

    • docs: ๋ฌธ์„œ ๋ณ€๊ฒฝ

    • style: ์ฝ”๋“œ ์Šคํƒ€์ผ ๋ณ€๊ฒฝ (๊ณต๋ฐฑ, ์„ธ๋ฏธ์ฝœ๋ก  ๋“ฑ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ๋ฌด๊ด€)

    • refactor: ์ฝ”๋“œ ๋ฆฌํŒฉํ† ๋ง (๊ธฐ๋Šฅ ๋ณ€๊ฒฝ ์—†์ด ๊ตฌ์กฐ ๊ฐœ์„ )

    • test: ํ…Œ์ŠคํŠธ ์ถ”๊ฐ€ ๋˜๋Š” ์ˆ˜์ •

    • chore: ๊ธฐํƒ€ ๋ณ€๊ฒฝ์‚ฌํ•ญ (๋นŒ๋“œ ํ”„๋กœ์„ธ์Šค๋‚˜ ํŒจํ‚ค์ง€ ์„ค์ • ๋“ฑ)

  2. <description>: ๋ณ€๊ฒฝ ๋‚ด์šฉ์„ ๊ฐ„๋žตํžˆ ์š”์•ฝ.

์„ ํƒ ์š”์†Œ

  • <scope>: ๋ณ€๊ฒฝ๋œ ํŒŒ์ผ์ด๋‚˜ ๊ธฐ๋Šฅ์˜ ๋ฒ”์œ„๋ฅผ ๋‚˜ํƒ€๋ƒ„.

  • ๋ณธ๋ฌธ: ๋ณ€๊ฒฝ ์ด์œ , ์ƒ์„ธ ์„ค๋ช… ๋“ฑ ์ถ”๊ฐ€ ์ •๋ณด๋ฅผ ์ž‘์„ฑ.

  • ํ‘ธํ„ฐ: ๊ด€๋ จ ์ด์Šˆ ๋ฒˆํ˜ธ๋‚˜ ์ฐธ๊ณ  ์ •๋ณด๋ฅผ ์ถ”๊ฐ€.


4.Remote Repository

  • GitHub๊ณผ ๊ฐ™์€ ์„œ๋ฒ„์— ์žˆ๋Š” ์ €์žฅ์†Œ

git push -u origin main(master) 
# Local Repository to Remote Repository 
# u = upstream. connect local branch and remote branch
# ์ตœ์ดˆ pushํ• ๋•Œ upstream ์„ค์ • ์ดํ›„ git push๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉ
git push

git status # view working directory and staging area status
git log #view the commit history of a Git repository
git log --oneline # view simple log
git log -n <number> # ๊ฐ€์žฅ ์ตœ๊ทผ์˜ N๊ฐœ ์ปค๋ฐ‹๋งŒ ํ‘œ์‹œ
git log <file name> # ํŠน์ • ํŒŒ์ผ์— ๋Œ€ํ•œ ๋ณ€๊ฒฝ ์ด๋ ฅ๋งŒ ํ™•์ธ
git log --graph # ์‹œ๊ฐ์ ์œผ๋กœ ๋ณด์—ฌ์คŒ

commit๋œ ํŒŒ์ผ์„ ์ˆ˜์ • ํ›„ git status๋กœ ํ™•์ธ ์‹œ modified ํŒŒ์ผ๋กœ ๋œธ


connect Local Repository with Remote Repository

commit๊ธฐ๋ฐ˜์œผ๋กœ ์—ฐ๊ฒฐ

  • Github์—์„œ create New repository

git remote add origin {New repository https}
# https์ฃผ์†Œ๋ฅผ origin์— ํ• ๋‹น(๊ด€๋ก€)

git push -u origin main(master) 

git push 

Remote Repository โ†’ Local Repository

Clone

  • Github์—์„œ Repository ์„ ํƒ

git clone {repository https}

git push -u origin main

git push

git pull

open . # current folder open 

start . # current folder open

code . # ํ•ด๋‹น ํด๋”์—์„œ vscode๋กœ open

Hiding file

.gitignore
# .gitignore์•ˆ์— file and folder ์ž‘์„ฑ
# ๋ฐ˜๋“œ์‹œ ๋ ˆํฌ์ง€ํ† ๋ฆฌ์— ๋งŒ๋“ค์–ด ๋†“๋Š”๊ฒƒ์ด ์ข‹์Œ
gitignore.io

venv/
config.py
.env
etc....
#ํ•œ๋ฒˆ commit๋˜๋ฉด .gitignore์— ๋„ฃ์„ ์ˆ˜ ์—†์Œ
git config --global user.name "user name" 
git config --global user.email "user email" 
# adding acount information to Git

Cancel commit

์ƒํ™ฉ
ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

์ปค๋ฐ‹๋งŒ ์ทจ์†Œ (ํŒŒ์ผ์€ ๊ทธ๋Œ€๋กœ)

git reset --soft HEAD~1

์ปค๋ฐ‹ + git add ์ทจ์†Œ (ํŒŒ์ผ ๋ณ€๊ฒฝ ๋‚ด์šฉ์€ ๋‚จ๊น€)

git reset HEAD~1

์ปค๋ฐ‹ + ํŒŒ์ผ ๋ณ€๊ฒฝ ๋‚ด์šฉ๊นŒ์ง€ ์‚ญ์ œ

git reset --hard HEAD~1

ํŠน์ • ํŒŒ์ผ๋งŒ ์ปค๋ฐ‹์—์„œ ์ทจ์†Œ

git reset HEAD~1 ํŒŒ์ผ์ด๋ฆ„

์›๊ฒฉ ์ €์žฅ์†Œ์—์„œ๋„ ์ทจ์†Œ (๊ฐ•์ œ ํ‘ธ์‹œ)

git push origin main --force

Last updated