본문 바로가기

회고록(TIL&WIL)

TIL 2023.01.30 Git 명령어 정리

Git 설정

Git 최초 계정 설정 - 설치 후 git bash에 접속하여 가장 먼저 해줘야 하는 계정 설정

 * 만약 프로젝트마다 다른 이름과 이메일 주소를 사용하고 싶으면 --global 옵션을 빼고 명령을 실행한다.

git config --global user.name "nickname"
git config --global user.email "user@mail.com"
git config --list (입력확인)

계정 설정 초기화

git config --unset user.name 
git config --unset user.email

Alias 를 이용하여 간단하게 사용할 명령어 설정하기

git config --global alias.unstage 'reset HEAD --' (언스태이징)
git unstage fileA

git config --global alias.last 'log -1 HEAD' (가장 최근 커밋 하나 확인)
git last

git config --global alias.cl 'log --pretty=format:"%h %s" --graph'
git cl

Git 저장소(Repository) 생성 방법

cd /e/webcash/project (우선 프로젝트의 디렉토리로 이동한다. )

git init (local repository 생성 .git 숨김파일 생성됨)
git remote origin NickName@<GitHubRepoURL>
git remote -v
git add .
git commit -m "initial commit"
git push -u origin main

기존 원격 Git 저장소를 Clone 하는 방법

git clone <GitHubRepoURL>

Git 기본 명령어

조회관련 명령어

 - git status (현재 파일의 상태를 확인)
 - git diff  file.bin (해당 파일, commit등의 변경점을 보여줌)

 - git mv README.md Update.md (파일 이름 변경)
 - git rm PROJECTS.md (파일 삭제 후 staged 까지 해줌 커밋은 따로 해야함)

 - git log (commit history 확인)
    -<n> (최근 n개의 commit 확인)
    --stat (diff 내용 추가 확인)
    --graph ( 그래프형태로 확인가능)
    --pretty=oneline , format:"%h %s"
    --after --before
    --author (저자로 검색) --grep (커밋메시지로 검색)
    -S (코드에서 추가되거나 제거된 내용중에 특정 텍스트가 포함되어 있는지 여부 확인)

저장하기 명령어 Add - Commit - Push

 - git add file.bin (해당 파일을 tracking하며, 파일을 staging함 . 을 사용하면 현재 경로 지정가능)
 - git commit -m "commit message" (스태이징한 파일을 커밋)
 - git push <remote> <branch> (최초 push할 때 현재 로컬branch가 원격branch를 가리키도록)
 - git push <remote> --delete <branch> (push 후에 원격branch 삭제)

불러오기, 병합 명령어 Pull, Merge

 - git fetch (원격 repo와 로컬 repo를 동기화)
 - git merge <branchName> (작성한 branch와 현재 checkout중인 branch를 병합)
 - git merge -Xours <branchName> (현재 브랜치 기준으로 덮어쓰기)
 - git merge -Xtheirs <branchName>  (타겟 브랜치 기준으로 덮어쓰기)
 - git pull (현재 로컬branch와 원격branch를 동기화, fetch와 merge 작업을 한번에 해줌)
 - git merge -Xignore-space-change whitespace (공백 변경 사항 무시하고 merge)

되돌리기 명령어들 Reset, Revert

 - git reset --option 돌아갈 커밋
 - git reset HEAD^ (commit을 바로 이전 상황으로 돌리기)
 - git reset --hard [commitHash](돌아간 커밋 이후의 변경 이력, 내용 모두 삭제)
 - git reset --mixed [commitHash] (변경 이력 삭제, 변경 내용은 유지)
 - git reset --soft [commitHash] (변경 이력 삭제, 변경 내용 유지, 내용 staging)
 - git reset HEAD <file> (스태이징 된 파일 언스태이징)
 - git revert [commitHash] (커밋에 해당하는 내용만 삭제, Revert되었다는 commit 추가됨) 
   * revert도 reset 과 같이 --hard, --mixed, --soft 옵션 사용 가능
 - git commit --amend (커밋 재작성)
 - git checkout -- <file> (해당 파일 수정 사항 되돌리기)
 - git merge --abort (merge 되돌리기)

브랜치 관련 명령어들 Branch, Checkout

 - git branch (branch 목록 및 현재 branch 위치 확인)
 - git branch <branchName> (branch 생성)
 - git branch -d <branchName> (branch 삭제 -D는 강제 삭제)
 - git branch -v <branchName> (각 branch와 마지막 commit 메시지 확인)
 - git branch --merged (merge가 완료되어 삭제가능한 branch 확인)
 - git branch --no-merged (merge안된 commit 보유중인 branch 확인)
 - git branch -u <remote>/<branch> (현재 로컬branch를 해당 원격branch와 연결하기)
 - git checkout <branchName> (branch 이동)
 - git checkout -b <branchName> (branch 생성 후 바로 이동)
 - git checkout -b <branch> <retmote>/<branch> (원격branch를 로컬branch로 생성+불러오기)

치워놓기 관련 명령어들 Stash

 - git stash (현재 작업본 stash로 치우기)
 - git stash list (stash 리스트 확인)
 - git stash apply [stashName] (stash 적용 생략 시 가장 최근 것 적용)
 - git stash drop [stashName] (stash 삭제 생략 시 가장 최근 것 삭제)
 - git stash pop (최근 stash 적용과 동시에 drop)
 - git stash show -p | git apply -R (stash 되돌리기)