본문으로 바로가기

★ git

category 기타자료 2019. 5. 17. 18:48

* github 기본 브랜치를 master에서 main으로 변경함 (Git 2.28 부터)

 

브랜치 확인

$ git branch

 

 

현재 프로젝트 기본 브랜치 변경

$ git config --local init.defaultBranch main

 

 

전체 기본 브랜치 변경

$ git config --global init.defaultBranch main

 

 

이미 생성한 프로젝트의 디폴트 브랜치 변경하기

$ git checkout -B main
$ git push -u origin main

 

 

 

 

 

git 캐시 삭제 하기

dev-hyun.tistory.com/52

 

[git/github] .ignore 파일이 적용이 안될 때

저번 포스팅에 이어 깃허브에 .ignore파일이 적용이 안되었을 때 수정하는 방법에 대해 알아보기로 해요:) [git/github] 인텔리제이와 git연동 저는 원격저장소에 먼저 저장소를 만들지 않고, 바로 인

dev-hyun.tistory.com

 

 

git 서버에 있는 기존 프로젝트를 복사해서 작업할 경우

 

1. 사용할 디렉토리 생성

 

 

git 설정 확인

$ git config --list

 

 

2. 계정 정보 설정

$ git config --global user.name "이름"

$ git config --global user.email "아이디@examole.com"

or

$ git config --local user.name "이름"

$ git config --local user.email "아이디@examole.com"

 

 

 

3. 원격 저장소 에서 복사

$ git clone <원격 repository 주소> <저장 할 디렉토리>

 

또는 원격 저장소 주소 등록 

$ git remote add origin http://gitlab..... (원격 서버 주소)

 

 

 

 

원격 저장소 확인

$ git remote -v

 

 

git 설정 확인

$ git config --list

 

 

 

$ git branch <branch> // 브랜치 만들기
$ git checkout <branch> //브랜치 이동 (없을경우는 먼저 만들어야 함)

* 로컬에서 브랜치를 만들고 push하면 원격저장소에 브랜치도 생성됨

 

파일 수정등 작업 후

 

$ git add <파일 또는 디렉토리 이름>

 

$ git commit -m “커밋 메시지”

 

$ git push origin master    또는  해당 브랜치

 

 

[오류]

fatal: Not a valid object name: 'master'.
-> 아직 한번도 커밋하지 않아서 발생

 

 

 

 

=========================================

 

 

git init

git 설치 후 원하는 디렉토리에서 git 셋팅

$ git init

Initialized empty Git repository in ...

 

 

git remote

원격 저장소 주소 등록

$ git remote add origin http://gitlab..... (원격 서버 주소)

 

원격 저장소 확인

$ git remote -v

 

이미 원격 저장소가 등록되어 있는 경우

remote origin already exists.

 

$ git remote rm origin    //원격 저장소 삭제

 

 

proxy 설정

 

$ git config --global http.proxy "http://주소 : 포트"

$ git config --global https.proxy "https://주소 : 포트"

 

프로젝트 하나에서만 변경 하러면 local  또는 아예 안쓰거나

$ git config --local http.proxy "http://주소 : 포트"

$ git config --local https.proxy "https://주소 : 포트"

 

 

--global 으로 설정해준 내용은 계정명 아래 path 에서 .gitconfig 으로 확인 가능

 

 

 

 

git config

 

저장소 정보 확인

$ git config --list

 

계정 정보 설정

$ git config --global user.name "이름"

$ git config --global user.email "아이디@examole.com"

 

프로젝트 하나에서만 변경 하러면 local  또는 아예 안쓰거나

$ git config --local user.name "이름"
$ git config --local user.email "아이디@examole.com"

 

편집기 등록

$ git config --global core.editor vi

 

확인

$ git config --list

 

 

 

git clone 예제

 

로컬 저장소를 로컬 저장소로 복사

$ git clone /로컬/저장소/경로

 

원격 저장소를 복사 로컬 저장소에 복사

$ git clone <원격 repository 주소> <저장 할 디렉토리>

 

 

git add

파일 또는 디렉토리를 statge에 올리기 (“*.txt”와 같은 와일드카드가 붙은 지정도 가능)

$ git add *  (전체)

$ git add .   (전체)

$ git add --all   (전체)

$ git add <파일 또는 디렉토리 이름>

$ git add <file-name-1>   <file-name-2>   <file-name-3> 여러 파일 동시에 add

 

add 취소

$ git reset  (add한 파일 전체를 취소)

$ git reset HEAD <file-name-1>    

 

이 명령어로 개별 제외가 안되는 경우

ambiguous argument 'HEAD': unknown revision or path not in the working tree

Use '--' to separate paths from revisions, like this:
'git  <command> [<revision>...] -- [<file>...]'

 

 

 

처음 , 천체를 add 하고 commit 하면 안되는건지..

(만약 프로젝트마다 다른 이름과 이메일 주소를 사용하고 싶으면 --global 옵션을 빼고 명령을 실행한다.)

git commit

커밋

$ git commit -m “커밋 메시지”

 

 

git log

커밋 목록 확인하기

$ git log

 

나가기

$ q

 

 

git reset HEAD

 

add 취소

$ git reset HEAD 

 

커밋 취소

$ git reset HEAD^ // 기본 옵션

$ git reset --mixed HEAD^ // 기본 옵션

$ git reset HEAD~2      // 마지막 2개의 commit을 취소

$ git reset --soft HEAD^      //commit을 취소하고 해당 파일들은 staged 상태로 보존

$ git reset --hard HEAD^     //commit을 취소하고 해당 파일들은 unstaged 상태로 변경

 

 

 

 

파일 이름 변경 할 때

$ git mv [원본 이름] [변경할 이름]

Staged 상태로 변경됨

 

-s 옵션으로 확인하는 경우 R로 표기

$ git status -s

R  myFile -> yourFile

 

 

 

git push

원격 저장소에 올리기

$ git push origin master    또는  해당 브랜치

 

proxy 사용 중이면 아래 오류가 발생할 수 있다

failed to connect to github.com port 443 connection refused

아래 설정을 해준다

$ git config --global http.proxy "http://주소 : 포트"

$ git config --global https.proxy "https://주소 : 포트"

 

프로젝트 하나에서만 변경 하러면 local  또는 아예 안쓰거나

$ git config --local http.proxy "http://주소 : 포트"

$ git config --local https.proxy "https://주소 : 포트"

 

 

--global 으로 설정해준 내용은 계정명 아래 path 에서 .gitconfig 으로 확인 가능

 

원격 저장소 파일 삭제

// 원격 저장소와 로컬 저장소에 있는 파일을 삭제한다.
$ git rm [File Name]

// 원격 저장소에 있는 파일을 삭제한다. 로컬 저장소에 있는 파일은 삭제하지 않는다.
$ git rm --cached [File Name]

 

 

 

프록시 서버 주소 알아보기 (윈도우일떄)

$netstat -ban 

 

 

git branch

git checkout

브랜치 만들기, 이동, 삭제

$ git branch <branch> // 브랜치 만들기
$ git checkout <branch>  //브랜치 이동

$ git checkout -b <branch>   //브랜치 만들고 + 이동

$ git branch -d <branch>  //브랜치 삭제

 

브랜치 목록 확인

$ git branch   //로컬에서

$ git branch -r   //원격 저장소에서

$ git branch -a   //로컬, 원격 모두에서

 

원격저장소 브랜치 가져오기

$ git checkout -t origin/<가져올 브랜치명>

 

브랜치 이름 변경

git branch -m <new name>

 

브랜치 확인 

git branch -v 

 

 

 

 

merge

git pull

원격 저장소의 내용이 로컬 저장소에 병합

$ git pull

 

pull시 오류 발생할 수 있음

추적 정보가 없는경우

There is no tracking information for the current branch. 

$ git branch --set-upstream-to=origin/<원격저장소의 브랜치>  master

 

repo에 파일이 삭제 되고 local엔 존재 하는경우

error: Your local changes to the following files would be overwritten by merge: 특정 파일
Please, commit your changes or stash them before you can merge.

$ git stash

$ git pull

$ git stash pop

 

stash 목록 확인 

$ git stash list

stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log

특정 stash 삭제

$ git stash drop stash@{2}

 

 

또는 특정 파일이 트래킹 중이면 파일을 삭제 하고 필요시 다시 만들어 주면 됨 

 

특정 브랜치를 pull 할 때

$ git pull origin <branch name> 

 

 

 

git diff

다른 브랜치 서로 비교

$ git diff <원래 브랜치> <비교 대상 브랜치>

 

 

 

.gitignore 파일 (버전 관리 예외 처리하기)

root경로에  .gitignore 파일을 생성 (/ 표시는 하위 모든 내용을 무시하겠다는 뜻)

 

log/
.idea/

.

.

.

등등

 

commit 후 push

 

 

 

디렉토리, 파일 삭제

 

로컬 + Git 파일 삭제하기

git rm  [파일명]

git rm  -r [디렉토리명]

 

로컬 파일은 남기고 Git에서만 삭제하기

git rm --cached [파일명]

git rm -r --cached [디렉토리명]

 

commit 후 push

 

 

워킹 디렉토리 안의 추적하고 있지 않은 모든 파일 삭제

$ git clean

 

삭제될 내역을 임시로 확인 하기

$ git clean -n

 

.gitignore 에 명시된 파일은 예외 

 

 

 

 

git merge squash

git stash   (아직 마무리하지 않은 작업을 스택에 잠시 저장할 수 있도록 하는 명령어)

git stash list

https://gmlwjd9405.github.io/2018/05/18/git-stash.html

git rebase

https://git-scm.com/book/ko/v2/Git-%EB%B8%8C%EB%9E%9C%EC%B9%98-Rebase-%ED%95%98%EA%B8%B0

 

git stash pop - 적용