본문 바로가기

Dev. Tools

Git: Commit History 초기화

공개 Repository를 사용하다며본 자칫 민감한 정보가 코드에 적혀 공개되는 경우가 생긴다.

이런 경우 다음과 같은 두가지 작업이 필요할 수 있다.

 

마지막 Commit 취소 후 Push

# 마지막에 실행한 commit을 되돌림
$ git reset HEAD^

# 새로운 내용으로 다시 commit 진행
$ git commit -m "new commit message"

# 서버로 강제 push
# git push origin master -f

 

Commit History 초기화

Git 초기화

# git 삭제
$ rm -rf .git

# git 생성
$ git init
$ git add -A
$ git commit -m "First Commit"
$ git remote add origin [저장소 URL]

초기화된 Git Push

그냥 push 하기 - rejected

$ git push origin master
... ...
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:dongchangYoo/test.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

강제 push 하기 - 성공

$ git push origin master -f

새로 생성된 Remote Git에 강제 push 하기 - 성공

$ git push --set-upstream origin master -f

'Dev. Tools' 카테고리의 다른 글

Maven에서 Gradle로 갈아타기  (0) 2020.07.21
Gson 사용법 (in Java)  (0) 2020.07.01
Project Naming on IntelliJ: groupId, artifactId, version  (0) 2020.06.30