近期項目需要使用到git進行多人開發。所以對git進行學習。以下是整理的筆記內容
git 配置使用者名稱和郵箱
git config --global user.name "yourname"
git config --global user.email "youremail"
查看配置資訊
git config --list
擷取協助
git help
初始化git倉庫
git init 使用後就會產生git檔案夾用於記錄版本資訊
把檔案加入版本控制 暫存區
git add *.c 可以添加多個檔案
提交檔案到項目
git commit -m "initial project version"
從現有的倉庫複製專案檔
git clone git://github.com/schacon/grit.git 後面是你的項目地址
需要自訂項目名稱時 在後面加項目名稱就OK
檢查當前檔案狀態
git status
查看尚未暫存的檔案修改了哪些內容 此時不加參數
git diff
加了 --stged參數以後,可以查看已暫存的檔案和上次提交的快照之間的差異
移除檔案
rm file 刪除工作區的檔案
git rm 刪除暫存區的檔案
移動檔案
git mv rename.txt rename 給檔案重新命名
查看日誌
git log
git log -p -2 加-p 參數展開顯示每次提交的內容差異 -2顯示最近兩次
git log --stat 顯示簡要的增改行數統計
git log --preetty=online 顯示ID
選項 說明
-p 按補丁格式顯示每個更新之間的差異。
--stat 顯示每次更新的檔案修改統計資訊。
--shortstat 只顯示 --stat 中最後的行數修改添加移除統計。
--name-only 僅在提交資訊後顯示已修改的檔案清單。
--name-status 顯示新增、修改、刪除的檔案清單。
--abbrev-commit 僅顯示 SHA-1 的前幾個字元,而非所有的 40 個字元。
--relative-date 使用較短的相對時間顯示(比如,“2 weeks ago”)。
--graph 顯示 ASCII 圖形表示的分支合并曆史。
--pretty 使用其他格式顯示曆史提交資訊。可用的選項包括 oneline,short,full,fuller 和 format(後跟指定格式)。
撤銷操作
修改最後一次提交
git commit --amend
撤銷修改
git checkout -- readme.txt
查看遠程倉庫
git remote
git remote -v 顯示對應的複製地址
添加遠程倉庫
git remote add [shortname] [url]:
將遠程版本的資料更新後抓取到本地
git fetch 遠程主機名稱
推送資料到遠程倉庫
git push [remote-name] [branch-name] 如git push origin master
查看遠程倉庫資訊
git remote show origin
遠程倉庫的刪除和重新命名
git remote rename pb paul
git remote rm paul
顯示已有標籤
git tag
git tag -l 'v1.4.2.*' 找出1.4.2相關版本並且列出標籤
建立標籤
git tag -a v1.4 -m "my version 1.4"
git show v1.4 查看相應標籤的版本資訊
後期加註標籤
git log --pretty=online
git tag -a v1.2 [id]
另外這是全部的git命令:
$ git help
usage: git [--version] [--help] [-C <path>] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
These are common Git commands used in various situations:
start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one
work on the current change (see also: git help everyday)
add Add file contents to the index
mv Move or rename a file, a directory, or a symlink
reset Reset current HEAD to the specified state
rm Remove files from the working tree and from the index
examine the history and state (see also: git help revisions)
bisect Use binary search to find the commit that introduced a bug
grep Print lines matching a pattern
log Show commit logs
show Show various types of objects
status Show the working tree status
grow, mark and tweak your common history
branch List, create, or delete branches
checkout Switch branches or restore working tree files
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
merge Join two or more development histories together
rebase Reapply commits on top of another base tip
tag Create, list, delete or verify a tag object signed with GPG
collaborate (see also: git help workflows)
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects
'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.