Git小記

來源:互聯網
上載者:User

標籤:

Git簡~介

Git是一個分布式版本控制系統,其他的版本控制系統我只用過SVN,但用的時間不長。大家都知道,分布式的好處多多,而且分布式已經包含了集中式的幾乎所有功能。Linus創造Git的傳奇經曆就不再贅述,直接記錄git命令吧! 文章會盡量按照使用git的順序來記錄,不定時的更新,版面可能會較為雜亂。

你的電腦上是否有Git?

windows版本的安裝: Git下載 ,下載之後雙擊安裝即可。

倉庫怎麼建立?

倉庫(repository),即是一個項目,你要對這個項目進行版本管理。使用如下命令來初始化一個倉庫。

[email protected] MINGW64 /e/Weixin$ git init;Initialized empty Git repository in E:/Weixin/.git/[email protected] MINGW64 /e/Weixin (master)
檔案還需要添加?

是的,此時雖然建立了倉庫,但是其實檔案是沒有添加進去的,它是空的。

下面的代碼是經常用到的,可以查看當前的檔案狀態!
[email protected] MINGW64 /e/Weixin (master)$ git status;On branch masterInitial commitUntracked files:  (use "git add ..." to include in what will be committed)        Common/        Home/        index.php        templates/nothing added to commit but untracked files present (use "git add" to track)[email protected] MINGW64 /e/Weixin (master)

可以看到沒有追蹤的檔案/檔案夾,沒有track意味著git沒有對它進行管理。你需要添加這些檔案(其實是添加到了暫存區,等待下一個命令)。

怎麼提交修改?

添加檔案之後就是提交檔案(提交暫存區的檔案到工作區)。

[email protected] MINGW64 /e/Weixin (master)$ git add Home;

查看狀態:

[email protected] MINGW64 /e/Weixin (master)$ git status;On branch masterChanges to be committed:  (use "git reset HEAD ..." to unstage)        new file:   .idea/vcs.xml        modified:   Home/index.html[email protected] MINGW64 /e/Weixin (master)

從上面可以看到,檔案Home/index.html已經被修改(追蹤,加入到了暫存區),最後再執行提交命令,把此狀態添加到目前的版本中。

[email protected] MINGW64 /e/Weixin (master)$ git commit -m "修改了標題";On branch masternothing to commit, working directory clean[email protected] MINGW64 /e/Weixin (master)
版本怎樣穿梭?

先看看git狀態,好像修改了檔案?

[email protected] MINGW64 /e/Weixin (master)$ git status;On branch masterChanges to be committed:  (use "git reset HEAD ..." to unstage)        modified:   test.php[email protected] MINGW64 /e/Weixin (master)
查看一下有什麼變化
$ git diff test.phpdiff --git a/test.php b/test.phpindex 0a02553..790878e 100644--- a/test.php+++ b/test.php@@ -5,4 +5,5 @@  * Date: 2016/4/12  * Time: 10:51  */-echo ‘this is a test.‘;\ No newline at end of file+echo ‘this is a test.‘;+echo ‘hello‘;\ No newline at end of file[email protected] MINGW64 /e/Weixin (master)

可以看到,檔案增加了一句:echo ‘hello‘;。我們執行add、commit命令後,得到了新的版本。

版本回退

我們查看一下此時test.php的代碼

$ cat test.php<?php/** * Created by PhpStorm. * User: creatint * Date: 2016/4/12 * Time: 10:51 */echo ‘this is a test.‘;echo ‘hello‘;[email protected] MINGW64 /e/Weixin (master)
提交日誌是什嗎?

提交日誌清楚地記錄了提交命令,查看他們:

$ git log;commit 52fc500fde16f4b9911e87e42d314a98af718a57Author: creatint <[email protected]>Date:   Tue Apr 12 10:54:13 2016 +0800    修改了test.php檔案commit 578ebf64a3e451a89f144eff37727d5569834158Author: creatint <[email protected]>Date:   Tue Apr 12 10:43:44 2016 +0800    增加了test.php檔案[email protected] MINGW64 /e/Weixin (master)

可以使之顯示在一行

$ git log --pretty=oneline52fc500fde16f4b9911e87e42d314a98af718a57 修改了test.php檔案578ebf64a3e451a89f144eff37727d5569834158 增加了test.php檔案[email protected] MINGW64 /e/Weixin (master)

其中那一長串的十六進位字串是版本的特徵字串,也就是版本的ID [caption id="" align="aligncenter" width="791"] git提交曆史時間軸[/caption] 版本是通過指標指向的,更改版本,其實是修改了指標,而各個版本的記錄仍然是存在的,只是看不到罷了。

$ git reset --hard HEAD^#git reset --hard 578ebf6HEAD is now at 578ebf6 增加了test.php檔案[email protected] MINGW64 /e/Weixin (master)

查看檔案內容,我們發現,已經回到修改檔案之前的狀態了。

$ cat test.php<?php/** * Created by PhpStorm. * User: creatint * Date: 2016/4/12 * Time: 10:51 */echo ‘this is a test.‘;[email protected] MINGW64 /e/Weixin (master)
撤銷功能怎麼實現?
$ git checkout test.php

如果你執行了add或commit,之後對檔案進行了一些修改,過了一小時卻想退回到修改之前的狀態呢?使用上面的代碼吧!

分支管理怎麼弄?

分支(branch)就是樹杈,建立分支時,相當於一次複製(其實只是多了個指標),之後對該分支的修改與之前的分支無關,你也可以把兩個分支合并。一個分支就像一個版本,可以很好的對文檔版本進行控制。 建立代碼倉庫時,系統預設的分支為master,在哪裡查看呢?看下面:

[email protected] MINGW64 /e/Weixin (master)

就句話出現在每一次命令輸出行的上方,其中的master代表當前的分支名。

建立分支branch
$ git checkout -b devSwitched to a new branch ‘dev‘[email protected] MINGW64 /e/Weixin (dev)

git checkout -b dev的等於:

$ git branch dev$ git checkout dev

查看當前分支:

$ git branch* dev  master[email protected] MINGW64 /e/Weixin (master)
合并分支

在dev中做了工作,想要把dev的工作同步到master,就需要合并分支。合并分支時,要合并到哪個分支,就要先切換至哪個分支,然後執行合并命令

[email protected] MINGW64 /e/Weixin <class="variable">(dev)$ git checkout masterSwitched to branch ‘master‘Your branch is up-to-date with ‘origin/master‘.[email protected] MINGW64 /e/Weixin <class="variable">(master)

可以看到已經切換至master。可以使用git diff dev查看dev與master的不同之處。

[email protected] MINGW64 /e/Html5 (master)$ git merge devAlready up-to-date.[email protected] MINGW64 /e/Html5 (master)

這樣,dev的工作便同步到了master中。

先有了遠程倉庫,在本地怎麼管理呢?這裡以開源中國Git為例

我已經在開源中國Git上建立了一個項目 首先,在本地init一個空倉庫

[email protected] MINGW64 /e/Html5$ git init;Initialized empty Git repository in E:/Html5/.git/

然後,添加遠程倉庫

[email protected] MINGW64 /e/Html5 (master)$ git remote add origin https://git.oschina.net/yotaku/Html5.git;

沒有反應?看看下面:

[email protected] MINGW64 /e/Html5 (master)$ git remote remote -v origin  https://git.oschina.net/yotaku/Html5.git (fetch)origin  https://git.oschina.net/yotaku/Html5.git (push)

上面顯示了串連的遠程倉庫

[email protected] MINGW64 /e/Html5 (master)$ git fetch origin master remote: Counting objects: 9, done.remote: Compressing objects: 100% (5/5), done.remote: Total 9 (delta 0), reused 0 (delta 0)Unpacking objects: 100% (9/9), done.From https://git.oschina.net/yotaku/Html5 * branch            master     -> FETCH_HEAD * [new branch]      master     -> origin/master

擷取遠程倉庫master分支的改變內容,但是沒有加入索引。遠程倉庫分支用 origin master表示。 擷取了之後呢?

[email protected] MINGW64 /e/Html5 (master)$ git merge origin/masterAlready up-to-date.[email protected] MINGW64 /e/Html5 (master)

起始,可以運行一個命令即可達到相同的效果。

[email protected] MINGW64 /e/Html5 (master)$ git pull...[email protected] MINGW64 /e/Html5 (master)

上面的命令是擷取遠程倉庫改變內容,然後直接執行merge.

 

原文連結:Git小記

Git小記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.