歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Git的常用命令的使用方法和解釋

Git的常用命令的使用方法和解釋

日期:2017/2/28 13:56:23   编辑:Linux教程

我們常用的git命令:

add Add file contents to the index(將文件添加到暫存區)

用法:

保存某個文件到暫緩區:git add 文件名

保存當前路徑的所有文件到暫緩區:git add .(注意,最後是一個點 . )

2. bisect Find by binary search the change that introduced a bug( 使用二分查找快速定位版本的錯誤,bisect雖然由於使用得不多而不廣為人知,但是當你想知道一個本來好的分支從什麼時候開始變壞時,它就能派上用場了。)

用法:

設定前後兩個版本,一個為good, 一個為bad, 使用二分查找中間的版本,進行編譯,看是否出現問題,如果沒有,在該版本與之前設定的bad之間再進行二分;如果有錯誤,則在該版本與之前設定的good之間進行二分

git bisect start/bad/good

3. branch List, create, or delete branches(列出,創建或者刪除分支)

用法:

git branch 不帶參數:列出本地已經存在的分支,並且在當前分支的前面加“*”號標記

git branch -r 列出遠程分支

git branch -a 列出本地分支和遠程分支

git branch 創建一個新的本地分支,需要注意,此處只是創建分支,不進行分支切換

git branch -m | -M oldbranch newbranch 重命名分支,如果newbranch名字分支已經存在,則需要使用-M強制重命名,否則,使用-m進行重命名。

git branch -d | -D branchname 刪除branchname分支

git branch -d -r branchname 刪除遠程branchname分支

4. checkout Checkout a branch or paths to the working tree(從分支或路徑中檢出內容到工作區)

用法:

切換到新分支:git checkout branchName

5. clone Clone a repository into a new directory(克隆一個倉庫到新的目錄)

用法:

下載遠程倉庫到當前路徑:git clone 倉庫的URL

下載遠程倉庫到特定路徑:git clone 倉庫的URL 存放倉庫的路徑

6. commit Record changes to the repository(將變更提交到倉庫)

用法:

提交某個文件到分支:git commit -m ”注釋” 文件名

保存當前路徑的所有文件到分支:git commit -m ”注釋”

7. diff Show changes between commits, commit and working tree, etc(顯示提交,工作目錄的變化)

8. fetch Download objects and refs from another repository(從另外一個倉庫下載對象)

9. grep Print lines matching a pattern(模糊查詢)

10. init Create an empty Git repository or reinitialize an existing one(創建一個空的git倉庫,或者再次初始化一個已經存在的倉庫,生成一個.git目錄,用於維護版本信息)

用法:

在當前路徑初始化倉庫:git init

在其他路徑初始化倉庫:git init 倉庫路徑

11. log Show commit logs(顯示提交日志)

(注意:在Git中的版本號是一個”40位“的哈希值, 而SVN中的版本號是一個遞增的整數)

用法:

查看某個文件的修改日志:git log 文件名

查看當前路徑所有文件的修改日志:git log

用一行的方式查看簡單的日志信息:git log ––pretty=oneline

查看最近的N次修改:git log –N(N是一個整數)

12. merge Join two or more development histories together(將兩個開發過程合並到一起)

13. mv Move or rename a file, a directory, or a symlink(移動,重命名一個文件,目錄,或個鏈接)

14. pull Fetch from and integrate with another repository or a local branch(從另外一個倉庫或者分支獲取和合並到本地倉庫)

15. push Update remote refs along with associated objects(將本地的倉庫更新到遠程倉庫)

16. rebase Forward-port local commits to the updated upstream head( Sequentially regenerate a series of commits so they can be applied directly to the head node,有序重新生成一系列的提交,並肢解用到頭部節點)

17. reset Reset current HEAD to the specified state(恢復版本到一個具體的狀態,建議加上––hard參數,git支持無限次後悔)

用法:

回退到上一個版本:git reset ––hard HEAD^

回退到上上一個版本:git reset ––hard HEAD^^

回退到上N個版本:git reset ––hard HEAD~N(N是一個整數)

回退到任意一個版本:git reset ––hard 版本號(版本號用7位即可)

18. rm Remove files from the working tree and from the index(將文件移除暫存區,刪完之後要進行commit操作,才能同步到版本庫)

用法: git rm 文件名

19. show Show various types of objects(顯示不同的對象)

用法:

20. status Show the working tree status(顯示工作區文件狀態)

用法:

查看某個文件的狀態:git status 文件名

查看當前路徑所有文件的狀態:git status

21. tag Create, list, delete or verify a tag object signed with GPG(創建,列出,刪除或修改標簽)

用法:git tag -a v1.0 -m ‘Version 1.0’

22. help 查看git指令幫助手冊

用法:

查看常用指令: git help

查看其他指令的做法:git help 其他指令

23. config 配置git相關信息(修改的是.git/config文件)

用法:

配置用戶名:git config “user.name” 用戶名(用於跟蹤修改記錄)

配置郵箱:git config “user.email” 郵箱(用於多人開發間的溝通)

查看配置信息:git config –l

編輯配置信息:git config –e(用vim編輯,:wq是退出vim編輯器)

設置指令的別名:git config alias.別名 原指令名稱

設置帶參數指令的別名:git config alias.別名 “原指令名稱 參數”

將此設置應用到整個系統中:git config ––global

24. reflog 查看分支引用紀錄(能夠察看所有的版本號)

Git 教程系列文章

GitHub 使用教程圖文詳解 http://www.linuxidc.com/Linux/2014-09/106230.htm

Git 標簽管理詳解 http://www.linuxidc.com/Linux/2014-09/106231.htm

Git 分支管理詳解 http://www.linuxidc.com/Linux/2014-09/106232.htm

Git 遠程倉庫詳解 http://www.linuxidc.com/Linux/2014-09/106233.htm

Git 本地倉庫(Repository)詳解 http://www.linuxidc.com/Linux/2014-09/106234.htm

Git 服務器搭建與客戶端安裝 http://www.linuxidc.com/Linux/2014-05/101830.htm

Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm

分享實用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm

Ubuntu下Git服務器的搭建與使用指南 http://www.linuxidc.com/Linux/2015-07/120617.htm

Git 的詳細介紹:請點這裡
Git 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved