git之三,git詳解之三
Jun 13, 2016 am 08:44 AMgit之三,git詳解之三
1、status命令與diff命令
前面我們已經(jīng)成功地添加并提交了一個(gè)readme.txt文件,修改readme.txt如下:
echo "Git is a distributed version control system. " > readme.txt echo "Git is free software." >> readme.txt
運(yùn)行g(shù)it status命令看看結(jié)果:
$ git status ... no changes added to commit (use "git add" and/or "git commit -a")
git status命令可以讓我們時(shí)刻掌握倉庫當(dāng)前的狀態(tài),上面顯示,readme.txt被修改過了,但還沒有準(zhǔn)備提交的修改。
git diff這個(gè)命令看看:
$ git diff readme.txt ... -Git is version control system. +Git is a distributed version control system. Git is free software
git diff顧名思義就是查看difference,顯示的格式正是Unix通用的diff格式,可以從上面的命令輸出看到,我們?cè)诘谝恍刑砑恿艘粋€(gè)“distributed”單詞。
readme.txt作了什么修改后,再把它提交到倉庫,提交修改和提交新文件是一樣的兩步,git add和git commit:
$ git add readme.txt $ git commit -m "add distributed"
注意
- 要隨時(shí)掌握工作區(qū)的狀態(tài),使用git status命令。
- 如果git status告訴你有文件被修改過,用git diff可以查看修改內(nèi)容。
2、版本回退
現(xiàn)在,再練習(xí)一次,修改readme.txt文件如下:
echo "Git is a distributed version control system." > readme.txt echo "Git is free software distributed under the GPL." >> readme.txt
我們?cè)俅翁峤灰淮蝦eadme.txt
$ git add readme.txt $ git commit -m "append GPL"
我們現(xiàn)在已經(jīng)提交多次文件,想看看有那些?版本控制系統(tǒng)肯定有某個(gè)命令可以告訴我們歷史記錄,在Git中,我們用git log命令查看:
$ git log commit 3628164fb26d48395383f8f31179f24e0882e1e0 Date: Tue Aug 25 15:11:49 2015 +0000 append GPL commit ea34578d5496d7dd233c827ed32a8cd576c5ee85 Date: Tue Aug 25 14:53:12 2015 +0000 add distributed commit cb926e7ea50ad11b8f9e909c05226233bf755030 Date: Mon Aug 24 17:51:55 2015 +0000 wrote a readme file
git log命令顯示從最近到最遠(yuǎn)的提交日志,我們可以看到3次提交,最近的一次是append GPL,上一次是add distributed,最早的一次是wrote a readme file。commit 36281**2e1e0是commit id(版本號(hào))。如果嫌輸出信息太多,可以使用$ git log --pretty=oneline,此時(shí)你看到的一大串類似3628164...882e1e0的是commit id(版本號(hào))。
每提交一個(gè)新版本,實(shí)際上Git就會(huì)把它們自動(dòng)串成一條時(shí)間線?,F(xiàn)在準(zhǔn)備把readme.txt回退到上一個(gè)版本,也就是“add distributed”的那個(gè)版本,怎么做呢?
首先,Git必須知道當(dāng)前版本是哪個(gè)版本,在Git中,用HEAD表示當(dāng)前版本,也就是最新的提交3628164...882e1e0(注意我的提交ID和你的肯定不一樣),上一個(gè)版本就是HEAD^,上上一個(gè)版本就是HEAD^^,當(dāng)然往上100個(gè)版本寫100個(gè)^比較容易數(shù)不過來,所以寫成HEAD~100。
現(xiàn)在,我們要把當(dāng)前版本“append GPL”回退到上一個(gè)版本“add distributed”,就可以使用git reset命令:
$ git reset --hard HEAD^ HEAD is now at ea34578 add distributed
3、重新恢復(fù)到新版本
接著上節(jié)版本回退,還可以繼續(xù)回退到上一個(gè)版本wrote a readme file,不過我們現(xiàn)在看看版本庫的狀態(tài)git log:
$ git log
最新的那個(gè)版本append GPL已經(jīng)看不到了!好比你從21世紀(jì)坐時(shí)光穿梭機(jī)來到了19世紀(jì),想再回去已經(jīng)回不去了,腫么辦?
只要右側(cè)環(huán)境還在,就可以找到那個(gè)append GPL的commit id是3628164...,于是就可以指定回到未來的某個(gè)版本:
$ git reset --hard 3628164 HEAD is now at 3628164 append GPL
版本號(hào)沒必要寫全,前幾位就可以了,Git會(huì)自動(dòng)去找。當(dāng)然也不能只寫前一兩位,因?yàn)镚it可能會(huì)找到多個(gè)版本號(hào),就無法確定是哪一個(gè)了。
可以查看readme.txt的內(nèi)容 $ cat readme.txt .
Git的版本回退速度非???,因?yàn)镚it在內(nèi)部有個(gè)指向當(dāng)前版本的HEAD指針,當(dāng)你回退版本的時(shí)候,Git僅僅是把HEAD從指向append GPL改為指向add distributed。
4、git reflog命令
現(xiàn)在,你回退到了某個(gè)版本,當(dāng)想恢復(fù)到新版本怎么辦?找不到新版本的commit id怎么辦?
在Git中可以放心下。當(dāng)你用$ git reset --hard HEAD^回退到add distributed版本時(shí),再想恢復(fù)到append GPL,就必須找到append GPL的commit id。Git提供了一個(gè)命令git reflog用來記錄你的每一次命令:
$ git reflog ea34578 HEAD@{0}: reset: moving to HEAD^ 3628164 HEAD@{1}: commit: append GPL ea34578 HEAD@{2}: commit: add distributed cb926e7 HEAD@{3}: commit (initial): wrote a readme file
這樣可以看到,第二行顯示append GPL的commit id是3628164,這樣我們就可以重新找到了。
注意,我們從這兩節(jié)中可以了解到:
- HEAD指向的版本就是當(dāng)前版本,因此,Git允許我們?cè)诎姹镜臍v史之間穿梭,使用命令git reset --hard commit_id。
- 穿梭前,用git log可以查看提交歷史,以便確定要回退到哪個(gè)版本。
- 要重返未來,用git reflog查看命令歷史,以便確定要回到未來的哪個(gè)版本。
5、基本概念
工作區(qū):就是你在電腦里能看到的目錄,learngit文件夾就是一個(gè)工作區(qū),比如我們環(huán)境中當(dāng)前的目錄。
版本庫:工作區(qū)有一個(gè)隱藏目錄.git 這個(gè)不算工作區(qū),而是Git的版本庫。
暫存區(qū):英文叫stage,或index。一般存放在git 目錄下的index文件(.git/index)中,所以我們把暫存區(qū)時(shí)也叫作索引(index).
Git的版本庫里存了很多東西,其中最重要的就是稱為stage(或者叫index)的暫存區(qū),還有Git為我們自動(dòng)創(chuàng)建的第一個(gè)分支master,以及指向master的一個(gè)指針叫HEAD。
我們把文件往Git版本庫里添加的時(shí)候,是分兩步執(zhí)行的:
- 第一步是用git add把文件添加進(jìn)去,實(shí)際上就是把文件修改添加到暫存區(qū);
- 第二步是用git commit提交更改,實(shí)際上就是把暫存區(qū)的所有內(nèi)容提交到當(dāng)前分支。
因?yàn)槲覀儎?chuàng)建Git版本庫時(shí),Git自動(dòng)為我們創(chuàng)建了唯一一個(gè)master分支,所以現(xiàn)在git commit就是往master分支上提交更改。
你可以簡(jiǎn)單理解為,需要提交的文件修改通通放到暫存區(qū),然后一次性提交暫存區(qū)的所有修改。
實(shí)踐理解暫存區(qū)
現(xiàn)在我們對(duì)readme.txt做個(gè)修改,比如追加一行內(nèi)容:
echo "Git has a mutable index called stage." >> readme.txt
然后,在工作區(qū)新增一個(gè)LICENSE文本文件
echo "LICENSE is a new file." > LICENSE
用git status查看一下狀態(tài),Git顯示結(jié)果,readme.txt被修改了,而LICENSE還從來沒有被添加過,所以它的狀態(tài)是Untracked。
現(xiàn)在,使用兩次命令git add,把readme.txt和LICENSE都添加后,用git status再查看一下,通過圖可以理解為:
所以,git add命令實(shí)際上就是把要提交的所有修改放到暫存區(qū)(Stage),然后,執(zhí)行g(shù)it commit就可以一次性把暫存區(qū)的所有修改提交到分支。
$ git commit -m "understand how stage works"
一旦提交后,如果你又沒有對(duì)工作區(qū)做任何修改,用git status查看下,沒有任何內(nèi)容,現(xiàn)在版本庫變成了這樣,暫存區(qū)就沒有任何內(nèi)容了:

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When you open PyCharm for the first time, you should first create a new project and select a virtual environment, and then be familiar with the editor area, toolbar, navigation bar, and status bar. Set up Darcula themes and Consolas fonts, use smart tips and debugging tools to get more efficient, and learn Git integration.

Social security number verification is implemented in PHP through regular expressions and simple logic. 1) Use regular expressions to clean the input and remove non-numeric characters. 2) Check whether the string length is 18 bits. 3) Calculate and verify the check bit to ensure that it matches the last bit of the input.

The steps to effectively use graphical tools to compare the differences in Git versions include: 1. Open GitKraken and load the repository, 2. Select the version to compare, 3. View the differences, and 4. In-depth analysis. Graphical tools such as GitKraken provide intuitive interfaces and rich features to help developers understand the evolution of code more deeply.

The gitstatus command is used to display the status of the working directory and temporary storage area. 1. It will check the current branch, 2. Compare the working directory and the temporary storage area, 3. Compare the temporary storage area and the last commit, 4. Check untracked files to help developers understand the state of the warehouse and ensure that there are no omissions before committing.

To develop a complete Python Web application, follow these steps: 1. Choose the appropriate framework, such as Django or Flask. 2. Integrate databases and use ORMs such as SQLAlchemy. 3. Design the front-end and use Vue or React. 4. Perform the test, use pytest or unittest. 5. Deploy applications, use Docker and platforms such as Heroku or AWS. Through these steps, powerful and efficient web applications can be built.

Verifying an IMEISV string in PHP requires the following steps: 1. Verify the 16-bit numeric format using regular expressions. 2. Verify the validity of the IMEI part through the Luhn algorithm. 3. Check the validity of the software version number. The complete verification process includes format verification, Luhn checking and software version number checking to ensure the validity of IMEISV.

Create tags on remote repository using gitpushorigin, delete tags using gitpushorigin--delete. The specific steps include: 1. Create a local tag: gittagv1.0. 2. Push to remote: gitpushoriginv1.0. 3. Delete local tag: gittag-dv1.0. 4. Delete remote tag: gitpushorigin--deletev1.0.

VSCode solves the problems of multilingual project coding and garbled code including: 1. Ensure that the file is saved with correct encoding and use the "redetection encoding" function; 2. Set the file encoding to UTF-8 and automatically detect the encoding; 3. Control whether to add BOM; 4. Use the "EncodingConverter" plug-in to convert encoding; 5. Use the multiple workspace functions to set encoding for different sub-projects; 6. Optimize performance and ignore unnecessary file monitoring. Through these steps, the coding problem of multilingual projects can be effectively dealt with.
