본문 바로가기
개발/Git, GitHub

Git Bash - 깃 명령어 정리

by 피로물든딸기 2023. 3. 29.
반응형

Git / GitHub 전체 링크

 

참고

- Git Cheet Sheet

 

- git config 로 설정하기

- 로컬 저장소 생성하기

- 파일 commit 하기

- 로그 확인하기

- 작업 취소하기

- 브랜치 관리하기

- 수정 중인 파일 숨기기

- 체리픽으로 다른 코드 가져오기


git config 로 설정하기

 

git config --global user.name [이름]

git config --global user.email [이메일]

$ git config --global user.name "bloodstrawberry"
$ git config --global user.email "bloodstrawberry@gmail.com"

 

git config --list 로 결과를 확인할 수 있다.

$ git config --list
...
filter.lfs.required=true
user.name=bloodstrawberry
user.email=bloodstrawberry@gmail.com
core.autocrlf=input
core.editor=vi

 

list에 나오는 다른 설정도 마찬가지로 수정할 수 있다.

예를 들어 현재 core.editorvi인데, notepad로 변경하는 명령어는 다음과 같다.

git config --global core.editor "notepad++"

 

git config [설정] 으로 개별 결과를 확인할 수 있다.

$ git config user.name
bloodstrawberry

 

git config --unset --global [지우고 싶은 설정] 으로 해당 설정을 지울 수 있다.

$ git config --unset --global user.name
$ git config --list
...
filter.lfs.required=true
user.email=bloodstrawberry@gmail.com
core.autocrlf=input
core.editor=vi

로컬 저장소 생성하기

 

git init 으로 로컬 저장소에 git 저장소를 만들 수 있다.

$ mkdir git-command
$ cd git-command/
$ git init

 

ls 명령어로 .git 폴더가 만들어진 것을 알 수 있다.

$ ls -a
./  ../  .git/

 

git status 로 현재 git 저장소의 상태를 알 수 있다.

방금 저장소만 생성했으므로 아래의 상태가 나오게 된다.

$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)

 

vi로 text 파일을 추가하면 status가 변경되는 것을 알 수 있다.

$ vi text.txt                                                                                           
-> 내용 입력 후 저장.

$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        text.txt

nothing added to commit but untracked files present (use "git add" to track)

파일 commit 하기

 

commit을 하려면 git add [파일 이름] 을 먼저해야 한다. (스테이징, staging)

git add 이후에는 아무 로그도 나오지 않지만 git status 로 확인하면 상태가 변한 것을 알 수 있다.

이후에 git commit -m "commit message"를 입력하면 commit이 된다.

$ git add text.txt
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   text.txt
        
$ git commit -m "commit!!"
git commit -m "commitgit log"
[master (root-commit) ff63286] commitgit log
 1 file changed, 1 insertion(+)
 create mode 100644 text.txt

 

한 번 commit된 파일은 git commit -am "message" 로 한 번에 add + commit이 가능하다.

하지만 새로운 파일은 불가능하며, 아래와 같은 에러가 발생한다.

$ git commit -am "commit2"
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        text2.txt

nothing added to commit but untracked files present (use "git add" to track)

 

commit 전에 git diff [파일 이름] 명령어로 파일에 어떤 변경 사항이 있는지 알 수 있다.

아래의 경우 hello라는 단어를 하나 더 추가했다는 것을 보여준다.

$ git diff text.txt
diff --git a/text.txt b/text.txt
index 0fc15fb..38df49a 100644
--- a/text.txt
+++ b/text.txt
@@ -1 +1 @@
-hello hello hello
+hello hello hello hello

로그 확인하기

 

git log 로 commit 로그를 확인할 수 있으며, 여러 옵션이 존재한다.

git log --oneline 은 한 줄로 요약해준다.

git log --graph 는 그래프를 만들어주고,

두 옵션을 같이 쓰면 git log --graph --oneline 요약된 그래프가 보인다.

$ git log
...
commit ff6328672afea998e583ce9077bb5a57ae851813
Author: bloodstrawberry 
Date:   Tue Mar 28 14:46:03 2023 +0900

    commit
    
$ git log --oneline
ff63286 (HEAD -> master) commitgit log

$ git log --graph
* commit e33ec5df9032faa431fe0512d0d0f11bd0191d1b (HEAD -> master)
| Author: bloodstrawberry 
| Date:   Tue Mar 28 14:52:29 2023 +0900
|
|     commit1
|
* commit 0de5b37b13cddd2805398c327dc8008310ae328b
| Author: bloodstrawberry 
| Date:   Tue Mar 28 14:52:07 2023 +0900
|
|     commit1
|
* commit ff6328672afea998e583ce9077bb5a57ae851813
  Author: bloodstrawberry 
  Date:   Tue Mar 28 14:46:03 2023 +0900

      commitgit log

$ git log --oneline --graph
* e33ec5d (HEAD -> master) commit1
* 0de5b37 commit1
* ff63286 commitgit log

작업 취소하기

 

깃허브 데스크탑 참고

- Commit 취소하기 (Amend, Undo, Revert)

- Revert와 Reset으로 과거 커밋으로 되돌아가기 (+ Git Bash)

- 특정 파일 하나만 되돌리기

 

git checkout 으로 commit하기 전 파일을 되돌릴 수 있다. 

아래는 2개의 파일이 원래대로 돌아간 로그다.

$ git checkout
M       text.txt
M       text2.txt

 

git checkout [파일 이름] 을 하면 해당 파일만 되돌린다.

git checkout text.txt
Updated 1 path from the index

 

git revert [커밋 해시] 를 하면 해당 커밋을 취소하는 상태가 된다.

커밋을 취소할 때 conflict가 일어나면 아래의 로그가 발생한다.

$ git revert 864760c
Auto-merging text.txt
CONFLICT (content): Merge conflict in text.txt
error: could not revert 864760c... commit2
hint: after resolving the conflicts, mark the corrected paths                                           
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

 

실제 파일을 열어보면 conflict가 난 것을 알 수 있다.

 1 <<<<<<< HEAD
  2 hello hello hello hello hello 234
  3 =======
  4 hello
  5 >>>>>>> parent of 864760c... commit2

 

git reset [커밋 해시] 를 하면 해당 commit으로 돌아간다.

$ git log --oneline
45e1980 (HEAD -> master) text2 commit
e33ec5d commit2
0de5b37 commit1
ff63286 commitgit log

 

ff63286을 입력하면 아래와 같은 상태가 된다.

$ git reset ff63286
Unstaged changes after reset:
M       text.txt

$ git log --oneline
ff63286 (HEAD -> master) commitgit log

브랜치 관리하기

 

git branch 로 현재 브랜치를 알 수 있다.

git branch [브랜치 이름] 으로 새 브랜치를 만든다.

git checkout [브랜치 이름] 으로 브랜치를 이동할 수 있다.

$ git branch
* master

$ git branch new_branch

$ git checkout new_branch
Switched to branch 'new_branch'
M       text.txt

$ git branch
  master
* new_branch

 

git branch -d [브랜치 이름] 으로 브랜치를 삭제할 수 있다.

삭제하려는 브랜치에서 checkout한 후, 다른 브랜치에서 삭제를 해야 한다.

병합하지 않은 브랜치를 삭제하려면 -D 옵션을 주면 된다.

$ git branch -d new_branch
Deleted branch new_branch (was ff63286).

수정 중인 파일 숨기기

 

참고

- 일부 파일만 스테이시로 감추기


체리픽 다른 코드 가져오기

 

참고

- 체리픽으로 원하는 commit 가져오기 (git cherry-pick)

- 체리픽으로 변경된 commit만 반영하기 (git cherry-pick --strategy=recursive -X thiers)

반응형

댓글