이것저것 개발하기!/개발 Tip

[GITHUB]완전 초보 git 명령어 - 쉽게 알아보는 git 사용법

바람이휑 2021. 6. 25. 23:53
반응형

안녕하세요! git 사용법에 대해 알려드리고자 합니다.
git은 많은 기능이 있는데, 그 중 자주 사용하는 것들만 정리해두었습니다.
미리 말씀드리면, 아래 소개해드린 것 이외에 다른 명령어는 git에 조금 익숙해진 다음에 사용하시는 것을 추천드립니다.

본 글의 목적은 git에 대한 아주 기초적인 내용을 설명하고, 그에 대한 부수적인 검색 키워드를 알려드린 것입니다. 자세한 내용은 각각 따로 검색하면 더 자세한 내용을 얻으실 수 있습니다.


git status

  현재 상태보기
  
$ git status
On branch main Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
	deleted: deletedFile.txt
    modified: modifiedFile.txt
    
 Untracked files: (use "git add <file>..." to include in what will be committed)
	newFile.txt 


한줄씩 살펴보면 다음과 같습니다.
On branch main : 현재 작업 중인 로컬 브랜치는 main 브랜치
Your branch is up to date with 'origin/main'. : 로컬 브랜치가 바라보고 있는 원격 브랜치는 origin/main 브랜치(main 브랜치)

Changes not staged for commit : 수정/삭제된 파일 목록입니다.
Changes not staged for commit 목록의 modified와 deleted은 다음과 같습니다.
modified : 수정된 파일입니다. 작업하신 내용의 파일일겁니다.
deleted : 삭제된 파일입니다. 필요없어져 해당 파일을 삭제하셨겠죠?

Untracked files : 추적 중이지 않은 파일 목록 - 로컬에서 생성되었지만, 원격에 올라가거나 올라갈 준비가 되지 않은 파일 목록


git diff

수정/삭제된 파일과 그 내용
$ git diff
diff --git a/deletedFile.txt b/deletedFile.txt
deleted file mode 100644 index e69de29..0000000
diff --git a/modifiedFile.txt b/modifiedFile.txt
index e69de29..c2f99cf 100644
--- a/modifiedFile.txt
+++ b/modifiedFile.txt
@@ -0,0 +1,2 @@
+'It is Modified'
- It is Original 


가장 먼저 볼 내용은 git diff입니다
수정하거나 삭제하신 파일의 내용을 확인하는 것으로 작업하신 내용을 원격에 올리기 전 한번 더 확인하는데 사용합니다.
저는 주로 git diff | grep 으로 print나 console과 같은 내용을 확인합니다.

diff를 통해서 보았을 때,
deletedFile.txt가 삭제되었고(deleted file mode),
modifiedFile.txt에 It is Modified 라는 텍스트가 추가되었고, It is Original 이라는 텍스트가 삭제되었습니다.


git add

수정/추가한 파일 원격에 올릴 준비하기
$ git add deletedFile.txt
$ git add modifiedFile.txt
$ git add newFile.txt
$ git status
On branch main Your branch is up to date with 'origin/main'.
Changes to be committed: (use "git restore --staged <file>..." to unstage)
	deleted: deletedFile.txt
	modified: modifiedFile.txt
 	new file: newFile.txt 


이제 로컬에서 작업한 내용을 원격에 올릴 준비를 해야합니다.
git status에서 보았던 Changes not staged for commit 라는 멘트에서 staged가 나옵니다.
stage는 쉽게 "이제 이것들을 올릴거야~"하고 준비하는 단계입니다.
not staged(unstage)의 파일들을 stage 상태로 만들어주기 위해 git add를 사용합니다.
간단하게 말하면, 편지를 다 쓰고, 편지봉투에 넣는 과정입니다.

git add 파일명으로 삭제/수정/생성한 파일을 입력하면 위와 같이 나옵니다. 꼭 모든 파일을 한번에 할 필요가 없고, 아직 보내기 싫은 것은 안하셔도 됩니다.


git commit -m 'MSG'

작업한 내용에 대한 설명과 보낼 준비 완료
$ git commit -m 'write commit message'
[main 7e1c83c] write commit message
3 files changed, 3 insertions(+) delete mode 100644 deletedFile.txt create mode 100644 newFile.txt 


git add로 추가한 파일들에 대한 설명을 적습니다.
commit은 원격 및 사용 중이 git 저장소에 기록을 남기는 과정입니다.
"제가 지금 이런 이런 작업을 해서 올립니다~"라고 알려주는 것이며, 해당 메시지는 저장소 기록에 남깁니다.
간단하게 말하면, 편지를 담은 편지봉투 바깥에 본인의 이름을 작성하는 정도로 생각하시면 됩니다.

commit 메시지는 꼭 다른 사람이 알아보기 쉽게 하는 것이 좋습니다.
기업 또는 팀마다 다르겠지만, 저의 개인적인 생각은 commit 단위는 작으면 작을수록 좋다입니다. 다음에 알아보기도 쉽고 추적하기도 쉬우니까요!


git commit -a

git add와 commit을 한번에!
$ git commit -a
vi 편집기--------
write commit message
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch main
# Your branch is up to date with 'origin/main'.
#
# Changes to be committed:
# deleted: deletedFile.txt
# modified: modifiedFile.txt
#
# Untracked files:
# newFile.txt #: :wq -------- $ [main 8337537] write commit message 2 files changed, 2 insertions(+) delete mode 100644 deletedFile.txt


git commit -a는 git add와 git commit을 한번에 할 수 있는 명령어입니다.
git commit -a는 정확하게 현재 stage 상태 파일에서 not stage 상태 파일을 add하고, commit 메시지를 작성할 수 있는 vi 편집기 화면을 띄웁니다.
따라서 git commit -a만 한다면, Untracked files에 있는 newFile.txt는 add되지 않는 것입니다. newFile.txt도 함께 처리하고 싶다면 git add newFile.txt로 stage 상태로 만들어준 뒤 git commit -a을 실행하면 됩니다.
참고로 git commit -am 'MSG'를 통해 vi 편집기를 열지 않고 바로 메시지를 작성하는 것도 가능합니다.


git push

작업한 내용 원격에 올리기
$ git status
On branch main Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean

$ git push To https://github.com/*****/gitTest.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/*****/gitTest.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.

$ git pull
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done.
From https://github.com/*****/gitTest 44675ef..9978730 main -> origin/main Merge made by the 'recursive' strategy.
remoteFile | 1 + 1 file changed, 1 insertion(+) create mode 100644 remoteFile 

$ % git push Enumerating objects: 10, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 4 threads Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 652 bytes | 652.00 KiB/s, done. Total 6 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done. To https://github.com/*****/gitTest.git 9978730..4ffce0b main -> main


commit까지 끝냈으면, 이제 원격에 올리면 됩니다.
git push는 커밋된 내용을 원격에 올리는 것입니다. 만약 git push를 하실 때 위에서 처음 git push할 때와 동일한 에러가 발생한다면, git pull을 해주시면 됩니다.정상적으로 git pull이 되었다면 다시 git push를 하시면 성공입니다!

*** 항상 많은 실수를 하고 문제가 발생하는 부분이 이 과정입니다.git pull을 하신 뒤 merge되었다는 창이나 메시지가 뜨면 성공이지만,

혹시나 conflict 와 같은 단어가 보인다면, git push를 바로 하시지 마시고, git 병합 중 충돌 해결 또는 git 충돌 로 검색하셔서 충돌난 부분을 처리하신 뒤에 push하시기 바랍니다.

무언가 이미지가 있어야할 거 같아서 넣는 이미지



쓰고 나니 차라리 하나씩 따로 적는게 좋을걸이라는 생각도 듭니다.ㅠㅠ

반응형