황현동 블로그 개발, 인생, 유우머

160204 파워쉘을 위한 git 명령어 모음

Tags:




  • git 을 항상 sourcetree로만 사용해 왔는데,
  • 쉘에서 특히 파워쉘에서 잘 활용하기 위해서 정리를 해 봤습니다.
    • (그렇다고 파워쉘 특성이 있는 명령어를 사용한것은 많이 없습니다.
  • 실습에는 도스쉘이나 심지어 bash같은 리눅스쉘도 상관없습니다.)

  • Pro Git 책에서 자주 사용하는 예제만 골라서 직접 테스트 해봤습니다.
  • https://play.google.com/store/books/details?id=qaGyAJ0US_kC
  • 좋은 책을 만들어준 scott chacon께 감사드립니다.
  • 무료로 좋은책을 공개해준 google 에도 감사드립니다.


전체 설정 확인

PS C:\project\mygittest> git config --list
core.symlinks=false
core.autocrlf=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
sendemail.smtpserver=/bin/msmtp.exe
diff.astextplain.textconv=astextplain
rebase.autosquash=true
user.name=Hyundong Hwang
user.email=hhd2002@gmail.com
core.excludesfile=C:\Users\Hyundong\Documents\gitignore_global.txt
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
core.hidedotfiles=dotGitOnly
remote.origin.url=https://github.com/HyundongHwang/gittest.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master


클론

PS C:\project> git clone https://github.com/HyundongHwang/gittest.git .\mygittest\
Cloning into '.\mygittest'...
remote: Counting objects: 10, done.
remote: Total 10 (delta 0), reused 0 (delta 0), pack-reused 10
Unpacking objects: 100% (10/10), done.
Checking connectivity... done.


현재 상태보기

PS C:\project\mygittest> git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean


서버상태 보기

PS C:\project\mygittest> git remote -v
origin  git@github.com/HyundongHwang/gittest.git (fetch)
origin  git@github.com/HyundongHwang/gittest.git (push)


변경사항 확인

PS C:\project\mygittest> git diff
diff --git a/dir.txt b/dir.txt
index e864d16..65fa135 100644
Binary files a/dir.txt and b/dir.txt differ

스테이징 파일 삭제하기

PS C:\project\mygittest> git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   dir.txt

PS C:\project\mygittest> git rm --cached .\dir.txt
rm 'dir.txt'
PS C:\project\mygittest> git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    dir.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        dir.txt

PS C:\project\mygittest>


변경사항 stach 이용하여 무시, 그리고 다시 복원

PS C:\project\mygittest> git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   dir2.txt
        new file:   dir3.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   dir.txt



PS C:\project\mygittest> git stash save
Saved working directory and index state WIP on master: 5d4d6d2 test
HEAD is now at 5d4d6d2 test



PS C:\project\mygittest> git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
PS C:\project\mygittest> git stash show
 dir.txt  | Bin 3010 -> 3010 bytes
 dir2.txt | Bin 0 -> 3302 bytes
 dir3.txt | Bin 0 -> 3594 bytes
 3 files changed, 0 insertions(+), 0 deletions(-)



PS C:\project\mygittest> git stash pop
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   dir2.txt
        new file:   dir3.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   dir.txt

Dropped refs/stash@{0} (98328051468c0254db4cd429e0750e920f1af487)



PS C:\project\mygittest> git stash show
No stash found.



PS C:\project\mygittest> git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   dir2.txt
        new file:   dir3.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   dir.txt


로그 3개만 보기

PS C:\project\mygittest> git log -p -3
commit 5d4d6d20eb4f661b047ef18e613881c9807899c4
Author: Hyundong Hwang <hhd2002@gmail.com>
Date:   Wed Jan 27 18:57:12 2016 +0900

    test

diff --git a/abc.html b/abc.html
new file mode 100644


로그 트리로 보기

PS C:\project\mygittest> git log --pretty=format:"%h %s - %an %ar" --graph -10
* 5d4d6d2 test - Hyundong Hwang 3 hours ago
* e0f3057 test - Hyundong Hwang 4 hours ago
* 25dfeab 서브모듈 커밋 - hhd2002 9 months ago
* aea15df submodulechanged - hhd2002 9 months ago
* 899f8cf firstcommit - hhd2002 9 months ago


알리아스 만들기

$ git config --global alias.last 'log -1 HEAD'


git 도움말 콘솔에서 보기 꼼수인데… 절대 없을것 같은 sub명령어를 실행해본다. 모르는 sub명령어인데 이거 써보겠냐며 suggestion이 나온다. –;; ```powershell PS C:\project\mygittest> git commit –abcdefgh error: unknown option `abcdefgh’ usage: git commit [] [--] ...

-q, --quiet           suppress summary after successful commit
-v, --verbose         show diff in commit message template

Commit message options -F, –file read message from file --author override author for commit ...


<br/>

# 브랜치 실습
- 브랜치를 만들고 master에 머지하는 실습

```powershell
PS C:\project\mygittest> git branch
* master

PS C:\project\mygittest> git branch feature/my-cool-func

PS C:\project\mygittest> git branch
  feature/my-cool-func
* master

PS C:\project\mygittest> git checkout feature/my-cool-func
Switched to branch 'feature/my-cool-func'

PS C:\project\mygittest> git branch
* feature/my-cool-func
  master

PS C:\project\mygittest> dir > cool.txt
PS C:\project\mygittest> dir > cool2.txt
PS C:\project\mygittest> git add *

PS C:\project\mygittest> git commit -a -m "cool func"
[feature/my-cool-func 983f398] cool func
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 cool.txt
 create mode 100644 cool2.txt
PS C:\project\mygittest> git push
fatal: The current branch feature/my-cool-func has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin feature/my-cool-func



PS C:\project\mygittest> git push -u
fatal: The current branch feature/my-cool-func has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin feature/my-cool-func


PS C:\project\mygittest> git push -u origin feature/my-cool-func
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 743 bytes | 0 bytes/s, done.
Total 4 (delta 2), reused 0 (delta 0)
To git@github.com:HyundongHwang/gittest.git
 * [new branch]      feature/my-cool-func -> feature/my-cool-func
Branch feature/my-cool-func set up to track remote branch feature/my-cool-func from origin.

PS C:\project\mygittest> git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

PS C:\project\mygittest> dir > current.txt
PS C:\project\mygittest> dir > current2.txt
PS C:\project\mygittest> git add *

PS C:\project\mygittest> git commit -a -m "current change"
[master af8f24d] current change
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 current.txt
 create mode 100644 current2.txt
PS C:\project\mygittest> git push
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 765 bytes | 0 bytes/s, done.
Total 4 (delta 2), reused 0 (delta 0)
To git@github.com:HyundongHwang/gittest.git
   035ce62..af8f24d  master -> master



PS C:\project\mygittest> git merge feature/my-cool-func
Merge made by the 'recursive' strategy.
 cool.txt  | Bin 0 -> 4470 bytes
 cool2.txt | Bin 0 -> 4762 bytes
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 cool.txt
 create mode 100644 cool2.txt



PS C:\project\mygittest> git log --graph
*   commit 92be49b3789a691546180f0b9822951b46084830
|\  Merge: af8f24d 983f398
| | Author: Hyundong Hwang <hhd2002@gmail.com>
| | Date:   Wed Jan 27 23:04:27 2016 +0900
| |
| |     Merge branch 'feature/my-cool-func'
| |
| * commit 983f398f54394f750475bd92ed61f36a552175b5
| | Author: Hyundong Hwang <hhd2002@gmail.com>
| | Date:   Wed Jan 27 23:01:24 2016 +0900
| |
| |     cool func
| |
* | commit af8f24dd06cf1582a770fea7254719dc52506e78
|/  Author: Hyundong Hwang <hhd2002@gmail.com>
|   Date:   Wed Jan 27 23:03:38 2016 +0900
|
|       current change
|
*   commit 035ce62b030f87e8ce3360bebe5477048200766c
|\  Merge: 159900b 54ace0a
| | Author: Hyundong Hwang <hhd2002@gmail.com>
| | Date:   Wed Jan 27 22:40:31 2016 +0900
| |
| |     Merge branch 'feature/abc'
| |
| * commit 54ace0a7b19f04aebc5726255cdd642b4f970982
| | Author: Hyundong Hwang <hhd2002@gmail.com>
| | Date:   Wed Jan 27 22:38:37 2016 +0900
| |
| |     abc *
| |
* | commit 159900b9d9908b0c5e410a75e67fa574821086ce
|/  Author: Hyundong Hwang <hhd2002@gmail.com>
|   Date:   Wed Jan 27 22:39:40 2016 +0900
|
|       master dir
|



PS C:\project\mygittest> git push
Counting objects: 2, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 345 bytes | 0 bytes/s, done.
Total 2 (delta 1), reused 0 (delta 0)
To git@github.com:HyundongHwang/gittest.git
   af8f24d..92be49b  master -> master




submodule 추가

PS C:\project\gittest> git submodule init

PS C:\project\gittest> git submodule add --force git@github.com:HyundongHwang/gitsubmodule.git

PS C:\project\gittest> cd .\gitsubmodule\

PS C:\project\gittest\gitsubmodule> git pull
Already up-to-date.

PS C:\project\gittest\gitsubmodule> git remote -v
origin  https://github.com/HyundongHwang/gitsubmodule (fetch)
origin  https://github.com/HyundongHwang/gitsubmodule (push)

PS C:\project\gittest\gitsubmodule> git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

PS C:\project\gittest\gitsubmodule> cd ..

PS C:\project\gittest> git status
warning: LF will be replaced by CRLF in .gitmodules.
The file will have its original line endings in your working directory.
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .gitmodules
        new file:   gitsubmodule

PS C:\project\gittest> git commit -a -m "add submodule gitsubmodule"
[master warning: LF will be replaced by CRLF in .gitmodules.
The file will have its original line endings in your working directory.
0f839b0] add submodule gitsubmodule
warning: LF will be replaced by CRLF in .gitmodules.
The file will have its original line endings in your working directory.
 2 files changed, 4 insertions(+)
 create mode 160000 gitsubmodule

PS C:\project\gittest> git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 391 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 1 (delta 0)
To git@github.com:HyundongHwang/gittest.git
   7d08691..0f839b0  master -> master


submodule 내부에서 수정후 업데이트

PS C:\project\gittest\gitsubmodule> dir > dir.txt

PS C:\project\gittest\gitsubmodule> git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   dir.txt

no changes added to commit (use "git add" and/or "git commit -a")

PS C:\project\gittest\gitsubmodule> git commit -a -m "modify dir.txt"
[master 5e79c29] modify dir.txt
 1 file changed, 0 insertions(+), 0 deletions(-)

PS C:\project\gittest\gitsubmodule> git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 311 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@github.com:HyundongHwang/gitsubmodule.git
   7be4d74..5e79c29  master -> master

PS C:\project\gittest\gitsubmodule> cd ..

PS C:\project\gittest> git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   gitsubmodule (new commits)

no changes added to commit (use "git add" and/or "git commit -a")

PS C:\project\gittest> git submodule update
Submodule path 'gitsubmodule': checked out 'f57ae8c95b582dca790ef821e86ebd11e0f1ce51'

PS C:\project\gittest> git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean


submodule 외부에서 수정후 업데이트

PS C:\project\gitsubmodule> git diff
diff --git a/dir.txt b/dir.txt
index 03a80a8..1181f77 100644
Binary files a/dir.txt and b/dir.txt differ

PS C:\project\gitsubmodule> git commit -a -m "modify dir.txt"
[master add3b58] modify dir.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
PS C:\project\gitsubmodule> git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 348 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To git@github.com:HyundongHwang/gitsubmodule.git
   5e79c29..add3b58  master -> master

PS C:\project\gitsubmodule> cd ..

PS C:\project> git .\gittest\
git: '.\gittest\' is not a git command. See 'git --help'.

PS C:\project\gittest> cd .\gitsubmodule\

PS C:\project\gittest\gitsubmodule> git status
HEAD detached at f57ae8c
nothing to commit, working directory clean

PS C:\project\gittest\gitsubmodule> git checkout master
Previous HEAD position was f57ae8c... dir2.txt add
Switched to branch 'master'
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)

PS C:\project\gittest\gitsubmodule> git status
On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)
nothing to commit, working directory clean

PS C:\project\gittest\gitsubmodule> git pull
Updating 5e79c29..add3b58
Fast-forward
 dir.txt | Bin 2290 -> 2032 bytes
 1 file changed, 0 insertions(+), 0 deletions(-)

PS C:\project\gittest\gitsubmodule> git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

PS C:\project\gittest\gitsubmodule> cd ..

PS C:\project\gittest> git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   gitsubmodule (new commits)

no changes added to commit (use "git add" and/or "git commit -a")

PS C:\project\gittest> git submodule update
Submodule path 'gitsubmodule': checked out 'f57ae8c95b582dca790ef821e86ebd11e0f1ce51'

PS C:\project\gittest> git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
PS C:\project\gittest>


submodule 제거

PS C:\project\gittest> git rm .\gitsubmodule\
rm 'gitsubmodule'
PS C:\project\gittest> git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   .gitmodules
        deleted:    gitsubmodule

PS C:\project\gittest> git commit -a -m "remove gitsubmodule"
[master 0da6537] remove gitsubmodule
 2 files changed, 4 deletions(-)
 delete mode 160000 gitsubmodule

PS C:\project\gittest> git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 286 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@github.com:HyundongHwang/gittest.git
   0f839b0..0da6537  master -> master

PS C:\project\gittest> git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean