CM/Github

git - 중요한 file을 모르고 push했을땐?

태하팍 2022. 3. 3. 16:57
반응형

1. 임의로 test.pem을 생성하여 push하였다.

test.pem이 올라가면 안돼는데 올라갔다면??


2. test.pem이 알고봤더니 정말 중요한 파일 이였다! 예를 들어 나의 비밀번호들이 저장되어있다..!!

3. 삭제를 하기 위해서는  아래와 같은 git filter-branch명령어를 사용 합니다.
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch src/main/resources/test.pem' --prune-empty -- --all

git rm --cached는 원격지 브랜치에 있는 파일을 삭제 해줍니다. 
git rm 로컬과 원격지 둘다 삭제.
여기에는 로컬에는 남아있어도 되지만 원격지에 올라가면 안되기때문에 --cached 옵션을 사용한 것이다.
--ignore-unmatch는 매칭되지 않는 파일은 pass~즉, 삭제 하지 않고 pass~
--prune-empty 해당 디렉토리 및 파일들과 관련된 내용만으로 존재하던 비어있는 커밋들도 젝해야하기 때문에 사용. 
4. remote 브랜치에 강제로 push
git push origin [branch명] --force
를 하게 되면 원격지에 파일이 깔끔하게 삭제 됩니다.


역시나 동일하게 중요파일이 올라갔다!!
3. 삭제를 하기 위해 git filter-branch를 사용하는데! 이번에는 index-filter가 아니라 --tree-filter를 사용 해본다.
 --tree-filter 옵션으로 특정 파일이나 디렉토리 제거가 가능 합니다.
git filter-branch --tree-filter 'git rm --cached --ignore-unmatch -f src/main/resources/test.pem' --prune-empty HEAD

아래와 같이 back up 오류가 났을 때

git filter-branch --tree-filter 'git rm --cached  -f src/main/resources/test.pem' --prune-empty HEAD
WARNING: git-filter-branch has a glut of gotchas generating mangled history
	 rewrites.  Hit Ctrl-C before proceeding to abort, then use an
	 alternative filtering tool such as 'git filter-repo'
	 (https://github.com/newren/git-filter-repo/) instead.  See the
	 filter-branch manual page for more details; to squelch this warning,
	 set FILTER_BRANCH_SQUELCH_WARNING=1.
Proceeding with filter-branch...

Cannot create a new backup.
A previous backup already exists in refs/original/
Force overwriting the backup with -f

git update-ref -d refs/original/refs/remotes/origin/master 로 백업을 삭제 해준다.
-d 에있는 경로는 다를수 있다. 그러므로 .git/refs/original/로 따라 들어가서 경로를 확인 후 제거해준다.

제거 후 아래와 같이 다시 시도를 하는데 다른 오류가 발생ㅋㅋ
fatal: pathspec 'src/main/resources/test.pem' did not match any files

--ignore-unmatch 옵션을 추가해주고 다시 수행한다.

그런데 --cached옵션을 주니 적용이 안된다. 

그리고 나서 git push origin master -f를 해주면 Everything up-to-date(최신정보)라는 문구만 뜬다..


그래서 아래와 같이 --cached옵션을 삭제 후 로컬과 원격지 둘다 삭제를 한다. 
git filter-branch --tree-filter 'git rm  --ignore-unmatch -f src/main/resources/test.pem' --prune-empty HEAD
그리고 마지막으로 git push origin master -f 를 수행해서 강제 push해주면 원격지에도 test.pem이 삭제된다.

github에 가보면 깔끔하게 삭제 된것을 볼수 있다.

- 끝 -

반응형