๐ฅ Github ์ค์นํ๊ธฐ
- Git SCM์ ์ ์ํ์ฌ ์ค์น ํ์ผ ๋ค์ด๋ก๋
- https://git-scm.com/
1. Github ๊ธฐ๋ณธ ๋ช ๋ น์ด
1) local repository์ฉ
git init
: ์๋ก์ด ์ ์ฅ์ ๋ง๋ค๊ธฐgit clone [์๊ฒฉ ์ ์ฅ์ URL]
: ์ ์ฅ์์ ๋ชจ๋ ํ์ผ๋ค์ ๊ฐ์ ธ์ค๊ธฐ๋ง ํจgit clone -b [๋ธ๋์น๋ช ] [์๊ฒฉ ์ ์ฅ์ URL]
: ํน์ ๋ธ๋์น๋ฅผ clone
git add [์ถ๊ฐํ ํ์ผ๋ช ]
: ์ปค๋ฐํ ํ์ผ์ stage(์ปค๋ฐํ๊ธฐ ์ํ ์ค๋น ์ํ์ ๋์ ) ์์ผ์ค๋๋ค- -update : ์ถ์ ํ๊ณ ์๋ ํ์ผ๋ง add
git rm [์ญ์ ํ ํ์ผ๋ช ]
: ์ ๊ฑฐ ๊ด๋ จ ๋ช ๋ น์ด- -cached : ํด๋น ํ์ผ์ working directory ์ํ๋ก ๋๋๋ฆผ
(Git ์ ์ฅ์์์๋ง ์ญ์ ํ๊ณ ์ค์ ํ์ผ์ ๋จ๊ฒจ๋๊ณ ์ถ์ ๊ฒฝ์ฐ)
- -cached : ํด๋น ํ์ผ์ working directory ์ํ๋ก ๋๋๋ฆผ
git commit -m "[์ปค๋ฐ ๋ฉ์์ง]"
: commit ๋ฉ์์ง์ ํจ๊ป commitgit commit --ammend
: commit ๋ฉ์ธ์ง ๋ณ๊ฒฝgit status
: git ์ํ ํ์ธgit log
: commit ๋ชฉ๋ก ํ์ธ
2) local repository ↔ remote repository
git remote
: local repository์ ์ฐ๊ฒฐ๋ remote repository๋ฅผ ํ์ธgit remote add [๋ณ์นญ] [remote repository URL ์ฃผ์]
: ์๊ฒฉ ์ ์ฅ์ ๋ฑ๋กgit remote show [์๊ฒฉ ์ ์ฅ์๋ช ]
: ์๊ฒฉ ์ ์ฅ์ ์ ๋ณดgit push [์๊ฒฉ ์ ์ฅ์๋ช ] [์๊ฒฉ branch๋ช ]
: branch ๋ช ์๋ master ํน์ develop ๋ฑ์ ๋ณธ์ธ์ด ๋ง๋ branch์ ์ด๋ฆ์ ๋ฃ์ด ์ฃผ์ธ์git push [์๊ฒฉ ์ ์ฅ์๋ช ] [์๊ฒฉ branch๋ช ]
: branch ๋ช ์๋ master ํน์ develop ๋ฑ์ ๋ณธ์ธ์ด ๋ง๋ branch์ ์ด๋ฆ์ ๋ฃ์ด ์ฃผ์ธ์git pull [์๊ฒฉ ์ ์ฅ์๋ช ] [์๊ฒฉ branch๋ช ]
: local repository์ ๋น๊ตํ์ฌ ๋ณํฉ์ ํ๊ณ , local repository์ ์ ์ฅ( add )๊น์ง ์ํ- git pull = git fetch + git merge
3) master ↔ branch
git branch
: ๋ธ๋์น์ ๋ชฉ๋ก๊ณผ ํ์ฌ ์ฌ์ฉํ๊ณ ์๋ ๋ธ๋์น๋ฅผ ํ์ธgit branch [branch]
: ๋ธ๋์น ์์ฑ-r [branch]
: remote repository ๋ธ๋์น ๋ชฉ๋ก ํ์ธ-a [branch]
: local / remote repository ๋ชจ๋์ ๋ธ๋์น ๋ชฉ๋ก ํ์ธ-D [branch]
: local repository ๋ธ๋์น ์ญ์
git checkout [branch]
: ๋ธ๋์น๋ฅผ ์ด๋git checkout -b [branch]
: ๋ธ๋์น ์์ฑ ํ, ๊ทธ ๋ธ๋์น๋ก ์ด๋git fetch
: local repository์์ remote repository์ ๋๊ธฐํํ๋ ์ญํ (์ ๋ฐ์ดํธ)git merge [๋ธ๋์น]
: ํ์ฌ checkout ํ๊ณ ์๋ branch๋ก [branch]์์ ๋ณํฉgit checkout master git merge develop # develop --merge-> master
branch๋ฅผ merge ํ ๋๋ ๊ฐ์ ํ์ผ์ ์์ ํ ๊ฒฝ์ฐ ์๊ธธ ์ ์๋ ์ถฉ๋ ๋ฐ์์ ํญ์ ์ฃผ์ํด์ผ ํฉ๋๋ค.
2. Github ์ทจ์ ๋ช ๋ น์ด
1) git add ์ทจ์ํ๊ธฐ
git reset HEAD [ํ์ผ๋ช
]
: git add ํ [ํ์ผ] ์ทจ์, ์์ ๋ ์ํน ๋๋ ํฐ๋ฆฌ์ ํ์ผ์ ๋ณด์กด
git reset HEAD
: git addํ [ํ์ผ ์ ์ฒด] ์ทจ์, ์์ ๋ ์ํน ๋๋ ํฐ๋ฆฌ์ ํ์ผ์ ๋ณด์กด
2) commit ์ทจ์ํ๊ธฐ
git reset HEAD^
: commit ์ทจ์ (์์ ๋์ผ)git reset HEAD~2
: ๋ง์ง๋ง commit 2๊ฐ ์ทจ์.git reset [commit_id]
: git add๋ฅผ ํ ์ํ์์, ๋ณ๊ฒฝ๋ staging area๋ฅผ ๋ฌดํจํ (์์ ๋ ๋ด์ฉ์ ์ ์ง)git reset --soft [commit_id]
: [commit_id
]์ผ๋ก ๋๋๋ฆผ. staged ์ํ, ์ํน ๋๋ ํฐ๋ฆฌ์ ํ์ผ ๋ณด์กด.git reset --soft HEAD^
: ์ง์ commit ์ทจ์. staged ์ํ, ์ํน ๋๋ ํฐ๋ฆฌ์ ํ์ผ ๋ณด์กด.git reset --mixed [commit_id]
: [commit_id
]์ผ๋ก ๋๋๋ฆผ. unstaged ์ํ, ์ํน ๋๋ ํฐ๋ฆฌ์ ํ์ผ ๋ณด์กดgit reset --mixed HEAD^
: ์ง์ commit ์ทจ์. unstaged ์ํ, ์ํน ๋๋ ํฐ๋ฆฌ์ ํ์ผ ๋ณด์กด(๊ธฐ๋ณธ ์ต์ )git reset --hard [commit_id]
: [commit_id
]์ผ๋ก ๋๋๋ฆผ. unstaged ์ํ, ์ํน ๋๋ ํฐ๋ฆฌ์ ํ์ผ ์ญ์ . ์ฆ ๋ชจ๋ ์ทจ์.git reset --hard HEAD^
: ์ง์ commit ์ทจ์. unstaged ์ํ, ์ํน ๋๋ ํฐ๋ฆฌ์ ํ์ผ ์ญ์ . ์ฆ ๋ชจ๋ ์ทจ์.
3) git push ์ทจ์ํ๊ธฐ
์ฃผ์ ์ฌํญ
- local์ ๋ด์ฉ์ remote์ ๊ฐ์ ๋ก ๋ฎ์ด์ฐ๊ธฐ๋ฅผ ํ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ์ฃผ์.
- ๋๋์๊ฐ commit ์ดํ์ ๋ชจ๋ commit ์ ๋ณด๊ฐ ์ฌ๋ผ์ง๊ธฐ ๋๋ฌธ์ ์ฃผ์.
- ํ์ ํ๋ก์ ํธ์์๋ ๋๊ธฐํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์์ผ๋ฏ๋ก ์ฃผ์
- ๋ฐฉ๋ฒ 1
- ๊ณผ์
- ์ํน ๋๋ ํฐ๋ฆฌ์์ commit์ ๋๋๋ฆฐ๋ค →
- ๋๋๋ ค์ง ์ํ์์ ๋ค์ commit์ ํ๋ค →
- ์๊ฒฉ ์ ์ฅ์์ ๊ฐ์ ๋ก push ํ๋ค.
- ๋ช
๋ น์ด ์์
git reset HEAD^
/git reset [๋ฒ์ ๋ช ]
git commit -m "๋ณ๊ฒฝํ ๋ฉ์์ง"
git push origin +[branch]
: ํด๋น branch๋ฅผ ์๊ฒฉ ์ ์ฅ์์ ๊ฐ์ ๋ก push ('+' ๋ถ์ฌ์ผ ํจ)git push origin [branch ] -f
: ํด๋น branch๋ฅผ ์๊ฒฉ ์ ์ฅ์์ ๊ฐ์ ๋ก push
- ๊ณผ์
- ๋ฐฉ๋ฒ 2
git revert [commit_id]
: ํด๋น ์ปค๋ฐ๋ง์ ๋๋๋ฆผgit revert [commit_id1..commit_id2]
: ํด๋น ๋ฒ์ฃผ์ ์ปค๋ฐ์ ๋ชจ๋ ๋๋๋ฆผ
4) git merge ์ทจ์ํ๊ธฐ
git reset -merge ORIG_HEAD
- master์ child ๋ ๋ธ๋์น๊ฐ ์์ ๋, master ๋ธ๋์น์์ child ๋ธ๋์น๋ฅผ ๋ณํฉํ๋ ค๊ณ ํ๋๋ฐ ๋ณํฉ์ ํ๋ฉด ์ ๋๋ ์์ ์ด์๋ ๊ฒ์ด ์๋ค๋ฉด, ์ด ๊ฒฝ์ฐ์ ๋ณํฉ์ ํ ํ, ๋ฐ๋ก ๋ณํฉ์ ์ทจ์
- ๋ณํฉ์ ์ํํ ์์ ์ด๊ธฐ ๋๋ฌธ์ Git์ ๋ณํฉ์ ํ๊ธฐ ์ ์, ์ต์ ์ปค๋ฐ์ ํฌ์ธํฐ๋ฅผ ์ง์ ( ORIG_HEAD )ํฉ๋๋ค.
- ๋ฐ๋ผ์ ๋ฐฉ๊ธ ๋ณํฉํ ๊ฒ์ ๋๋๋ฆฌ๋ ค๋ฉด -merge ์ต์ ๊ณผ ORIG_HEAD ํฌ์ธํฐ๋ฅผ ์ง์ ํ์ฌ reset ๋ช ๋ น์ด๋ฅผ ์คํํ๋ฉด ๋ฉ๋๋ค.
git revert --mainline 1 {์ทจ์ํ ๋ณํฉ ์ปค๋ฐ ID}
- revert์ --mainline ์ต์ ์ ์ฌ์ฉํด์ ๋ณํฉ์ ์ทจ์ํ ์ ์์ต๋๋ค.
- mainline์ด๋ ๋ณํฉ์ ์ทจ์ํ ํ์, ์ด๋ ๋ผ์ธ์ ๊ธฐ์ค์ผ๋ก ๋๋์๊ฐ ๊ฒ์ธ์ง ๊ธฐ์ค์ ์ ํ๋ ๊ฒ์ ๋๋ค.
- ๋ช ๋ น์ด๋ฅผ ์คํํ๋ฉด ํธ์ง๊ธฐ ์ฐฝ์ด ๋์ค๋ฉด์ ์ปค๋ฐ ๋ฉ์์ง๋ฅผ ์์ฑํ๋ผ๊ณ ํฉ๋๋ค.
5) git reset VS git revert
- ๋จ์ํ๊ฒ ๋๋๋ฆด ์์ ์ดํ์ commit์ ์ ๋ถ ๋ ๋ฆฌ๊ณ remote repository์ ๊ฐ์ ๋ก push ํ์ฌ ๋๋๋ฆฌ๋ ค๋ฉด reset์ ์ฌ์ฉํ๋ฉด ๋๋ค - commit history๋ฅผ ๋จ์ํ๊ฒ ๋ง๋ค์ด ์ค๋ค
์ด๋ฏธ ์๊ฒฉ ๋ฆฌํ์งํ ๋ฆฌ์ push๋ฅผ ํ ์ํ๋ผ๋ฉด reset์ ์ฌ์ฉํ๋ฉด reset ํ๊ธฐ ์ด์ ์ผ๋ก ๋๋๋ฆฌ๊ธฐ ์ ๊น์ง๋ push ํ ์ ์๊ฒ ๋ฉ๋๋ค. (๋ฌผ๋ก force๋ผ๋ ๋ฌด์๋ฌด์ํ ์ต์ ์ด ์๊ธฐ๋ ํฉ๋๋ค. )
๋ฐ๋ผ์, ์ฌ๋ฌ ๋ฌธ์๋ค์์๋ ์ด๋ฏธ push ํ ์ฝ๋์ ๋ํด์๋ revert๋ฅผ ์ฌ์ฉํ ๊ฒ์ ์ถ์ฒํ๊ณ ์๋ค์...ใ ใ (์ ํํ์ง ์์ต๋๋ค.)
- ํ์ง๋ง ์ด๋ ฅ ์ค๊ฐ์ ๋ก๊ทธ ์ถ๋ ฅํ๋๋ก ํ ์ปค๋ฐ์ด ์๊ณ ๊ทธ ์ปค๋ฐ๋ง์ ์ทจ์ํ๋ ค๊ณ ํ๋ค๋ฉด revert๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ์ปค๋ฐ์ ๋ด์ฉ๋ง ๋๋๋ฆด ์ ์๋ค.
3. ํ์ ์ฉ github ์ฌ์ฉ
1. upstream์ ๋ํ ์ดํด
git remote add upstream [remote repository URL ์ฃผ์]
: upstream [์ฃผ์] remotegit branch -r
: -r ๋ถ์ด๋ฉด ์๊ฒฉ ์ ์ฅ์์ branch list ํ์ธgit branch -v
git fetch upstream
: upstream ์ ์ฅ์์ ์๋ ๋ณ๊ฒฝ์ฌํญ๋ค์ ๋ก์ปฌ๋ก ๊ฐ์ ธ์ค๋ฉด์ ์๋ก์ด branch ์์ฑgit checkout [master]
: [๋ด ๋ธ๋์น]๋ก ํ์ฌ ๊ฐ๋ฆฌํค๋ branch ์ ๋ฐ์ดํธgit merge [upstream/develop]
:[upstream/develop]
์ ๋ด ํ์ฌ branch๋ก merge(๋๊ธฐํ)git push [origin] [branch]
: ๋ด ์๊ฒฉ repo๋ก push ํด์ฃผ๋ฉด ๋
2. PR (Pull Request)
- upstream repository๋ฅผ ๋ด repository์ ๋๊ธฐํํ๋ ๊ฒฝ์ฐ
- ์ด๋ ๊ฒ upstream repository์ ๋๊ธฐํ ํ ๋ด ๋ ํฌ๋ฅผ upstream ๋ ํฌ๋ก PR์ ๋ณด๋
- upstream์์ conflict๊ฐ ์์ ์ PR ์น์ธ
- ๋ค๋ฅธ ํ์ ์๋ค์ ๋ณธ์ธ์ ๋ณ๊ฒฝ์ฌํญ์ผ๋ก PR ํ๊ธฐ ์ ์ ๋ค์ 1. ์ ๊ณผ์ ์ ํตํด ๋ณธ์ธ์ ๋ ํฌ๋ฅผ upstream์ ๋ ํฌ์ ๋๊ธฐํํ ํ PR
- upstream repository์ ๋ด ๋ณ๊ฒฝ์ฌํญ๋ง ์ถ๊ฐํ๊ณ ์ถ์ ๊ฒฝ์ฐ
- upstream๊ณผ ๋๊ธฐํ๋ฅผ ํ์ง ์๊ณ , ํ์ ์์ ๋ค๋ฅธ ํ์ผ๋ง์ ๊ฑด๋ ๋ค๋ฉด fetch, merge ์์ด PR ๊ฐ๋ฅ
๋ ์๊ณ ์ถ์ ๋ถ์...
- https://learngitbranching.js.org/index.html?demo
- https://goodtogreate.tistory.com/entry/GIT-tip-์ ๋ฆฌ?category=440231
์ฐธ๊ณ ๋ฐ ์ถ์ฒ:
[์ ์ฒด์ ์ธ git ๋ช
๋ น์ด ์ฐธ๊ณ ](https://victorydntmd.tistory.com/category/Git/%EA%B0%9C%EB%85%90%EA%B3%BC%20%ED%99%9C%EC%9A%A9?page=2)
[reset, revert ์ฌ์ฉํ๋ ์์ ์ฐธ๊ณ ](https://medium.com/nonamedeveloper/%EC%B4%88%EB%B3%B4%EC%9A%A9-git-%EB%90%98%EB%8F%8C%EB%A6%AC%EA%B8%B0-reset-revert-d572b4cb0bd5)