Skip to content

Commit 7ef39e2

Browse files
committed
git switch and git restore tutorial
1 parent 5642c6c commit 7ef39e2

File tree

1 file changed

+107
-1
lines changed

1 file changed

+107
-1
lines changed

README.md

+107-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,9 @@ git push --force-with-lease
407407

408408
## checkout
409409

410-
git checkout -- file 可以丟棄工作區的修改:
410+
也請參考 [git switch](https://github.com/twtrubiks/Git-Tutorials#git-switch)[git restore](https://github.com/twtrubiks/Git-Tutorials#git-restore).
411+
412+
`git checkout -- file` 可以丟棄工作區的修改:
411413

412414
```cmd
413415
git checkout -- hello.py
@@ -485,6 +487,12 @@ git checkout bug1 為切換到一個名稱為 bug1 的分支底下。
485487
git checkout -b bug1
486488
```
487489

490+
(這邊教大家一個小技巧, 以下這個指令可以快速切換上一個分支, 和 `cd -` 概念一樣:exclamation:)
491+
492+
```cmd
493+
git checkout -
494+
```
495+
488496
我們在 bug1 分支上進行任何修改操作,
489497

490498
然後再把工作成果 ( 補充一下,修改任何內容後請記得使用 git add 指令和 git commit 指令 ) 合併到 master 分支上:
@@ -614,6 +622,94 @@ git branch --remote
614622
git checkout develop
615623
```
616624

625+
## git switch
626+
627+
[Youtube Tutorial - git switch 和 git restore 教學](https://youtu.be/JL_bSOGDR-k)
628+
629+
請先確認目前的 git 版本, 更新方法可參考 [git 更新](https://github.com/twtrubiks/Git-Tutorials#git-%E6%9B%B4%E6%96%B0).
630+
631+
在 git 2.23 版本開始, 增加了 `git switch``git restore`, 這兩個指令主要是
632+
633+
要更清楚的劃分功能, 主要是來代替 `git checkout`.
634+
635+
你其實可以想成 `git checkout` = `git switch` + `git restore`.
636+
637+
官方文件可參考 [git-switch](https://git-scm.com/docs/git-switch)
638+
639+
```cmd
640+
git switch [<options>] (-c|-C) <new-branch> [<start-point>]
641+
```
642+
643+
切換到一個已經存在的 branch (如果該 branch 不存在則指令無效)
644+
645+
```cmd
646+
git switch <new-branch>
647+
```
648+
649+
建立 new-branch 並且切換到 new-branch 分支
650+
651+
```cmd
652+
git switch -c <new-branch>
653+
```
654+
655+
`-c` `--create`
656+
657+
`-C` `--force-create`
658+
659+
依照 commit_id (或前 N 的 commit 點) 建立 new-branch 並且切換到 new-branch 分支
660+
661+
```cmd
662+
git switch -c <new-branch> <commit_id>
663+
git switch -c <new-branch> HEAD~2
664+
```
665+
666+
(這邊教大家一個小技巧, 以下這個指令可以快速切換上一個分支, 和 `cd -` 概念一樣:smile:)
667+
668+
```cmd
669+
git switch -
670+
```
671+
672+
## git restore
673+
674+
[Youtube Tutorial - git switch 和 git restore 教學](https://youtu.be/JL_bSOGDR-k)
675+
676+
請先確認目前的 git 版本, 更新方法可參考 [git 更新](https://github.com/twtrubiks/Git-Tutorials#git-%E6%9B%B4%E6%96%B0).
677+
678+
在 git 2.23 版本開始, 增加了 `git switch``git restore`, 這兩個指令主要是
679+
680+
要更清楚的劃分功能, 主要是來代替 `git checkout`.
681+
682+
你其實可以想成 `git checkout` = `git switch` + `git restore`.
683+
684+
官方文件可參考 [git-restore](https://git-scm.com/docs/git-restore)
685+
686+
以下兩個指令是相同的.
687+
688+
```cmd
689+
git checkout <file>
690+
git restore <file>
691+
```
692+
693+
還原目前資料夾全部的檔案
694+
695+
```cmd
696+
git restore .
697+
```
698+
699+
還原目前資料夾底下結尾是 `*.py` 的全部檔案
700+
701+
```cmd
702+
git restore '*.py'
703+
```
704+
705+
如果你的 `git` 版本比較新, 你應該會發現這個指令你以前好像沒看過:smile:
706+
707+
![alt tag](https://i.imgur.com/IHqfVrn.png)
708+
709+
```cmd
710+
git restore --staged <file>
711+
```
712+
617713
## git pull
618714

619715
通常在開始工作或要 push 之前,會先從遠端抓取分支,
@@ -1727,6 +1823,16 @@ git config alias.stu status
17271823
man git-config
17281824
```
17291825

1826+
### git 更新
1827+
1828+
```cmd
1829+
sudo add-apt-repository ppa:git-core/ppa
1830+
sudo apt-get update
1831+
sudo apt-get install git
1832+
```
1833+
1834+
![alt tag](https://i.imgur.com/WrQNZln.png)
1835+
17301836
## 使用 Git 一次 Push 到多個不同的遠端 ( remote )
17311837

17321838
假如有一天 github 掛了,這樣是不是就不能 work 了,你可能會說本地端還有 ?

0 commit comments

Comments
 (0)