본문 바로가기

dev/git

깃 다루기..

코드를 깃허브에 올리면서 정확히 깃 사용법을 알고 있지 않다.

잘못된 커밋을 하고 푸시를 하는 등의 상황이 빈번하게 일어날 것이기에 공부를 해야겠다.

깃 상태 확인

git status

코드 수정 전

On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean

코드 수정 후

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   lib/repository/movie.dart

no changes added to commit (use "git add" and/or "git commit -a")

커밋할 파일 추가하기 - 스테이징 상태

# 모든 파일을 스테이징시킴
git add .
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   lib/repository/movie.dart

스테이징 취소

git reset HEAD lib/repository/movie.dart
Unstaged changes after reset:
M       lib/repository/movie.dart

커밋 - 수정사항들을 로컬 저장소에 올림

git commit -m "commit test"
[main 658e820] commit test
 1 file changed, 1 insertion(+), 1 deletion(-)
$ 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 log
git log --oneline # 한줄로 보기

한줄로 보기 (우)

커밋 메세지 수정

git commit --amend
git commit --amend -m "git study" # 마지막 커밋 메세지를 바로 수정

vi 편집기로 들어가진다. 제일 상단의 메세지 수정 후 저장하면 된다.(좌)  커밋 메세지 변경 후 로그(우)

커밋 취소

git reset --soft HEAD^ # 마지막 커밋을 스테이징된 상태로 되돌림
git reset HEAD^ # 마지막 커밋을 스테이징 상태 이전으로 되돌림 git reset --mixed HEAD^ 와 동일
git reset --hard HEAD^ # 마지막 커밋을 스태이징 상태 이전으로 되돌리고 코드 수정도 초기화
# ^ 개수로 취소할 커밋 수 결정
# 또는 ~숫자 로 결정

푸시 - 원격 저장소에 올리는 것

git push
# git push <원격 저장소> <브랜치>

로컬의 main 브랜치에서 원격 main 브랜치로 이동함
깃허브에 올라간 것

풀 - 원격 저장소의 내용을 로컬에 가져와서(fetch) 병합(merge)하는 것. 충돌이 일어날 수도 있음

git pull
# git pull <원격 저장소> <브랜치>

푸시 취소 - 로컬 내용을 원격에 덮어쓰니 조심할 것

git reset HEAD^ # 1. 로컬 커밋 취소
git add . # 2. 다시 스테이징
git commit -m "reset" # 3. 커밋
git push origin main -f # 4. 원격 저장소에 강제로 푸시 (-f : force)

덮어씌운 것

 

'dev > git' 카테고리의 다른 글

vscode repository 연결  (0) 2023.03.26
git clone 후 git hub에 push 가 안됨  (0) 2023.02.04
깃 이슈  (0) 2023.01.13