ansir 님의 블로그

Git Bash를 이용한 Git&GitHub 관리 본문

Git

Git Bash를 이용한 Git&GitHub 관리

ansir 2025. 6. 9. 22:07

테스트, 형상관리( Git )


폴더 생성: mkdir

폴더 생성하기

mkdir git_test


깃 초기화: git init

git init


폴더 내 목록 확인: ls

ls -all


터미널 코드 삭제: clear

clear


파일 생성: echo

echo ‘test’ >> first.txt


Staging Area에 수정된 파일 올리기: git add

git add first.txt - >특정 파일만( first.txt ) 올리기

git add . → 전체 파일 올리기

💡파일 명은 되도록 전부 입력하지 말고 탭으로 자동완성 하기


git 상태 확인: git status

git status


Staging Area에서 내리기: git rm --cached or git restore --staged

git rm --cached first.txt

git restore --staged first.txt


git 권한

git 로컬 권한은 한 번 설정이 되면 계속 유지되기 때문에 자신의 것으로 설정되어 있는지 확인해야 한다.
만약 다른 사람의 권한으로 git을 사용하게 된다면 원격 또는 로컬 저장소에 다른 사람의 정보로 정보가 올라가게 되며, 다른 사람이 자신의 저장소에 접근하게 될 수도 있다.

로컬 권한을 확인하는 방법

로컬 권한을 확인하는 방법은 윈도우의 자격 증명 관리자에서 확인할 수 있다.


권한 설정

git config --global user.name ‘user_name’

git config --global user.email ‘user_email’


Commit

git commit -m “first commit”


로그 확인: git log

git log

author: 올린 사람

data: 올린 날짜

커밋 메시지

를 확인할 수 있다.

--oneline

git log --oneline

깃 로그 간략하기 보기


원격 리포지토리 등록하기: git remote add origin

git remote add origin [ git url ]


원격 리포지토리에 올리기: git push origin

git push origin [ origin branch name ]


특정 커밋 내용 되돌리기: git revert

git revert [ commit hash ]

commit hash 확인하는 방법

git log --oneline에서 commit message 앞의 코드가 commit hash이다.

💡특정 부분을 드래그하고 마우스 휠을 클릭하면 커서 위치에 내용이 붙여넣기가 된다.

특정 커밋 revert를 되돌리기

특정 커밋 a를 revert하여 새로운 커밋 b가 생성되었을 때, revert한 커밋 a를 다시 되돌리려면

새롭게 만들어진 커밋 b를 revert하면 된다.

git revert b_hash


git checkout

checkout은 브랜치도 되지만 커밋으로도 이동 가능하다.

git checkout 1e734e9
Note: switching to '1e734e9'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

git checkout -b [ new_branch_name ]

-b 설정을 추가하면 새로운 브랜치를 추가하고 그곳을 가리키도록 한다.


특정 커밋으로 돌아가서 새로운 브랜치 나누기

  1. 특정 커밋 hash로 checkout: git checkout [ commit hash ]
  2. git checkout -b [new_brach_name ]으로 새로운 브랜치 생성

새로 나눈 브랜치 병합

  1. 작업 내용 Staging&Commit
  2. git push origin [ new_branch_name ]( new_branch_name은 원격 저장소에 올릴 이름임 )
  3. 깃허브로 가서 Pull Request 요청
  4. 병합 후 원격 new_branch_name 브랜치 삭제

원격 저장소의 브랜치 삭제하기

  1. 원격 저장소에서 브랜치 삭제: git push origin --delete branch_name
  2. 로컬에 남아있는 원격 추적 브랜치 제거:
    git fetch --prune → 원격에서 삭제된 브랜치 정보를 로컬에서도 정리해준다.

또는,

수동으로 브랜치 제거:
git branch -dr origin/branch_name
-d: 브랜치 삭제
-r: remote-tracking 브랜치 대상으로

반응형