push๊นŒ์ง€ ํ•ด๋ฒ„๋ฆฐ git commit ์›๋ณตํ•˜๋Š” ๋ฐฉ๋ฒ•


๊ณต๋™ repository์— push๊นŒ์ง€ ํ•ด๋ฒ„๋ฆฐ commit์„ ๋˜๋Œ๋ฆฌ๋Š” ๋ฐฉ๋ฒ•์€ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
์ปค๋ฐ‹ ๊ฐœ์ˆ˜๊ฐ€ ํ•œ ๋‘ ๊ฑด ์ผ ๋•Œ git revert๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๊ณ , ์ปค๋ฐ‹ ๊ฐœ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์•„ ํ•˜๋‚˜์”ฉ revertํ•˜๊ธฐ ํž˜๋“  ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” git revert๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์›๋ณตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค๋“œ๋ฆฝ๋‹ˆ๋‹ค.

1. commit log ํ™•์ธ

2. reset (์›ํ•˜๋Š” ์‹œ์ ์œผ๋กœ ๋˜๋Œ์•„๊ฐ€๊ธฐ)

3. revert (ํŠน์ • ์‹œ์  ์ดํ›„์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ ๋˜๋Œ๋ฆฌ๊ธฐ)

4. force push (๋˜๋Œ๋ฆฐ ๋‚ด์šฉ์„ ๊ณต๋™ repo์— ๋ฐ˜์˜ํ•˜๊ธฐ)


1. commit history์—์„œ ์›๋ณต ์‹œ์ ์˜ ์ปค๋ฐ‹ ํ™•์ธ

keichee$ git log -5 --pretty=format:"%h - %an, %ar : %s"

148444a6a - keichee, 4 days ago : Merge branch 'dev-sentry' into stage

eebdd9202 - keichee, 4 days ago : dev ํ™˜๊ฒฝ ๋กœ๊น… sentry ์—ฐ๋™

1c74ca53e - john, 5 days ago : Merge pull request #1238 in test/repo from release/200202 to stage

6544cd10a - john, 5 days ago : Merge pull request #1237 in test/repo from feature/200202 to release

754046d47 - tom, 5 days ago : test commit 1


git log ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด์„œ commit ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋•Œ ์˜ต์…˜์œผ๋กœ -5 ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ๋ฉด ์ตœ์‹ ์ˆœ์œผ๋กœ 5๊ฐœ ๊นŒ์ง€๋งŒ ์กฐํšŒ๋ฅผ ํ•  ์ˆ˜ ์žˆ๊ณ  

์˜ต์…˜์„ ์ฃผ์ง€ ์•Š์œผ๋ฉด ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ๋˜์–ด ๊ณ„์† ์กฐํšŒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋’ค์— pretty ์˜ต์…˜์„ ์ฃผ๋ฉด ์œ„ ๊ฒฐ๊ณผ์ฒ˜๋Ÿผ ์กฐํšŒ๊ฐ€ ๋˜๋Š”๋ฐ pretty ์˜ต์…˜์„ ์•ˆ์ฃผ๋ฉด commit ํ•˜๋‚˜์˜ ๋‚ด์šฉ์ด ์ด 6์ค„์— ๊ฑธ์ณ์„œ ํ‘œ์‹œ๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๋ˆˆ์— ๋ณด๊ธฐ๊ฐ€ ํž˜๋“ง๋‹ˆ๋‹ค.


2. ์›ํ•˜๋Š” ์‹œ์ ์œผ๋กœ Reset 

์œ„์—์„œ ์กฐํšŒํ•œ 5๊ฐœ์˜ ์ปค๋ฐ‹๋“ค ์ค‘์— john์ด 5์ผ ์ „์— ์ปค๋ฐ‹ํ•œ ๋‚ด์šฉ๊นŒ์ง€๋งŒ ์ ์šฉํ•˜๊ณ  keichee๊ฐ€ ์ปค๋ฐ‹ํ•œ ๋‚ด์šฉ์„ revert์‹œ์ผœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

commit it๊ฐ€ 1c74ca53e์ธ ๊ฒƒ์„ ํ™•์ธํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด reset ํ•ฉ๋‹ˆ๋‹ค.

keichee$ git reset 1c74ca53e

Unstaged changes after reset:

M       src/main/resources/logback-spring.xml


3. Revert

reset์„ ํ•˜๋ฉด 1c74ca53e ์ปค๋ฐ‹๊นŒ์ง€ ์™„๋ฃŒ๋œ ์ƒํƒœ๋กœ ๋˜๋Œ์•„ ๊ฐ‘๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ทธ ์ดํ›„์˜ ์ปค๋ฐ‹์— ๋Œ€ํ•ด์„œ๋Š” ์ˆ˜์ •์ƒํƒœ๋กœ ๋ณ€๊ฒฝ๋ฉ๋‹ˆ๋‹ค.

์ด๋•Œ git status๋กœ ํ™•์ธ์„ ํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

keichee$ git status

On branch test-revert

Changes not staged for commit:

  (use "git add <file>..." to update what will be committed)

  (use "git checkout -- <file>..." to discard changes in working directory)


        modified:   src/main/resources/logback-spring.xml

์ด์ œ ์ด๋ ‡๊ฒŒ ์ˆ˜์ •๋œ ๋‚ด์šฉ๋“ค์— ๋Œ€ํ•ด์„œ ์›๋ณตํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

keichee$ git checkout -- src/main/resources/logback-spring.xml

ํŒŒ์ผ๋“ค์ด ๋„ˆ๋ฌด ๋งŽ์•„ ์ด๋ ‡๊ฒŒ ํ•˜๊ธฐ๊ฐ€ ๋ถˆํŽธํ•˜๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

# Revert changes to modified files.

git reset --hard


# Remove all untracked files and directories.

# '-f' is force, '-d' is remove directories.

git clean -fd

์ถœ์ฒ˜ : https://stackoverflow.com/questions/5807137/how-to-revert-uncommitted-changes-including-files-and-folders


4. Force push

keichee$ git push -f

Total 0 (delta 0), reused 0 (delta 0)

remote: 

remote: Create pull request for test-revert:

remote:   https://git.repository/compare/commits?sourceBranch=refs/heads/test-revert

remote: 

To https://git.repository

 + 61495b45b...1c74ca53e test-revert -> test-revert (forced update)



์ด์ƒ์ž…๋‹ˆ๋‹ค.

์˜ค๋Š˜๋„ ์ฆ์ฝ”๋”ฉํ•˜์„ธ์š”~