첫 수업시간에 Git의 기본 원리와 사용 방법에 대해서 배웠다.
아래 글에 더 자세하게 있다.
2023.07.18 - [IT/Git] - Git의 기본 원리 (git add, commit, push)
아이맥이라는 새로운 PC와 새로운 환경에서 Git을 진행했다.
포맷이 되지 않았는지 전에 사용하던 분의 정보가 저장되어 있었고, Git 설정 중에 config관련해서 새로운 사실을 알게 되었다.
그리고 add, commit, push를 하는 방법은 습득하면서도 취소하는 방법이 궁금해져서 찾아보다가 내용이 복잡해서 나만의 글로 정리하려 한다.
1. git config
git 수업을 진행하다가 아래와 같은 git 명령어의 결과가 달라서 검색해보았다.
상황이 어떠했냐면, user.name과 user.email을 등록하기 위해 우연히 명령어를 치다가 --global 옵션을 빼놓고 쳐본 것이 화근이었다.
정신 없이 검색하다가 발견하고 찾은 내용을 정리해보려고 한다.
# 두 명령어의 결과가 달랐다.
$ git config --list
$ git config --global list
1) git config를 설정할 때, configuration level이라는 용어를 봤다.
구성 수준이라고 번역되었는데, 총 3가지가 존재했다. (local / global / system)
(1) local: 현재 존재하는 로컬 레포지토리 범위라고 생각하면 된다.
(2) global: 전역이라고 번역이 되는데, 조금 더 정확하게는 현재 접속한 사용자(특정 사용자) 범위라고 생각하면 된다.
(3) system: 시스템 내부의 모든 사용자와 모든 저장소가 포함된다.
각각의 범위 만큼 실제 관리하는 config 파일도 3가지나 존재했다.
Windows와 Ubuntu에서 git config파일을 어디에서 보관하고 관리하는지 잘 정리된 글을 공유하려고 한다.
*[git config 파일 위치] (https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Where-system-global-and-local-Windows-Git-config-files-are-saved)
$ git config --list와 $ git config --global --list, $ git config --system --list의 결과가 해당 파일을 정말로 가지고 오는지 해결하지는 못했지만, 관리 파일이 어디에 존재하는지 문제가 발생했을 때 어떤 파일을 찾아가서 보면 되는지 알게 되었다.
2. $ git add, $ git commit, $ git push 취소하기
오늘 수업에서는 기본적인 git add ~ commit ~ push 까지의 과정을 집중적으로 진행했는데, 문득 반대로 git add ~ commit ~ push 과정을 취소하는 방법은 없을까 싶어서 구글링을 해봤다.
아래 사이트에서 작성한 글을 코드 위주로 정리해봤다.
*[git add, commit, push 취소하기] (https://gmlwjd9405.github.io/2018/05/25/git-add-cancle.html)
※ Windows에서는 $ git reset HEAD^ 라고 입력하면, More? 라고 물어본다.
이 경우, 마지막에 ^ 하나를 더 입력해주면 된다.
Ex) $ git reset HEAD^^
*[Windows에서 More?라고 진행되는 것 해결 방법] (https://stackoverflow.com/questions/14203952/git-reset-asks-more)
1) git add 취소하기
# git add 취소하기
$ git reset HEAD [fileName] # 파일명이 없으면 모든 파일 git add 취소
2) git commit 취소하기
# git commit 취소하기
$ git reset [--soft | --mixed | --hard]
# 3가지 방법이 존재
# 1) index와 워킹디렉토리의 파일을 모두 보존
# → add 한 상태, staged상태
$ git reset --soft HEAD^
# 2) index는 제거, but 워킹디렉토리의 파일은 보존
# → add 하기 전 상태, unstaged 상태
$ git reset --mixed HEAD^ # default
$ git reset HEAD^
# 3-1) index와 워킹디렉토리의 파일을 모두 제거
# → add 하기 전 상태, unstaged 상태
$ git reset --hard HEAD^
# 3-2) index와 워킹디렉토리의 파일을 모두 제거
# → 원격 저장소에 있는 마지막 commit 상태가 됨 & add 했던 파일들 모두 사라짐
$ git reset --hard HEAD
2-2) commit message 변경하기
# commit message 변경하기
$ git commit --amend
3) git push 취소하기
※ 주의: 이 과정을 통해 진행하면 자신의 local내용이 remote에 덮어씌워진다.
# git push 취소하기
# 1) 워킹 디렉토리에서 commit을 취소
$ git reset HEAD^
# 2) Reflog 목록 확인
$ git reflog
$ git log -g
# 3) 원하는 시점으로 워킹 디렉토리를 되돌림
$ git reset HEAD@{number}
$ git reset [commit id]
# 4) 되돌려진 상태에서 다시 commit 실행
$ git commit -m "message"
# 5) 원격 저장소에 강제로 push
$ git push [remoteName] [branchName] -f
$ git push [remoteName] +[branchName]
- git log -g와 git reflog 결과 이미지
3. $ git push [-u] 옵션의 의미
# -u option
# main이라는 브랜치를 자동으로 origin이라는 원격 저장소의 main브랜치로 연결
# → $ git push나 $ git pull이라고만 써도 알아서 작업이 진행된다.
$ git push -u origin main
[Reference]
Where system, global and local Git config files on Windows and Ubuntu Linux are
Git config file locations One of the five basic Git commands beginners need to learn is git config, if for no other reason than to perform an initial commit without the Git tool pestering for a user.name and user.email address. But the git config command c
www.theserverside.com
2) [git add, commit, push 취소하기] (https://gmlwjd9405.github.io/2018/05/25/git-add-cancle.html)
[Git] git add 취소하기, git commit 취소하기, git push 취소하기 - Heee's Development Blog
Step by step goes a long way.
gmlwjd9405.github.io
git reset asks 'more?'
Git windows command line, version 1.8.0 I have 3 commits so far and when I type git reset --soft HEAD^ new line comes up with More? and flashing cursor for input Then, whatever I type, I alwa...
stackoverflow.com