본문 바로가기

Tools/깃 Git

깃 기초 Git basic

Git 시작하기

  • git 명령어 —h
  • git config —h

해당 명령어에서 쓰는 속성값들을 볼 수 있음

Git init

깃을 시작할 디렉토리에서 git init 명령어로 깃을 초기화하면 .git이라는 숨김폴더가 생성되고 ls -al로 확인할 수 있다. git repository에 있는 다양한 정보들이 들어있다.

기본적으로 main 브랜치가 만들어지나 master로 만들어질 경우 리모트와 충돌이 날 수 있다. https://blog.outsider.ne.kr/1503 참고.

git init #initialise git 
rm -rf .git #delete .git 

Show the working tree status

git status #full status
git status -s #short status

git status(깃 상태 확인)같이 반복적으로 자주 사용하는 명령어를 단축어로 사용하고싶으면 별칭 등록해 git st로 사용 가능

git config —global [alias.st](<http://alias.st>) status

Ignoring Files

Add files that should be ignored in .gitignore in project directory

For example:

# ignore all .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in any directory named build
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory and any of its subdirectories
doc/**/*.pdf

Staging files

git add a.txt #stage a.txt file
git add a.txt b.txt #stage a.txt, b.txt files
git add *.txt #stage all files ends with .txt 
git add * #stage all files except deleted files and files that begin with a dot
git add . #stage everything 

Modifying files

Removing files

rm file.txt #파일 삭제, git에 바로 반영되지 않고 deleted가 생김
git add file.txt #해당 파일을staging area에 추가
git rm file.txt #해당 파일을 working directory와 staging area 모두에서 삭제
git rm --cached file.txt #해당 파일을 staging area에서 삭제
git clean -fd #모든 untracked files 삭제

Moving files

git mv from.txt to.txt
git mv from.text /logs/from.text

Viewing the Staged/Unstaged changes

git status #full status
git status -s #short status
git diff #changes in working directory
git diff --staged #changes in staging area
git diff --cached #same as --staged

Visual Diff Tool

Open .gitconfig and add below

[diff]
    tool = vscode
[difftool "vscode"]
    cmd = code --wait --diff $LOCAL $REMOTE

Run Git Diff tool

git difftool

Commit

git commit #commit stagged files
git commit -m "Commit message" #commit stagged files with commit message
git commit -am "Commit message" #commit all files with commit message

Log · History

history 보기

git log #list of commits 
git log --patch #shows the difference introduced in each commit
git log -p #same as --patch
# git log --state #abbreviated states for each commit
git log --oneline #oneline
git log --oneline --reverse #oneline, from the oldest to the newest 

Formatting

git log --pretty=oneline #same as --oneline
git log --pretty=format:"%h - %an %ar %s" #formatting
git log --pretty=format:"%h %s" --graph #show graph
git log --graph --all --pretty=format:'%C(yellow)[%ad]%C(reset) %C(green)[%h]%C(reset) | %C(white)%s %C(bold red){{%an}}%C(reset) %C(blue)%d%C(reset)' --date=short

$ git config —global alias.hist "log --graph --all --pretty=format:'%C(yellow)[%ad]%C(reset) %C(green)[%h]%C(reset) | %C(white)%s %C(bold red){{%an}}%C(reset) %C(blue)%d%C(reset)' --date=short"

Filtering

git log -2 #shows only the last n commits
git log --author="ellie" 
git log --before="2020-09-29"
git log --after="one week ago"
git log --grep="message" #커밋 메세지에 message가 포함된 커밋 찾기
git log -S="code" #코드 안에 code가 포함된 커밋 찾기
git log -S="code" -p #코드 내용 더 자세히 보기
git log file.txt #file.txt의 로그 보기

History of a file

git log file.txt #history of file.txt
git log --state file.txt #shows statistics
git log --patch file.txt #show the changes

HEAD · Hash code

git log HEAD
git log HEAD~1
git log hash

Viewing a commit

git show HEAD #shows the last commit
git show hash #shows the given commit
git show hash:file.txt 

Comparing

git diff hash1 hash2 #all changes between two commits
git diff hash1 hash2 file.txt #changes to file.txt only

Tagging

특정 커밋을 tag해 놓고 그 부분으로 빠르게 돌아갈 수 있는 북마크같은 기능. 보통 제품 릴리즈 버전을 태그한다.

팀, 회사마다 다른 버전 시스템을 사용할 수 있지만 대부분 semantic versioning 시스템을 사용한다. v1.0.1처럼 메이저 버전, 마이너 업데이트, 픽스 버전 순이다.

메이저 버전은 전체적인 업데이트가 발생했을 때, 마이너 버전은 그 중 조금의 기능이 업데이트 되거나 개선되었을 때, 픽스 버전은 존재하는 기능의 오류를 수정했을 때, 성능이 개선되었을 때 업데이트 한다.

Creating

git tag v1.0.0 #lightweight tag on latest commit
git tag v1.0.0 hash #lightweight tag on the given commit
git show v.0.0 #shows the tag
git tag -a v.1.0.0 -m "message" #annotated tag

Listing

git tag #all the tags
git tag -l "v1.0.*" #search certain tags

Deleting

git tag -d v1.0.0 #delete the given tag

Syncing with Remote

git push origin v1.0.0 #sharing the given tag with remote
git push origin --tags #sharing all the tags
git push origin --delete v1.0.0 #delete a remote tag 

Checking out Tags

git checkout v1.0.0 #checkout certain tag
git checkout -b branchName v1.0.0 #create a new bracnh with the given tag 

'Tools > 깃 Git' 카테고리의 다른 글

git-flow 사용법  (0) 2022.04.04
git-flow란, git-flow 설치  (0) 2022.04.04
깃허브 폴더 화살표 클릭 안됨 remote git repository arrow  (0) 2022.03.05
깃 브랜치 Git Branch  (0) 2021.12.16