Skip to content

Instantly share code, notes, and snippets.

@jungbin-kim
Last active September 28, 2017 05:21
Show Gist options
  • Save jungbin-kim/10424463615a87d82f6bf191afa4011a to your computer and use it in GitHub Desktop.
Save jungbin-kim/10424463615a87d82f6bf191afa4011a to your computer and use it in GitHub Desktop.

git stash

설명

working directory와 그 index의 현재 상태를 저장을 하지만, 다시 깨끗한 working directory로 돌아가고 싶을 때 git stash를 사용한다.

주로 작업하는 도중 새로 push된 사항이 있을 경우, 이 명령어를 써서 clean 상태로 만든 뒤, pull 받아 코드 상태를 업데이트하고 다시 작업 사항을 반영한다.

사용법

# 현재 상태 임시 저장 git stash = git stash save
$ git stash
$ git stash save

# stash한 리스트 보기
$ git stash list

# stash 내용 자세히 보기
$ git stash show stash {이름}

# stash 다시 work directory에 반영
$ git stash pop

# stash 삭제
$ git stash drop

사례

# https://git-scm.com/docs/git-stash
# 사용 사례 (작업 내용을 commit하지 않고, 다른 브랜치에 적용하기)

# branch1 작업 내용 stashing
$ git stash
# Checkout branch2
$ git checkout branch2
# 가장 나중에 stashing한 내용 branch2에 적용
$ git stash pop

참고

git stash 공식 문서 git stash 사용하기

git fetch를 통해서 remote에 있는 branch가 pull이 안되는 현상.

처음 프로젝트를 clone 하였을 때 branch가 안 딸려오고, master만 있는 문제가 있었음. stackoverflow에서 다음과 같은 해결방법을 찾았다.

# show remote git repository named 'origin' list
$ git ls-remote origin

$ git fetch

만약, 이 방법으로 안될 경우, ls-remote 명령어에 대한 결과를 다시 체크해봐야한다. 만약 branch 이름들이 refs/heads/로 시작할 경우 안 될 수 있음.

그럴 경우, remote를 지우고 다시 add하고 git fetch를 하면 remote에 있는 모든 branch를 받아옴.

$ git remote rm origin
$ git remote add origin {git repository address}
$ git pull origin branch
$ git checkout -b branch origin/branch
# Ref: https://backlogtool.com/git-tutorial/kr/stepup/stepup2_8.html
$ git checkout branch
$ git rebase master # => 이 과정에서 master의 변경사항이 생겨 rebase 중 에러 발생 가능 Ref 참고
$ git checkout master
$ git merge branch
# error message example
falling back to patching base and 3-way merge...
# Init git
$ git add --all
# Initial Commit
$ git commit -m "Initial Commit"
# Add remote repository
$ git remote add origin {git repository address}
# Push to remote server
$ git push -u origin master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment