首頁 php教程 PHP开发 Git 教學之基本操作詳解

Git 教學之基本操作詳解

Dec 16, 2016 pm 03:00 PM

Git 基本操作

Git 的工作就是創建和保存你專案的快照及與之後的快照進行對比。本章將對建立與提交你的專案快照的命令作介紹。

取得與建立專案指令

git init

用 git init 在目錄中建立新的 Git 倉庫。 你可以在任何時候、任何目錄中這麼做,完全是在地化的。
在目錄中執行 git init,就可以建立一個 Git 倉庫了。例如我們建立 runoob 專案:

$ mkdir runoob
$ cd runoob/
$ git init
Initialized empty Git repository in /Users/tianqixin/www/runoob/.git/
# 在 /www/runoob/.git/ 目录初始化空 Git 仓库完毕<BR>
登入後複製

   


現在你可以看到在你的專案中產生了 .git 這個子目錄。 這就是你的 Git 倉庫了,所有有關你的此項目的快照資料都存放在這裡。

ls -a
. .. .git

git clone

使用 git clone 拷貝一個 Git 倉庫到本地,讓自己能夠查看該項目,或者進行修改。

如果你需要與他人合作一個項目,或者想要複製一個項目,看看程式碼,你就可以克隆那個項目。 執行指令:

 git clone [url]

[url] 為你想要複製的項目,就可以了。

例如我們複製 Github 上的專案:

$ git clone git@github.com:schacon/simplegit.git
Cloning into &#39;simplegit&#39;...
remote: Counting objects: 13, done.
remote: Total 13 (delta 0), reused 0 (delta 0), pack-reused 13
Receiving objects: 100% (13/13), done.
Resolving deltas: 100% (2/2), done.
Checking connectivity... done.
登入後複製

   

複製完成後,在目前目錄下會產生一個 simplegit 目錄:
lib


上述操作將複製該項目的全部記錄。

$ ls -a
.    ..    .git   README  Rakefile lib
$ cd .git
$ ls
HEAD    description info    packed-refs
branches  hooks    logs    refs
config   index    objects
登入後複製

   


預設情況下,Git 會依照你提供的 URL 所指示的項目的名稱建立你的本地專案目錄。 通常就是該 URL 最後一個 / 之後的項目名稱。如果你想要一個不一樣的名字, 你可以在該指令後面加上你想要的名稱。


基本快照


Git 的工作就是創建和保存你的專案的快照及與之後的快照進行對比。本章將對建立與提交你的專案的快照的命令作介紹。


git add


git add 指令可將該檔案加入快取,如我們新增以下兩個檔案:

$ touch README
$ touch hello.php
$ ls
README hello.php
$ git status -s
?? README
?? hello.php
$
登入後複製

   

接下來我們執行 git add 指令來新增檔案:

$ git add README hello.php

現在我們再執行 git status,就可以看到這兩個檔案已經加上去了。

$ git status -s
A README
A hello.php
$
登入後複製

   新專案中,新增所有檔案很普遍,我們可以使用 git add . 指令來新增目前專案的所有檔案。
現在我們修改 README 檔案:


$ vim README
<pre class="brush:php;toolbar:false">
<p>在 README 添加以下内容:<b># Runoob Git 测试</b>,然后保存退出。</p>
<p>再执行一下 git status:</p>
$ git status -s
AM README
A hello.php
登入後複製

   


"AM" 狀態的意思是,這個檔案在我們將它加到快取之後又有改動。改變後我們在執行 git add 指令將其加入快取:

$ git add .
$ git status -s
A README
A hello.php
登入後複製

   

當你要將你的修改包含在即將提交的快照裡的時候,需要執行 git add。


git status

git status 以查看在你上次提交之後是否有修改。

我示範該指令的時候加了 -s 參數,以獲得簡短的結果輸出。如果沒有加上這個參數會詳細輸出內容:

$ git status
On branch master
  
Initial commit
  
Changes to be committed:
 (use "git rm --cached <file>..." to unstage)
  
 new file:  README
 new file:  hello.php
登入後複製

   


git diff

執行 git diff 以查看執行 git status 的結果的詳細資訊。

git diff 指令顯示已寫入快取與已修改但尚未寫入快取的變更的差異。 git diff 有兩個主要的應用場景。

1.尚未快取的變更:git diff

2.查看已快取的變更:git diff --cached


3.查看已快取的與未快取的所有變更:git diff HEAD

4.摘要而非整個diff:git diff --stat

在hello.php 檔案中輸入以下內容:

<?php
echo &#39;菜鸟教程:www.runoob.com&#39;;
?>
登入後複製

   


$ git status -s
A README
AM hello.php
$ git diff
diff --git a/hello.php b/hello.php
index e69de29..69b5711 100644
--- a/hello.php
+++ b/hello.php
@@ -0,0 +1,3 @@
+<?php
+echo &#39;菜鸟教程:www.runoob.com&#39;;
+?>
登入後複製

git  


$ git add hello.php 
$ git status -s
A README
A hello.php
$ git diff --cached
diff --git a/README b/README
new file mode 100644
index 0000000..8f87495
--- /dev/null
+++ b/README
@@ -0,0 +1 @@
+# Runoob Git 测试
diff --git a/hello.php b/hello.php
new file mode 100644
index 0000000..69b5711
--- /dev/null
+++ b/hello.php
@@ -0,0 +1,3 @@
+<?php
+echo &#39;菜鸟教程:www.runoob.com&#39;;
+?>
登入後複製

git或寫入快取的改動, 而git diff 一行一行地顯示這些改動具體是啥。

接下來我們來查看下git diff --cached 的執行效果:

$ git add hello.php
$ git status -s
A README
A hello.php
$ $ git commit -m &#39;第一次版本提交&#39;
[master (root-commit) d32cf1f] 第一次版本提交
 2 files changed, 4 insertions(+)
 create mode 100644 README
 create mode 100644 hello.php
登入後複製


git commit

使用快照快取區內容加入倉庫。

Git 為你的每一個提交都記錄你的名字與電子郵件地址,所以第一步需要配置使用者名稱和郵箱地址。

$ git config --global user.name 'runoob'
$ git config --global user.email test@runoob.com


接下來我們寫入緩存,並提交對 hello.php 的所有變更。在首個例子中,我們使用 -m 選項以在命令列中提供提交註解。

# Please enter the commit message for your changes. Lines starting
# with &#39;#&#39; will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#  (use "git reset HEAD <file>..." to unstage)
#
# modified:  hello.php
#
~
~
".git/COMMIT_EDITMSG" 9L, 257C
登入後複製
登入後複製

   


 現在我們已經錄製了快照。如果我們再執行 git status:

$ git status
# On branch master

nothing to commit (working directory clean)


以上输出说明我们在最近一次提交之后,没有做任何改动,是一个"working directory clean:干净的工作目录"。
如果你没有设置 -m 选项,Git 会尝试为你打开一个编辑器以填写提交信息。 如果 Git 在你对它的配置中找不到相关信息,默认会打开 vim。屏幕会像这样:

# Please enter the commit message for your changes. Lines starting
# with &#39;#&#39; will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#  (use "git reset HEAD <file>..." to unstage)
#
# modified:  hello.php
#
~
~
".git/COMMIT_EDITMSG" 9L, 257C
登入後複製
登入後複製


如果你觉得 git add 提交缓存的流程太过繁琐,Git 也允许你用 -a 选项跳过这一步。命令格式如下:

git commit -a

我们先修改 hello.php 文件为以下内容:

<?php
echo &#39;菜鸟教程:www.runoob.com&#39;;
echo &#39;菜鸟教程:www.runoob.com&#39;;
?>
登入後複製

再执行以下命令:


git commit -am &#39;修改 hello.php 文件&#39;
[master 71ee2cb] 修改 hello.php 文件
 1 file changed, 1 insertion(+)
登入後複製

git reset HEAD

git reset HEAD 命令用于取消已缓存的内容。

我们先改动文件 README 文件,内容如下:

# Runoob Git 测试
# 菜鸟教程

hello.php 文件修改为:

<?php
echo &#39;菜鸟教程:www.runoob.com&#39;;
echo &#39;菜鸟教程:www.runoob.com&#39;;
echo &#39;菜鸟教程:www.runoob.com&#39;;
?>
登入後複製

现在两个文件修改后,都提交到了缓存区,我们现在要取消其中一个的缓存,操作如下


$ git status -s
 M README
 M hello.php
$ git add .
$ git status -s
M README
M hello.pp
$ git reset HEAD -- hello.php 
Unstaged changes after reset:
M hello.php
$ git status -s
M README
 M hello.php
登入後複製

现在你执行 git commit,只会将 README 文件的改动提交,而 hello.php 是没有的。

$ git commit -m &#39;修改&#39;
[master f50cfda] 修改
 1 file changed, 1 insertion(+)
$ git status -s
 M hello.php
登入後複製

可以看到 hello.php 文件的修改并为提交。

这时我们可以使用以下命令将 hello.php 的修改提交:

$ git commit -am &#39;修改 hello.php 文件&#39;
[master 760f74d] 修改 hello.php 文件
 1 file changed, 1 insertion(+)
$ git status
On branch master
nothing to commit, working directory clean
登入後複製

简而言之,执行 git reset HEAD 以取消之前 git add 添加,但不希望包含在下一提交快照中的缓存。

git rm

git rm 会将条目从缓存区中移除。这与 git reset HEAD 将条目取消缓存是有区别的。 "取消缓存"的意思就是将缓存区恢复为我们做出修改之前的样子。

默认情况下,git rm file 会将文件从缓存区和你的硬盘中(工作目录)删除。

如果你要在工作目录中留着该文件,可以使用 git rm --cached:

如我们删除 hello.php文件:

$ git rm hello.php 
rm &#39;hello.php&#39;
$ ls
README
登入後複製

不从工作区中删除文件:

$ git rm --cached README 
rm &#39;README&#39;
$ ls
README
登入後複製

git mv

git mv 命令做得所有事情就是 git rm --cached 命令的操作, 重命名磁盘上的文件,然后再执行 git add 把新文件添加到缓存区。

我们先把刚移除的 README 添加回来:

$ git add README

然后对其重名:

$ git mv README README.md
$ ls
README.md
登入後複製

   

 以上就是Git 教程之基本操作详解的内容,更多相关文章请关注PHP中文网(www.php.cn)!


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

git怎麼更新代碼 git怎麼更新代碼 Apr 17, 2025 pm 04:45 PM

更新 git 代碼的步驟:檢出代碼:git clone https://github.com/username/repo.git獲取最新更改:git fetch合併更改:git merge origin/master推送更改(可選):git push origin master

git怎麼下載項目到本地 git怎麼下載項目到本地 Apr 17, 2025 pm 04:36 PM

要通過 Git 下載項目到本地,請按以下步驟操作:安裝 Git。導航到項目目錄。使用以下命令克隆遠程存儲庫:git clone https://github.com/username/repository-name.git

git怎麼合併代碼 git怎麼合併代碼 Apr 17, 2025 pm 04:39 PM

Git 代碼合併過程:拉取最新更改以避免衝突。切換到要合併的分支。發起合併,指定要合併的分支。解決合併衝突(如有)。暫存和提交合併,提供提交消息。

git下載不動怎麼辦 git下載不動怎麼辦 Apr 17, 2025 pm 04:54 PM

解決 Git 下載速度慢時可採取以下步驟:檢查網絡連接,嘗試切換連接方式。優化 Git 配置:增加 POST 緩衝區大小(git config --global http.postBuffer 524288000)、降低低速限制(git config --global http.lowSpeedLimit 1000)。使用 Git 代理(如 git-proxy 或 git-lfs-proxy)。嘗試使用不同的 Git 客戶端(如 Sourcetree 或 Github Desktop)。檢查防火

git commit怎麼用 git commit怎麼用 Apr 17, 2025 pm 03:57 PM

Git Commit 是一種命令,將文件變更記錄到 Git 存儲庫中,以保存項目當前狀態的快照。使用方法如下:添加變更到暫存區域編寫簡潔且信息豐富的提交消息保存並退出提交消息以完成提交可選:為提交添加簽名使用 git log 查看提交內容

如何解決PHP項目中的高效搜索問題? Typesense助你實現! 如何解決PHP項目中的高效搜索問題? Typesense助你實現! Apr 17, 2025 pm 08:15 PM

在開發一個電商網站時,我遇到了一個棘手的問題:如何在大量商品數據中實現高效的搜索功能?傳統的數據庫搜索效率低下,用戶體驗不佳。經過一番研究,我發現了Typesense這個搜索引擎,並通過其官方PHP客戶端typesense/typesense-php解決了這個問題,大大提升了搜索性能。

git怎麼更新本地代碼 git怎麼更新本地代碼 Apr 17, 2025 pm 04:48 PM

如何更新本地 Git 代碼?用 git fetch 從遠程倉庫拉取最新更改。用 git merge origin/&lt;遠程分支名稱&gt; 將遠程變更合併到本地分支。解決因合併產生的衝突。用 git commit -m "Merge branch &lt;遠程分支名稱&gt;" 提交合併更改,應用更新。

git怎麼刪除倉庫 git怎麼刪除倉庫 Apr 17, 2025 pm 04:03 PM

要刪除 Git 倉庫,請執行以下步驟:確認要刪除的倉庫。本地刪除倉庫:使用 rm -rf 命令刪除其文件夾。遠程刪除倉庫:導航到倉庫設置,找到“刪除倉庫”選項,確認操作。

See all articles