태섭씨 블로그

git commit 되돌리기 (reset 명령어) 본문

IT/git

git commit 되돌리기 (reset 명령어)

태섭씨 2015. 7. 29. 11:23

<git 수정한 파일 되돌릴 때 (discard changes)>

local 에 수정한 상태(modified)의 파일을 되돌릴 때 


git checkout -- <파일>


<git staging 상태 파일을 되돌릴 때 (unstage)>

local 에 수정 후 git add 명령어로 staging 상태로 되어 있는 파일을 되돌릴 때


git reset HEAD <file>




<git local commit한 것을 취소해야 하는 경우>


git reset HEAD~1      


여기서 ~1은 뒤로 돌릴 커밋의 갯수다. 3개를 돌리려면 git reset HEAD~3

동일한 명령어로 


git reset HEAD^ 


여기서 ^ 는 뒤로 돌릴 커밋의 갯수. 3개를 돌리려면 git reset HEAD ^^^


경우에 따라서 --hard 옵션으로 local의 더 최신 커밋을 삭제할 필요도 있겠다.



<git push 취소해야 하는 경우 (또는 일부 파일을 commit 에서 제외)>

소스 수정,커밋 후 푸시까지 했는데 알고 보니 취소해야 하는 상황 ... 이런 경우 

1. local 의 commit 을 취소한다. 위의 <git local commit한 것을 취소해야 하는 경우> 를 참고.

2. 커밋 제외할 파일을 돌린다 : <git staging 상태 파일을 되돌릴 때> 참고

3. local commit.

4. push하려고  

git push <remote> <branch명> 

실행하면 아래와 같이 오류 발생.

$ git push -v origin feature/deactivate

Pushing to ssh://내ㄱㅖ정@서버명 (- -;)

계정 password:

To ssh://계정@서버명

 ! [rejected]        feature/<branch명> -> feature/<branch명> (non-fast-forward)

error: failed to push some refs to 'ssh://계정@서버'

hint: Updates were rejected because the tip of your current branch is behind

hint: its remote counterpart. Integrate the remote changes (e.g.

hint: 'git pull ...') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.

현재 local 에서 가리키는 커밋이 리모트보다 뒤(behind)에 있어서 발생.

무시하고 커밋을 하기 위해 

git push -f <remote> <branch명>


이렇게 하면 강제로 push 를 해버린다. 



Comments