현상관리(svn, git)/설치, 명령어

[git] 로컬, 깃허브 - 기본 커맨드

Mary's log 2024. 8. 31. 00:10

* github.com repository 1개 만들고

1) 여러개의 플젝 최상위 경로에 .git으로 commit 해보기

2) 브랜치를 나눠서 프로젝트 관리 <- 각각 브랜치 변경해서 접근해보기

 

3) 다른 pc, 다른 git 계정으로 접근해서 여러 사용자로 commit, push

4) 특정 파일의 커밋 시점 추출 > 회사에서 해보던거 참고

 



git-bash 설치 필요

https://git-scm.com/download

 

 

Window

ㄴ> 명령 프롬프트에서 X

ㄴ> 전부 디폴트 설정으로 설치 (기본 브랜치가 'master'로 되어있는데 영어권에선 어감 때문에 main 등으로 변경하여 사용)

일단 'master'로 설치

ㄴ> Git Bash 터미널 '관리자 권한' 실행

 

 

Mac

ㄴ> brew 먼저 설치 후 깃설치

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
$ brew -v
zsh: command not found: brew

$ vi ~/.zshrc
$ export PATH=/opt/homebrew/bin:$PATH // ESC - :wq - Enter

// 터미널 강제종료

$ brew -v
Homebrew 4.3.18

$ brew install git

ㄴ> Mac에선 git 디폴트 브랜치가 'main'으로 설정

 

 

* 기본 브랜치 명 확인

나중에, 브랜치명에 맞춰서 push해야함

$ git branch
* main    // Mac
* master  // Window

$ git branch
fatal: not a git repository (or any of the parent directories): .git 
// 기존에 .git이 있는 경로로 이동하거나, 아직 git init 안했으면 하고 나서 조회

 

 


< 초기 설정 >

글로벌 이름,이메일로 등록하면 spring, vs code, x code? 등에서 그 이름,이메일로 커밋푸시된다.

 

 - 글로벌 이메일 등록, 조회

$ git config --global user.email abc123@gmail.com
$ git config --global user.email

 

 - 글로벌 이름 등록, 조회

$ git config --global user.name abc123
$ git config --global user.name

 

 

 

< 깃허브에 올릴 경로로 이동 >

$ cd C:\localTest

 

 

1. github repo가 빈 채로 생성되어있고 & local에서 .git 생성하여
 github repo에 remote 원격을 붙이기

 

- https://github.com 에서 repository 생성 

 

- local 저장소에서 깃 초기화 (.git 폴더 및 하위 파일 생성)

$ git init
Initialized empty Git repository in C:/localProject/.git/

 

* 보안 상의 이유로 숨겨진 .git를 조회하려면

Window :  탐색기\보기\숨긴항목 체크

Mac       :   Finder\ Shift + cmd + .

$ rmdir /?                             // 명령프롬프트 키워드 조회
$ rmdir --help                         // git bash 키워드 조회
$ find ./ -name ".git" | xargs rm -Rf  // 잘못 생성한 .git 폴더 하위파일까지 강제 삭제

 

* 만약 위치를 잘못 init했다면

하위폴더의  .git 폴더를 '싹' 찾아서 전부 제거!!   파일 조심해서 지우기

$ find ./ -name ".git" | xargs rm -Rf

 

 

 

- README.md 파일 생성하고, 깃에 추가

$ echo "# blog" >> README.md
$ git add README.md
warning: in the working copy of 'README.md', LF will be replaced by CRLF the next time Git touches it

 

 

- 깃에 추가할 파일은 해당 경로에 있어야함 

1개나 소량이면 전부 입력할 수 있지만 큰 프로젝트는 한꺼번에 추가할 수도 있다

MacBookPro Desktop % git add textFile.txt        
fatal: (현재 폴더 또는 상위 폴더 중 일부가) 깃 저장소가 아닙니다: .git

MacBookPro Desktop % cd localTest
MacBookPro localTest % git add textFile.txt


$ git add file1 file2 file3  // 파일 3개 직접 입력해서 추가
$ git add .                  // 새로운 파일 추가,수정삭제된 파일 전부 추가
$ git add -A                 // 새로운 파일 추가,수정삭제된 파일 전부 추가
$ git add -u                 //              수정삭제된 파일 전부 추가

 

- git add 했던 파일 내리기 

$ git reset                  // git add 되어서 staging 된 파일 전부 내리기
$ git reset .gitignore       // git add 되어서 staging 되었던 특정 파일 내리기

 

 

 

- git add 되거나 안된 파일 확인

$ git status
On branch master
Your branch is up to date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        testImg.bmp
nothing added to commit but untracked files present (use "git add" to track)


$ git add testFile.txt


$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   testImg.bmp

 

 

 

 

- 커밋메시지 입력하여 커밋

$ git commit -m "first commit"
[master (root-commit) 60abb21] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.md


$ git commit -m "first commit_README_(Windows)"
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        textFile.txt
nothing added to commit but untracked files present (use "git add" to track)

// 현재 경로에 git add되지 않은 'textFile.txt'란 파일이 남아있다

 

커밋할 변경 사항 :
  (스테이지 해제하려면 "git rm --cached <파일>..."을 사용하십시오)

추적하지 않는 파일:
  (커밋할 사항에 포함하려면 "git add <파일>..."을 사용하십시오)

 

 

 

- local 저장소가 -> 깃허브 원격 저장소에 remote원격 추가

origin 뒤의 http 주소 ( https://github.com/abc123/repository이름.git

$ git remote add origin https://github.com/abc123/localProject.git

 

* remote 확인, 제거

한 번 연결하면 remove 하기 전까진 계속 원격 연결되어있음

$ git remote -v
$ git remote remove origin

 

 

- 브랜치 (master, main) 에 푸시

$ git push -u origin master 
remote: Repository not found
...
// github.com에서 repo 생성을 안하고 하면 계속 오류 뜸. 하고 오기.



$ git push -u origin master   // Windows 브랜치 master로 push 성공
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (5/5), 443 bytes | 443.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/abc123/localProject.git
 * [new branch]      master -> master
branch 'master' set up to track 'origin/master'.



$ git push -u origin main   // Mac 브랜치 main으로 push 성공
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 215 bytes | 215.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/abc123/localTest.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

 

* Mac은 혹시 아래 뜨면 이건 그냥 맥북 계정 암호 입력.    git token인 줄?

 

Mac의 second commit 성공 로그는 갑자기 한글;

오브젝트 나열하는 중: 4, 완료.
오브젝트 개수 세는 중: 100% (4/4), 완료.
Delta compression using up to 8 threads
오브젝트 압축하는 중: 100% (2/2), 완료.
오브젝트 쓰는 중: 100% (3/3), 286 bytes | 286.00 KiB/s, 완료.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/abc123/localTest.git
   21e3d6d..e7b7d2c  main -> main
branch 'main' set up to track 'origin/main'.

 


 

* token 생성 시엔, 모든 권한에 체크하고 generate 해주기

 

* github.com에 README.md 체크 없이 생성하면 아래 처럼 커맨드를 알려줌

 

 

 


참고 문헌

더보기