Linux

Git常用指令 & 範例

git 是一個分散式版本控制軟體,最初由林納斯·托瓦茲創作,於2005年以GPL授權條款釋出。最初目的是為了更好地管理Linux核心開發而設計。應注意的是,這與GNU Interactive Tools(一個類似Norton Commander介面的檔案管理器)不同。
git最初的開發動力來自於BitKeeperMonotone。git 只是作為一個可以被其他前端(比如Cogito或Stgit)包裝的後端而開發的,但後來git核心已經成熟到可以獨立地用作版本控制。很多著名的軟體都使用git進行版本控制,其中包括Linux核心、X.Org伺服器OLPC核心等專案的開發流程。

git init

初始化目錄, Git 會開始對這個目錄進行版本控制
ps. 若要移除版本控制,只要刪除 .git 即可
 ~/git-test$ git init
Initialized empty Git repository in /home/xxx/git-test/.git/

git clone

複製你想要的 Repository 檔案到 git 版本控制目錄。
若無法clone, 請參考…[Linux] 如何設定gitlab的ssh key
~/git-test$ git clone git@gitlab.com:gitlab288/busybox-test-20210728.git

Cloning into 'busybox-test-20210728'…
remote: Enumerating objects: 114165, done.
remote: Counting objects: 100% (114165/114165), done.
remote: Compressing objects: 100% (21969/21969), done.
remote: Total 114165 (delta 91475), reused 114165 (delta 91475), pack-reused 0
Receiving objects: 100% (114165/114165), 28.34 MiB | 2.00 MiB/s, done.
Resolving deltas: 100% (91475/91475), done.

git status

查看目錄內檔案的新增修改狀態
~/git-test$ cd busybox-test-20210728/
~/git-test/busybox-test-20210728$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
~/git-test /busybox-test-20210728 $ vi TODO
 Busybox TODO 
 ##FOR TEST 
 Harvest patches from
http://git.openembedded.org/cgit.cgi/openembedded/tree/recipes/busybox/
https://dev.openwrt.org/browser/trunk/package/busybox/patches/ 
...
~/git-test/busybox-test-20210728$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add …" to update what will be committed)
(use "git restore …" to discard changes in working directory)
modified: TODO
 no changes added to commit (use "git add" and/or "git commit -a

git add

將檔案/資料夾加入版本控制git add . 可加入全部資料夾內的檔案/資料夾)
Changes to be committed: 表示有成功加進索引
~/git-test/busybox-test-20210728$ git add TODO
~/git-test/busybox-test-20210728$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged …" to unstage)
modified: TODO

git commit or git commit -m

提交(commit)新增修改的程式碼(透過 -m 參數附上此次修改的說明)
~/git-test/busybox-test-20210728$ git commit -m "for test"
[master cf36e1936] for test
1 file changed, 1 insertion(+), 1 deletion(-)
~/git-test/busybox-test-20210728$ git log
commit cf36e1936debe2a706fe981ee129cc5862d47cae (HEAD -> master)
Author: ubuntu-desktop-20.04 your-email@gmail.com
Date: Wed Jul 28 07:17:33 2021 -0700 

for test

git push

上傳分支到gitlab
~/git-test/busybox-test-20210728$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 315 bytes | 315.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To gitlab.com:gitlab288/busybox-test-20210728.git
37460f5da..cf36e1936 master -> master

Reference

常用 Git 指令介紹

Comments Off on Git常用指令 & 範例