Skip to content

Instantly share code, notes, and snippets.

@onmoving
Last active January 20, 2022 10:18
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save onmoving/3154846 to your computer and use it in GitHub Desktop.
Save onmoving/3154846 to your computer and use it in GitHub Desktop.
git: command summary git 명령 정리

#git commands summary (git 명령 정리)

##git의 환경 확인

$ git config --list
$ git config --global --list

##최초 글로벌 설정

$ git config --global user.name "Your Name"
$ git config --global user.email your.email@example.com
$ git config --global core.editor "sublime_text -w"
$ git config --global merge.tool vimdiff
$ git config --global color.ui true

글로벌 설정은 ~/.gitconfig파일에 저장되므로 이 파일을 직접 편집해도 된다.

password 캐싱하기

With git 1.7.9 or later

$ git config --global credential.helper cache
$ git config --global credential.helper 'cache --timeout=3600'

if below git 1.8 <= ~ < 2.0

$ git config --global push.default simple

##change git diff tool with meld

$ git config --global diff.external meld
$ git diff filename
Usage: 
  meld                        Start with an empty window
  meld <file|dir>             Start a version control comparison
  meld <file> <file> [<file>] Start a 2- or 3-way file comparison
  meld <dir> <dir> [<dir>]    Start a 2- or 3-way directory comparison
  meld <file> <dir>           Start a comparison between file and dir/file

meld: error: too many arguments (wanted 0-3, got 7)
external diff died, stopping at src/transferlistfilterswidget.h.

Meld will open but it will complain about bad parameters. The problem is that Git sends its external diff viewer seven parameters when Meld only needs two of them; two filenames of files to compare. One way to fix it is to write a script to format the parameters before sending them to Meld. Let's do that.

Create a new python script in your home directory (or wherever, it doesn't matter) and call it diff.py.

#!/usr/bin/python

import sys
import os

os.system('meld "%s" "%s"' % (sys.argv[2], sys.argv[5]))

Now we can set Git to perform it's diff on our new script (replacing the path with yours):

$ git config --global diff.external /home/username/bin/diff.py
$ git diff filename

##브랜치 ###브랜치 만들기

$ git checkout -b {branch_name}

###브랜치 삭제

$ git branch -d {branch_name}
$ git branch -D {branch_name} # 강제 삭제

###브랜치 머지

$ git checkout master # master로 전환 후
$ git merge {branch_name_to_merge}

##원격저장소

원격저장소 보기

$ git remote -v

###로컬 브랜치를 새로 만든 후 원격저장소에 해당 브랜치를 push하고자 할 때

$ git push -u origin {branch_name}

원격 저장소에 해당 이름의 브랜치가 없다면 새로 생성된다. -u 옵션은 향후에 수정된 내용을 push 혹은 pull하기 쉽게 할 수 있도록 하는 옵션이다.

###로컬저장소에는 없는 원격브랜치를 가져오고자 할 때

$ git fetch origin
$ git checkout --track origin/{branch_name}

fetch 명령으로 우선 원격 저장소에 변경사항을 가져온다. fetch해야만 새로운 브랜치가 추가 됐는 지를 확인할 수 있다. 두 번째 명령으로 원격 브랜치에 내용을 로컬브랜치로 생성하여 체크아웃한다.

###fetch tags from remote repo 원격저장소에서 태그 만 가져오기

$ git fetch --tags

위의 방법은 로컬태그가 non-fast-forward라는 에러 메시지가 떳을 때 해결할 수 있는 방법임

$ git push --tags
To https://github.com/onmoving/learning-django.git
 ! [rejected]        eof-creating-app -> eof-creating-app (non-fast-forward)
error: failed to push some refs to 'https://github.com/onmoving/learning-django.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

###checkout remote branch 원격 브랜치 체크아웃

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/bookmarks-app
  remotes/origin/master
$ git checkout -b bookmarks-app origin/bookmarks-app
Branch bookmarks-app set up to track remote branch bookmarks-app from origin.
Switched to a new branch 'bookmarks-app'
$ git branch -a
* bookmarks-app
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/bookmarks-app
  remotes/origin/master

###add remote tag 원격 태그 추가

$ git tag local-tag-name
$ git push origin remote-tag-name

push all local tags 로컬의 모든 태그를 한꺼번에 추가하기

$ git push --tags

[example]
$ git tag end-of-start-creating-app
$ git tag -l
end-of-start-creating-app
$ git push origin end-of-start-creating-app 
To https://github.com/onmoving/learning-django.git
 * [new tag]         end-of-start-creating-app -> end-of-start-creating-app

###create tag 로컬 태그 생성 $ git tag tag-name

[example]
$ git tag end-of-start-creating-app
$ git tag -l
end-of-start-creating-app

###delete remote tag 원격 태그 삭제

git tag -d local-tag-name
git push origin :refs/tags/remote-tag-name
 - or -
git push origin :tags/remote-tag-name


[example]
$ git push origin :refs/tags/end-of-start-creating-app
To https://github.com/onmoving/learning-django.git
 - [deleted]         end-of-start-creating-app

###delete remote branch 원격 브랜치 삭제

git push origin :remote-branch-name

[example]
$ git push origin :start-creating-app
To https://github.com/onmoving/learning-django.git
 - [deleted]         start-creating-app

##로깅

###로그 출력하기

한줄로 출력하기

$ git log --pretty=oneline
44babcb1f426d6ce880fd1796405b405e65af45f modified:   gcc_compiler/inside_of_comp
aaf9e94c38e1a0893268be1c3e793577dc36c09a modified:   ../gcc_compiler/compile_opt
c07679cd82b1a5f34cec52bff5e75cebf6a209e3 modified:   .gitignore modified:   READ
f63b7e3e89e88ebc0e9a094c6de2a3a85d011018 modified:   README.md modified:   binut
9e29a8631f6b42b2fb828033604361081c2b0582 modified:   binutils/README.md
a1ec640dc00981d04e12f6478083a94e096ba8f1 modified:   README.md new file:   binut
d9ef89e4954c62bba6bc335e3998cec0332be349 modified:   README.md new file:   binut
8181b452796172b0d231ea725a5ebf6f7990e993 modified:   README.md deleted:    vim/R
39b0dbcb161c241483298524daad26ce62cbb6a4 modified:   getting-started/README.md m
448276fc616ecd20ab48c6e7c8e6850696817b96 modified:   .gitignore modified:   gett
76e440d3c55e1dba89cb83f87fa172ba5c586a94 modified:   getting-started/autotools/R
6608ff81144769cdb42283db45e8ee8808812e11 modified:   getting-started/autotools/R
467536ad88b1c5d77589f5f6b91993e05d30e039 modified:   getting-started/autotools/R
f5241e23c8c71d942684546991bb1edf29a29b96 modified:   getting-started/autotools/R
b0b5dfca56abcffda2f28b97c3c8399ac774ed2e modified:   getting-started/autotools/R
b357978de8ef1fc1512615a52819142d18b15873 modified:   getting-started/autotools/R

##diff

$ git diff filename
$ git diff commit-hash filename

##Installing msysgit for Windows

  • 설치 파일을 다운로드 받는다. http://msysgit.github.io/ 작성 당시 최신 버전: 1.8.1.2

  • cygwin에서 git을 사용하려면 설치 단계에서 cygwin에서 사용할 수 있도록 선택한다. 주의 사항: cygwin에서 제공하는 git 프로그램을 모두 제거한 후 설치한다. 특별한 이유가 있지 않다면 굳이 cygwin에서 msysgit을 사용할 필요는 없으므로 옵션 선택 시 용도를 다시한번 생각 해 보자.

##설정하기

  • 아래와 같은 warning이 나오면 git config --global push.default simple를 실행한다.

    warning: push.default is unset; its implicit value is changing in Git 2.0 from 'matching' to 'simple'. To squelch this message and maintain the current behavior after the default changes, use:

    git config --global push.default matching
    

    To squelch this message and adopt the new behavior now, use:

    git config --global push.default simple
    

###password cache하기

unix에서는 git config --global credential.helper cache로 설정하지만 Windows에서는 적용이 되지 않는다.

결과를 먼저 얘기하면 현재로서는 아래의 방법대로 cygwin의 git를 사용하기 보다는 msysgit의 bash를 사용하고 password cache 방법으로 winstore를 사용 하는 방법이 제일 좋다.

  • git-credential-winstore 다운로드.
  • git-credential-winstore.exe를 C:\Program Files\Git\libexec\git-core (64비트인 경우 C:\Program Files (x86)\Git\libexec\git-core) 디렉터리 아래에 복사한다.
  • credential.helper 항목을 설정한다.

git bash에서 아래 명령을 내린다.

$ git config --global credential.helper winstore

혹은 .gitconfig를 직접 수정하여 아래의 항목을 추가한다.

[credential]
    helper = winstore
  • 만일 위와 같이 했는데도 'credential-cache' is not a git command와 같은 메시지가 출력되면 git config --list | grep credential로 설정을 확인한다. credential.helper=winstore 외에 다른 항목이 있다면 git config --unset credential.cache 명령으로 항목을 제거한다.

위와 같이 winstore를 사용하도록 설정하면 윈도우 제어판의 Windows Credential Manager를 통해서 git credentials을 관리 할 수 있게 된다.

참고: cygwin에 있는 git는 password cache가 되는 경우도 있고 안되는 경우도 있다. 안되는 경우는 cygwin의 fork()에러가 발생하는 경우이다. fork()에러가 발생하는 빈도가 높아 매우 귀찮으므로 사용하지 않는 것이 낫다. vmware를 이용하는 경우에는 git push 시 에러도 없고 password cache도 잘 작동하였다.

##pull 후 modified로 출력되는 문제

gitgui혹은 git status로 출력 시 모든 파일이 수정된 것으로 보여지고, gitgui 혹은 git diff했을 때 출력 메시지가 아래와 같을 경우 filemode설정을 바꾸어 준다.

old mode 100755
new mode 100644

filemode 환경 설정

$ git config core.filemode false

$ git config --global core.filemode false을 수행해도 글로벌 적용이 되지 않는다. 각각의 git repo마다 core.filemode를 설정해 주어야 한다.

###autocrlf 옵션 끄기

git config –global core.autocrlf false

###git 출력 텍스트 색상 적용하기

$ git config --global color.ui true

만일 git config --global color.ui true 만으로 git 출력에 대한 글자 색상이 변하지 않는다면 color.diff, color.status, color.branch의 값을 always로 바꾼다.

$ git config --global color.diff always
$ git config --global color.status always
$ git config --global color.branch always

###msysgit의 .gitconfig

[credential]
	helper = winstore
[user]
	name = [your name]
	email = [your email]
[color]
	ui = true
	diff = always
	status = always
	branch = always
[push]
	default = simple
[core]
	editor = 'C:\\Program Files\\Sublime Text 2\\sublime_text.exe' -w
	filemode = false
	autocrlf = false
[gui]
	encoding = utf-8

##Installing Git on Cygwin

select items below at 'Devel'

  • git
  • git-completion
  • git-gui
  • git-svn (optional)
  • gitk

select item below at 'Net'

  • ca-certificates

참고: cygwin에서 fork() 에러가 발생하는 경우 아래 URL에서 해결 방법이 있기는 하지만 간섭을 받는 요인이 많으므로 윈도우즈 용인 msysgit를 설치해 사용한다. http://cygwin.com/faq/faq-nochunks.html#faq.using.fixing-fork-failures

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment