Git Tips & Tricks - yunkon-kim/test-on-github-actions GitHub Wiki
Git์ ๋ฌ์ธ์ด ๋์
- ์์ ์ด ์์ผ๋ก ํ์ตํ ๋ด์ฉ์ ๊ธฐ๋กํ
TILํด๋๋ฅผ ํ๋ ์์ฑํ๋ค. ์ด๋ ํด๋น ํด๋๋ ์ต์๋จ์ ์์ฑํ๋ค -
git bash์์TILํด๋๋ก ์ด๋ํ ์ดํ์ ์๋์ ๋ช ๋ น์ด๋กgit๊ด๋ฆฌ๋ฅผ ์์ํ๋ค
$ git init- ํ์ฌ ์ฝ๋ ์ํ์ ์ค๋ ์ท์ ์ฐ๊ธฐ ์ํ ํ์ผ ์ ํ (== Staging Area์ ํ์ผ ์ถ๊ฐ)
$ git add [ํ์ผ์ด๋ฆ] # .์ ๋ชจ๋ ๋ณ๊ฒฝ ์ฌํญ์ staging area๋ก ์ฌ๋ฆผ- ํ์ฌ ์ํ
์ ๋ํ ์ค๋
์ท์
commitํ์ฌ, ๋ฒ์ ๊ด๋ฆฌ๋ฅผ ์งํํ๋ค.
$ git commit -m "commit message"- Github ์๊ฒฉ(remote) ์ ์ฅ์(repository)๋ฅผ ์์ฑํ๊ณ
TILํด๋์ ์ฐ๊ฒฐํ๋ค - ์๋ก์ด ์๊ฒฉ ์ ์ฅ์๊ฐ ์ถ๊ฐ๋ ๋๋ง ์ ๋ ฅํ๋ค.
$ git remote add origin [github ์๊ฒฉ ์ ์ฅ์ ์ฃผ์]- Github ์๊ฒฉ ์ ์ฅ์์ ์์ ์ฝ๋๋ฅผ ๋ถ๋ฌ์ค๊ณ ๊ฐ
branch์ ํด๋น๋๋commit๋ค์ ์ ์ฉํ๋ค.
$ git pull
$ git pull [์๊ฒฉ ์ ์ฅ์ ์ด๋ฆ] (specify a remote to pull from)- ์ต์ข ์ ์ผ๋ก Github ์๊ฒฉ ์ ์ฅ์์ push ํ๋ค.
$ git push
$ git push origin master: ๋ง์ฝ ํ์ฌ branch๊ฐ push ํ๋ ค๋ repo์ branch ์ด๋ฉด $ git push ๋ช
๋ น์ด๋ง ์
๋ ฅํด๋ ๋๋ค!
- ํ์ฌ
git์ ์ํ๋ฅผ ์กฐํgit status
$ git status- ๋ฒ์ ๊ด๋ฆฌ ์ด๋ ฅ์ ์กฐํ
$ git log-
git์ค์ (user.name & user.email) : ์ต์ด 1ํ ์ค์
$ git config --global user.name "Chloe Kim"
$ git config --global user.email "[email protected]"- Undo
git addbefore commit
$ git reset์๊ฒฉ(remote) ์ ์ฅ์(repository)์ ๋ํ ์ ๋ณด๋ฅผ ๊ธฐ๋กํ๋ ๋งํฌ๋ค์ด ๋ฌธ์. ์ผ๋ฐ์ ์ผ๋ก ํด๋น ํ๋ก์ ํธ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํ ๋ฐฉ๋ฒ ๋ฑ์ ๊ธฐ์ฌํ๋ค.
-
README.mdํ์ผ์TILํด๋ (์ต์๋จ)์ ์์ฑํ๋ค. ์ด๋ฆ์ ๋ฐ๋์ README.md๋ก ์ค์ ํ๋ค.
$ touch README.md- ๋งํฌ๋ค์ด ์์ฑ๋ฒ pdf์์ ๋ฐฐ์ฐ๊ณ ์ค์ตํ ๋ด์ฉ์ ํ ๋๋ก
README.mdํ์ผ์ ์์ฑํ๋ค. - ํ์์ ์์ ๋กญ๊ฒ ์์ฑํ๋ ๋งํฌ๋ค์ด ๋ฌธ๋ฒ(์๋ฏธ๋ก ์ )์ ์ง์ผ์ ์์ฑํ๋ค.
- ์์ฑ์ด ์๋ฃ๋๋ฉด ์๋์ ๋ช ๋ น์ด๋ฅผ ํตํด commit ์ด๋ ฅ์ ๋จ๊ธฐ๊ณ ์๊ฒฉ ์ ์ฅ์๋ก push ํ๋ค.
$ git add README.md
$ git commit -m "add README.md"
$ git push origin masterFast forward merge ์ค์ต
# branch ํ์ธ
$ git branch
# ์ branch ๋ง๋ค๊ธฐ
$ git branch [branch ๋ช
]
# ๋ค๋ฅธ branch๋ก ์ด๋ํ๊ธฐ
$ git switch [์ด๋ํ๊ณ ์ ํ๋ branch ๋ช
]
# ๋ค๋ฅธ branch ์ด๋ ver2)
$ git checkout [์ด๋ํ๊ณ ์ ํ๋ branch ๋ช
]
# branch ์ญ์ ํ๊ธฐ
$ git branch -d [์ง์ฐ๊ณ ์ ํ๋ branch ๋ช
]
# FF(=Fast Forward) merge ํ๊ธฐ
# : ์ฃผ์ฒด๊ฐ ๋๋ branch๋ก ์ด๋ ํ ์งํ (ex. master๋ก ์ด๋ ํ chloe ๋ง๋ค๊ธฐ)
$ git merge [๋ณํฉํ branch ์ด๋ฆ]
# ์ branch ๋ง๋ค๋ฉด์ ์ด๋ํ๊ธฐ
$ git switch -c [์๋ก ๋ง๋ค branch]
# ์ branch ๋ง๋ค๋ฉด์ ์ด๋ ver2)
$ git checkout -b ashley # -b๋ branch ๋ปํจ
# git log graph๋ก ๋ณด๊ธฐ
$ git log --oneline --graph
ํน์ commit์ ๊ธฐ์ค์ผ๋ก ๋์๊ฐ๊ธฐ
$ git checkout f008d8ftest branch ๋ง๋ค๋ฉด์ ์ด๋ํ๊ธฐ
$ git checkout -b test
$ git log --oneline
f008d8f (HEAD -> test) 05 | Article Index
c2be3bc 04 | Article Model
0d28d53 03 | startapp articles
5afc6a9 02 | settings
a046911 01 | startprojectreference log ํ์ธ
$ git reflog์ด๋์ head ์๋์ง ํ์ธ
$ git log --oneline
fc58709 (HEAD -> master, origin/master, origin/HEAD) README.md ์ถ๊ฐ
fd2e992 06 | Article C with ModelForm
f008d8f (test) 05 | Article Index
c2be3bc 04 | Article Model
0d28d53 03 | startapp articles
5afc6a9 02 | settings
a046911 01 | startproject๋ค์ test branch๋ก ๋์๊ฐ๊ธฐ
$ git checkout test
Switched to branch 'test'
$ git reset์ผ๋ก commit ๋๋๋ฆฌ๊ธฐ
$ git reset --hard HEAD (going back to HEAD)
$ git reset --hard HEAD^ (going back to the commit before HEAD)
$ git reset --hard HEAD~2 (going back two commits before HEAD)
$ git reset --hard HEAD^^ (another syntax for going back two commits)
-for--force๋ช ๋ น์ด๋ก ๊ฐ์ ๋ก Push ํ๊ธฐ
$ git push -f origin master
$ git push --force origin master- ๋ฐฉ๋ฒ 1์ ์๊ฒฉ ์ ์ฅ์ commit history๋ฅผ ๊ฐ์ ๋ก ์กฐ์ํ๋ค๋ ๋ฌธ์ ์ ์ด ์์
- but,
git revert๋ฅผ ์ฌ์ฉํ๋ฉด commit์ ๋๋๋ฆฌ๋ ์์ ๋ ํ๋์ commit์ผ๋ก ๊ฐ์ฃผํ์ฌ commit history์ ์ถ๊ฐํ๊ฒ ๋จ
ํน์ commit์์์ ๋ณ๊ฒฝ ์ฌํญ์ ์ ๊ฑฐํ๋ ๋ ๋ค๋ฅธ commit ์์ฑํ๊ธฐ
$ git revert --no-commit [๋๋๋ฆฌ๊ณ ์ถ์ commit์ hash]
$ git revert --no-commmit [๋๋๋ฆฌ๊ณ ์ถ์ ์ปค๋ฐ์ ๋ฒ์]
$ git revert --no-commit Head~3+
-
masterbranch์ ์๋ ๋ด์ฉ์ ์๋์ผ๋ก ๋ฐฐํฌ๊ฐ ๋จ-
master๋ ๋ฐฐํฌ ๊ฐ๋ฅํ ์ํ
-
$ git clone -b {branch_name} --single-branch {์ ์ฅ์ URL}์ฐ์ ์๊ฒฉ ์ ์ฅ์์ ์ด๋ค branch ์๋์ง ํ์ธ
$ git branch -r์๊ฒฉ์ ์ฅ์ + ๋ก์ปฌ branch ํ์ธ
$ git branch -a$ git checkout -t [origin/๊ฐ์ ธ์ฌ ์๊ฒฉ ์ ์ฅ์ branch ์ด๋ฆ]$ git checkout -b [์์ฑํ branch ์ด๋ฆ][๊ฐ์ ธ์ฌ ์๊ฒฉ ์ ์ฅ์ branch ์ด๋ฆ]remote branch ๊ฐ ์ด๋๊น์ง ๊ฐ์๋์ง ํ์ธ ๊ฐ๋ฅ
- git pull == git fetch && merge
์ฌ ๋ฐฐ์นํ๊ธฐ
- ๋ค์
-i๋ฅผ ๋ถ์ด๋ฉด interactive ํ๊ฒ (๋ํํ์ผ๋ก) ์คํ ๊ฐ๋ฅ
-
Git main repo ์์ ๋ค๋ฅธ sub repo๊ฐ ์๋ ๊ฒ
- main repo์์๋ sub repo๋ฅผ ๊ด๋ฆฌํ์ง ์๋๋ค!
-
Submodule์ ์ฌ์ฉํ๋ฉด ํ๋ก์ ํธ ๊ฐ์ ๊ณตํต์ผ๋ก ์ฌ์ฉ๋๋ logic์ ๊ณต์ ํ ์ ์๋ค!
$ git submoduel add [repo ์ฃผ์]- ์ดํ์ ๋ฐ๋ก
git add์์ด commit ํ ์ ์์
$ git pull
- main repo root์์ ๋ค์ ๋ช ๋ น์ด๋ฅผ ์คํํ๋ฉด main repo ๊ฐ update ๋จ
$ git submodule update --remote --merge- main repo์ root์์ ์์ ๋ช ๋ น์ด๋ฅผ ์คํํ๋ฉด update๋ main repo์ sub repo์ ๊ด๊ณ์ ๋ฐ๋ผ sub repo๋ฅผ ์ ๋ฐ์ดํธ ํด์ผํ๋ค