W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
是目前世界上最先進(jìn)的分布式版本控制系統(tǒng)
git命令 很多人都知道,Linus在1991年創(chuàng)建了開源的Linux,從此,Linux系統(tǒng)不斷發(fā)展,已經(jīng)成為最大的服務(wù)器系統(tǒng)軟件了。
Linus雖然創(chuàng)建了Linux,但Linux的壯大是靠全世界熱心的志愿者參與的,這么多人在世界各地為Linux編寫代碼,那Linux的代碼是如何管理的呢?
事實(shí)是,在2002年以前,世界各地的志愿者把源代碼文件通過diff的方式發(fā)給Linus,然后由Linus本人通過手工方式合并代碼!
你也許會(huì)想,為什么Linus不把Linux代碼放到版本控制系統(tǒng)里呢?不是有CVS、SVN這些免費(fèi)的版本控制系統(tǒng)嗎?因?yàn)長inus堅(jiān)定地反對(duì)CVS和SVN,這些集中式的版本控制系統(tǒng)不但速度慢,而且必須聯(lián)網(wǎng)才能使用。有一些商用的版本控制系統(tǒng),雖然比CVS、SVN好用,但那是付費(fèi)的,和Linux的開源精神不符。
不過,到了2002年,Linux系統(tǒng)已經(jīng)發(fā)展了十年了,代碼庫之大讓Linus很難繼續(xù)通過手工方式管理了,社區(qū)的弟兄們也對(duì)這種方式表達(dá)了強(qiáng)烈不滿,于是Linus選擇了一個(gè)商業(yè)的版本控制系統(tǒng)BitKeeper,BitKeeper的東家BitMover公司出于人道主義精神,授權(quán)Linux社區(qū)免費(fèi)使用這個(gè)版本控制系統(tǒng)。
安定團(tuán)結(jié)的大好局面在2005年就被打破了,原因是Linux社區(qū)牛人聚集,不免沾染了一些梁山好漢的江湖習(xí)氣。開發(fā)Samba的Andrew試圖破解BitKeeper的協(xié)議(這么干的其實(shí)也不只他一個(gè)),被BitMover公司發(fā)現(xiàn)了(監(jiān)控工作做得不錯(cuò)?。?,于是BitMover公司怒了,要收回Linux社區(qū)的免費(fèi)使用權(quán)。
Linus可以向BitMover公司道個(gè)歉,保證以后嚴(yán)格管教弟兄們,嗯,這是不可能的。實(shí)際情況是這樣的:
Linus花了兩周時(shí)間自己用C寫了一個(gè)分布式版本控制系統(tǒng),這就是Git!一個(gè)月之內(nèi),Linux系統(tǒng)的源碼已經(jīng)由Git管理了!牛是怎么定義的呢?大家可以體會(huì)一下。
Git迅速成為最流行的分布式版本控制系統(tǒng),尤其是2008年,GitHub網(wǎng)站上線了,它為開源項(xiàng)目免費(fèi)提供Git存儲(chǔ),無數(shù)開源項(xiàng)目開始遷移至GitHub,包括jQuery,PHP,Ruby等等。
歷史就是這么偶然,如果不是當(dāng)年BitMover公司威脅Linux社區(qū),可能現(xiàn)在我們就沒有免費(fèi)而超級(jí)好用的Git了。
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>]
add 將文件內(nèi)容添加到索引
bisect 通過二進(jìn)制查找引入錯(cuò)誤的更改
branch 列出,創(chuàng)建或刪除分支
checkout 檢查分支或路徑到工作樹
clone 將存儲(chǔ)庫克隆到新目錄中
commit 將更改記錄到存儲(chǔ)庫
diff 顯示提交,提交和工作樹等之間的更改
fetch 從另一個(gè)存儲(chǔ)庫下載對(duì)象和引用
grep 打印匹配圖案的行
init 創(chuàng)建一個(gè)空的Git倉庫或重新初始化一個(gè)現(xiàn)有的
log 顯示提交日志
merge 加入兩個(gè)或更多的開發(fā)歷史
mv 移動(dòng)或重命名文件,目錄或符號(hào)鏈接
pull 從另一個(gè)存儲(chǔ)庫或本地分支獲取并合并
push 更新遠(yuǎn)程引用以及相關(guān)對(duì)象
rebase 轉(zhuǎn)發(fā)端口本地提交到更新的上游頭
reset 將當(dāng)前HEAD復(fù)位到指定狀態(tài)
rm 從工作樹和索引中刪除文件
show 顯示各種類型的對(duì)象
status 顯示工作樹狀態(tài)
tag 創(chuàng)建,列出,刪除或驗(yàn)證使用GPG簽名的標(biāo)簽對(duì)象
init
git init #初始化
status
git status #獲取狀態(tài)
add
git add file # .或*代表全部添加git rm --cached <added_file_to_undo> # 在commit之前撤銷git add操作git reset head # 好像比上面git rm --cached更方便
commit
git commit -m "message" #此處注意亂碼
remote
git remote add origin git@github.com:JSLite/test.git #添加源
push
git push -u origin master # push同事設(shè)置默認(rèn)跟蹤分支
git push origin master
git push -f origin master # 強(qiáng)制推送文件,縮寫 -f(全寫--force)
clone
git clone git://github.com/JSLite/JSLite.js.gitgit clone git://github.com/JSLite/JSLite.js.git mypro #克隆到自定義文件夾git clone [user@]example.com:path/to/repo.git/ #SSH協(xié)議還有另一種寫法。
git clone支持多種協(xié)議,除了HTTP(s)以外,還支持SSH、Git、本地文件協(xié)議等,下面是一些例子。git clone <版本庫的網(wǎng)址> <本地目錄名>
$ git clone http[s]://example.com/path/to/repo.git/
$ git clone ssh://example.com/path/to/repo.git/
$ git clone git://example.com/path/to/repo.git/
$ git clone /opt/git/project.git
$ git clone file:///opt/git/project.git
$ git clone ftp[s]://example.com/path/to/repo.git/
$ git clone rsync://example.com/path/to/repo.git/
首先是配置帳號(hào)信息 ssh -T git@github.com 測(cè)試。
git help config # 獲取幫助信息,查看修改個(gè)人信息的參數(shù)
git config --global user.name "小弟調(diào)調(diào)" # 修改全局名字
git config --global user.email "wowohoo@qq.com" # 修改全局郵箱
git config --list # 查看配置的信息
自動(dòng)轉(zhuǎn)換坑太大,提交到git是自動(dòng)將換行符轉(zhuǎn)換為lf
git config --global core.autocrlf input
這個(gè)密鑰用來跟 github 通信,在本地終端里生成然后上傳到 github
ssh-keygen -t rsa -C 'wowohoo@qq.com' # 生成密鑰
ssh-keygen -t rsa -C "wowohoo@qq.com" -f ~/.ssh/ww_rsa # 指定生成目錄文件名字
ssh -T git@github.com # 測(cè)試是否成功
1.生成指定名字的密鑰
ssh-keygen -t rsa -C "郵箱地址" -f ~/.ssh/jslite_rsa會(huì)生成 jslite_rsa 和 jslite_rsa.pub 這兩個(gè)文件
2.密鑰復(fù)制到托管平臺(tái)上
vim ~/.ssh/jslite_rsa.pub打開公鑰文件 jslite_rsa.pub ,并把內(nèi)容復(fù)制至代碼托管平臺(tái)上
3.修改config文件
vim ~/.ssh/config #修改config文件,如果沒有創(chuàng)建 config
Host jslite.github.com
HostName github.com
User git
IdentityFile ~/.ssh/jslite_rsa
Host work.github.com
HostName github.com
# Port 服務(wù)器open-ssh端口(默認(rèn):22,默認(rèn)時(shí)一般不寫此行)
# PreferredAuthentications 配置登錄時(shí)用什么權(quán)限認(rèn)證
# publickey|password publickey|keyboard-interactive等
User git
IdentityFile ~/.ssh/work_rsa
4.測(cè)試
ssh -T git@jslite.github.com # `@`后面跟上定義的Host
ssh -T work.github.com # 通過別名測(cè)試
ssh -i ~/公鑰文件地址 Host別名 # 如 ssh -i ~/.ssh/work_rsa work.github.com
5.使用
# 原來的寫法
git clone git@github.com:<jslite的用戶名>/learngit.git
# 現(xiàn)在的寫法
git clone git@jslite.github.com:<jslite的用戶名>/learngit.git
git clone git@work.github.com:<work的用戶名>/learngit.git
5.注意
如果你修改了id_rsa的名字,你需要將ssh key添加到SSH agent中,如:
ssh-add ~/.ssh/jslite_rsa
ssh-add -l # 查看所有的key
ssh-add -D # 刪除所有的key
ssh-add -d ~/.ssh/jslite_rsa # 刪除指定的key
$ ssh-keygen -t rsa -P '' -f ~/.ssh/aliyunserver.key
$ ssh-copy-id -i ~/.ssh/aliyunserver.key.pub root@192.168.182.112 # 這里需要輸入密碼一次
編輯 ~/.ssh/config
Host aliyun1
HostName 192.168.182.112
User root
PreferredAuthentications publickey
IdentityFile ~/.ssh/aliyunserver.key
上面配置完了,可以通過命令登錄,不需要輸入IP地址和密碼 ssh aliyun1
git clone https://github.com/username/rep.git
通過上面方式克隆可能需要密碼,解決辦法:進(jìn)入當(dāng)前克隆的項(xiàng)目 vi rep/.git/config 編輯 config, 按照下面方式修改,你就可以提交代碼不用輸入密碼了。
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
- url = https://github.com/username/rep.git
+ url = https://用戶名:密碼@github.com/username/rep.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
1. 增加3個(gè)遠(yuǎn)程庫地址
git remote add origin https://github.com/JSLite/JSLite.git
git remote set-url --add origin https://gitlab.com/wang/JSLite.js.git
git remote set-url --add origin https://oschina.net/wang/JSLite.js.git
2. 刪除其中一個(gè) set-url 地址
usage: git remote set-url [--push] <name> <newurl> [<oldurl>]
or: git remote set-url --add <name> <newurl>
or: git remote set-url --delete <name> <url>
git remote set-url --delete origin https://oschina.net/wang/JSLite.js.git
3.推送代碼
git push origin master
git push -f origin master # 強(qiáng)制推送
4.拉代碼
只能拉取 origin 里的一個(gè)url地址,這個(gè)fetch-url默認(rèn)為你添加的到 origin的第一個(gè)地址
git pull origin master
git pull --all # 獲取遠(yuǎn)程所有內(nèi)容包括tag
git pull origin next:master # 取回origin主機(jī)的next分支,與本地的master分支合并
git pull origin next # 遠(yuǎn)程分支是與當(dāng)前分支合并
# 上面一條命令等同于下面兩條命令
git fetch origin
git merge origin/next
如果遠(yuǎn)程主機(jī)刪除了某個(gè)分支,默認(rèn)情況下,git pull 不會(huì)在拉取遠(yuǎn)程分支的時(shí)候,刪除對(duì)應(yīng)的本地分支。這是為了防止,由于其他人操作了遠(yuǎn)程主機(jī),導(dǎo)致git pull不知不覺刪除了本地分支。但是,你可以改變這個(gè)行為,加上參數(shù) -p 就會(huì)在本地刪除遠(yuǎn)程已經(jīng)刪除的分支。
$ git pull -p
# 等同于下面的命令
$ git fetch --prune origin
$ git fetch -p
5.更改pull
只需要更改config文件里,那三個(gè)url的順序即可,fetch-url會(huì)直接對(duì)應(yīng)排行第一的那個(gè)utl連接。
git remote remove origin # 刪除該遠(yuǎn)程路徑
git remote add origin git@jslite.github.com:JSLite/JSLite.git # 添加遠(yuǎn)程路徑
git reset --hard HEAD~1 # 撤銷一條記錄
git push -f origin HEAD:master # 同步到遠(yuǎn)程倉庫
git reset --hard FETCH_HEAD # FETCH_HEAD表示上一次成功git pull之后形成的commit點(diǎn)。然后git pull
git reset --hard FETCH_HEAD 出現(xiàn)錯(cuò)誤
git pull
You are not currently on a branch, so I cannot use any
'branch.<branchname>.merge' in your configuration file.
Please specify which remote branch you want to use on the command
line and try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) FOR details.
解決方法:
git checkout -b temp # 新建+切換到temp分支
git checkout master
# 如果有的修改以及加入暫存區(qū)的話
git reset --hard
# 還原所有修改,不會(huì)刪除新增的文件
git checkout .
# 下面命令會(huì)刪除新增的文件
git clean -xdf
通過存儲(chǔ)暫存區(qū)stash,在刪除暫存區(qū)的方法放棄本地修改。
git stash && git stash drop
git revert HEAD~1 # 撤銷一條記錄 會(huì)彈出 commit 編輯
git push # 提交回滾
git reset --hard <hash>
# 例如 git reset --hard a3hd73r
# --hard代表丟棄工作區(qū)的修改,讓工作區(qū)與版本代碼一模一樣,與之對(duì)應(yīng),
# --soft參數(shù)代表保留工作區(qū)的修改。
# 實(shí)質(zhì)是新建了一個(gè)與原來完全相反的commit,抵消了原來commit的效果
git revert <commit-hash>
# 這種方式新建的分支(gh-pages)是沒有 commit 記錄的
git checkout --orphan gh-pages
# 刪除新建的gh-pages分支原本的內(nèi)容,如果不刪除,提交將作為當(dāng)前分支的第一個(gè)commit
git rm -rf .
# 查看一下狀態(tài) 有可能上面一條命令,沒有刪除還沒有提交的的文件
git state
# 這個(gè)命令,將最近4個(gè)commit合并為1個(gè),HEAD代表當(dāng)前版本。
# 將進(jìn)入VIM界面,你可以修改提交信息。
git rebase -i HEAD~4
# 可以看到其中分為兩個(gè)部分,上方未注釋的部分是填寫要執(zhí)行的指令,
# 而下方注釋的部分則是指令的提示說明。指令部分中由前方的命令名稱、commit hash 和 commit message 組成
# 當(dāng)前我們只要知道 pick 和 squash 這兩個(gè)命令即可。
# --> pick 的意思是要會(huì)執(zhí)行這個(gè) commit
# --> squash 的意思是這個(gè) commit 會(huì)被合并到前一個(gè)commit
# 我們將 需要保留的 這個(gè) commit 前方的命令改成 squash 或 s,然后輸入:wq以保存并退出
# 這是我們會(huì)看到 commit message 的編輯界面
# 其中, 非注釋部分就是兩次的 commit message, 你要做的就是將這兩個(gè)修改成新的 commit message。
#
# 輸入wq保存并推出, 再次輸入git log查看 commit 歷史信息,你會(huì)發(fā)現(xiàn)這兩個(gè) commit 已經(jīng)合并了。
# 將修改強(qiáng)制推送到前端
git push -f origin master
git commit --amend
# amend只能修改沒有提交到線上的,最后一次commit記錄
git rebase -i HEAD~3
# 表示要修改當(dāng)前版本的倒數(shù)第三次狀態(tài)
# 將要更改的記錄行首單詞 pick 改為 edit
pick 96dc3f9 doc: Update quick-start.md
pick f1cce8a test(Transition):Add transition test (#47)
pick 6293516 feat(Divider): Add Divider component.
# Rebase eeb03a4..6293516 onto eeb03a4 (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
保存并退出,會(huì)彈出下面提示
# You can amend the commit now, with
#
# git commit --amend
#
# Once you are satisfied with your changes, run
#
# git rebase --continue
# 通過這條命令進(jìn)入編輯頁面更改commit,保存退出
git commit --amend
# 保存退出確認(rèn)修改,繼續(xù)執(zhí)行 rebase,
git rebase --continue
# 如果修改多條記錄反復(fù)執(zhí)行上面兩條命令直到完成所有修改
# 最后,確保別人沒有提交進(jìn)行push,最好不要加 -f 強(qiáng)制推送
git push -f origin master
echo node_modules/ >> .gitignore
這個(gè)功能在Github上可以玩兒,Gitlab上特別老的版本不能玩兒哦,那么如何跟隨著commit關(guān)閉一個(gè)issue呢? 在confirm merge的時(shí)候可以使用一下命令來關(guān)閉相關(guān)issue:
fixes #xxx、 fixed #xxx、 fix #xxx、 closes #xxx、 close #xxx、 closed #xxx、
設(shè)置添加多個(gè)遠(yuǎn)程倉庫地址。
在同步之前,需要?jiǎng)?chuàng)建一個(gè)遠(yuǎn)程點(diǎn)指向上游倉庫(repo).如果你已經(jīng)派生了一個(gè)原始倉庫,可以按照如下方法做。
$ git remote -v
# List the current remotes (列出當(dāng)前遠(yuǎn)程倉庫)
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)
$ git remote add upstream https://github.com/otheruser/repo.git
# Set a new remote (設(shè)置一個(gè)新的遠(yuǎn)程倉庫)
$ git remote -v
# Verify new remote (驗(yàn)證新的原唱倉庫)
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)
# upstream https://github.com/otheruser/repo.git (fetch)
# upstream https://github.com/otheruser/repo.git (push)
同步更新倉庫內(nèi)容
同步上游倉庫到你的倉庫需要執(zhí)行兩步:首先你需要從遠(yuǎn)程拉去,之后你需要合并你希望的分支到你的本地副本分支。從上游的存儲(chǔ)庫中提取分支以及各自的提交內(nèi)容。 master 將被存儲(chǔ)在本地分支機(jī)構(gòu) upstream/master
git fetch upstream
# remote: Counting objects: 75, done.
# remote: Compressing objects: 100% (53/53), done.
# remote: Total 62 (delta 27), reused 44 (delta 9)
# Unpacking objects: 100% (62/62), done.
# From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
# * [new branch] master -> upstream/master
檢查你的 fork's 本地 master 分支
git checkout master
# Switched to branch 'master'
合并來自 upstream/master 的更改到本地 master 分支上。 這使你的前 fork's master 分支與上游資源庫同步,而不會(huì)丟失你本地修改。
git merge upstream/master
# Updating a422352..5fdff0f
# Fast-forward
# README | 9 -------
# README.md | 7 ++++++
# 2 files changed, 7 insertions(+), 9 deletions(-)
# delete mode 100644 README
# create mode 100644 README.md
1.克隆倉庫
注意參數(shù),這個(gè)不是普通的clone,clone下來的倉庫并不能參與開發(fā)
git clone --bare https://github.com/user/repo.git
cd repo.git
2.命令行中運(yùn)行代碼
OLD_EMAIL原來的郵箱CORRECT_NAME更正的名字CORRECT_EMAIL更正的郵箱
將下面代碼復(fù)制放到命令行中執(zhí)行
git filter-branch -f --env-filter '
OLD_EMAIL="wowohoo@qq.com"
CORRECT_NAME="小弟調(diào)調(diào)"
CORRECT_EMAIL="更正的郵箱@qq.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags
執(zhí)行過程
Rewrite 160d4df2689ff6df3820563bfd13b5f1fb9ba832 (479/508) (16 seconds passed, remaining 0 predicted)
Ref 'refs/heads/dev' was rewritten
Ref 'refs/heads/master' was rewritten
3.同步到遠(yuǎn)程倉庫
同步到push遠(yuǎn)程git倉庫
git push --force --tags origin 'refs/heads/*'
我還遇到了如下面錯(cuò)誤,lab默認(rèn)給master分支加了保護(hù),不允許強(qiáng)制覆蓋。Project(項(xiàng)目)->Setting->Repository 菜單下面的Protected branches把master的保護(hù)去掉就可以了。修改完之后,建議把master的保護(hù)再加回來,畢竟強(qiáng)推不是件好事。
remote: GitLab: You are not allowed to force push code to a protected branch on this project.
當(dāng)上面的push 不上去的時(shí)候,先 git pull 確保最新代碼
git pull --allow-unrelated-histories
# 或者指定分枝
git pull origin master --allow-unrelated-histories
git log --pretty=oneline 文件名 # 列出文件的所有改動(dòng)歷史
git show c178bf49 # 某次的改動(dòng)的修改記錄
git log -p c178bf49 # 某次的改動(dòng)的修改記錄
git blame 文件名 # 顯示文件的每一行是在那個(gè)版本最后修改。
git whatchanged 文件名 # 顯示某個(gè)文件的每個(gè)版本提交信息:提交日期,提交人員,版本號(hào),提交備注(沒有修改細(xì)節(jié))
git config --global alias.st status
git config --global alias.br branch
git config --global alias.co checkout
git config --global alias.ci commit
配置好后再輸入git命令的時(shí)候就不用再輸入一大段了,例如我們要查看狀態(tài),只需:
git st
git config --global core.quotepath false
git init #初始化
git status #獲取狀態(tài)
git add file # .或*代表全部添加git rm --cached <added_file_to_undo> # 在commit之前撤銷git add操作git reset head # 好像比上面git rm --cached更方便
git commit -m "message" #此處注意亂碼
git remote add origin git@github.com:JSLite/test.git #添加源
git push -u origin master # push同事設(shè)置默認(rèn)跟蹤分支
git push origin master
git push -f origin master # 強(qiáng)制推送文件,縮寫 -f(全寫--force)
git clone git://github.com/JSLite/JSLite.js.gitgit clone git://github.com/JSLite/JSLite.js.git mypro #克隆到自定義文件夾git clone [user@]example.com:path/to/repo.git/ #SSH協(xié)議還有另一種寫法。
git clone支持多種協(xié)議,除了HTTP(s)以外,還支持SSH、Git、本地文件協(xié)議等,下面是一些例子。git clone <版本庫的網(wǎng)址> <本地目錄名>
$ git clone http[s]://example.com/path/to/repo.git/
$ git clone ssh://example.com/path/to/repo.git/
$ git clone git://example.com/path/to/repo.git/
$ git clone /opt/git/project.git
$ git clone file:///opt/git/project.git
$ git clone ftp[s]://example.com/path/to/repo.git/
$ git clone rsync://example.com/path/to/repo.git/
git help config # 獲取幫助信息
git add * # 跟蹤新文件
git add -u [path] # 添加[指定路徑下]已跟蹤文件
rm *&git rm * # 移除文件
git rm -f * # 移除文件
git rm --cached * # 取消跟蹤
git mv file_from file_to # 重命名跟蹤文件
git log # 查看提交記錄
git commit #提交更新
git commit -m 'message' #提交說明
git commit -a #跳過使用暫存區(qū)域,把所有已經(jīng)跟蹤過的文件暫存起來一并提交
git commit --amend #修改最后一次提交
git commit log #查看所有提交,包括沒有push的commit
git commit -m "#133" #關(guān)聯(lián)issue 任意位置帶上# 符號(hào)加上issue號(hào)碼
git commit -m "fix #133" commit關(guān)閉issue
git commit -m '概要描述'$'\n\n''1.詳細(xì)描述'$'\n''2.詳細(xì)描述' #提交簡(jiǎn)要描述和詳細(xì)描述
git reset HEAD * # 取消已經(jīng)暫存的文件
git reset --mixed HEAD * # 同上
git reset --soft HEAD * # 重置到指定狀態(tài),不會(huì)修改索引區(qū)和工作樹
git reset --hard HEAD * # 重置到指定狀態(tài),會(huì)修改索引區(qū)和工作樹
git reset -- files * # 重置index區(qū)文件
git revert HEAD # 撤銷前一次操作
git revert HEAD~ # 撤銷前前一次操作
git revert commit # 撤銷指定操作
git checkout -- file # 取消對(duì)文件的修改(從暫存區(qū)——覆蓋worktree file)
git checkout branch|tag|commit -- file_name # 從倉庫取出file覆蓋當(dāng)前分支
git checkout HEAD~1 [文件] # 將會(huì)更新 working directory 去匹配某次 commit
git checkout -- . # 從暫存區(qū)取出文件覆蓋工作區(qū)
git checkout -b gh-pages 0c304c9 # 這個(gè)表示 從當(dāng)前分支 commit 哈希值為 0c304c9 的節(jié)點(diǎn),分一個(gè)新的分支gh-pages出來,并切換到 gh-pages
git diff file # 查看指定文件的差異
git diff --stat # 查看簡(jiǎn)單的diff結(jié)果
git diff # 比較Worktree和Index之間的差異
git diff --cached # 比較Index和HEAD之間的差異
git diff HEAD # 比較Worktree和HEAD之間的差異
git diff branch # 比較Worktree和branch之間的差異
git diff branch1 branch2 # 比較兩次分支之間的差異
git diff commit commit # 比較兩次提交之間的差異
git diff master..test # 上面這條命令只顯示兩個(gè)分支間的差異
git diff master...test # 你想找出‘master’,‘test’的共有 父分支和'test'分支之間的差異,你用3個(gè)‘.'來取代前面的兩個(gè)'.'
git stash # 將工作區(qū)現(xiàn)場(chǎng)(已跟蹤文件)儲(chǔ)藏起來,等以后恢復(fù)后繼續(xù)工作。
git stash list # 查看保存的工作現(xiàn)場(chǎng)
git stash apply # 恢復(fù)工作現(xiàn)場(chǎng)
git stash drop # 刪除stash內(nèi)容
git stash pop # 恢復(fù)的同時(shí)直接刪除stash內(nèi)容
git stash apply stash@{0} # 恢復(fù)指定的工作現(xiàn)場(chǎng),當(dāng)你保存了不只一份工作現(xiàn)場(chǎng)時(shí)。
git merge --squash test # 合并壓縮,將test上的commit壓縮為一條
git cherry-pick commit # 揀選合并,將commit合并到當(dāng)前分支
git cherry-pick -n commit # 揀選多個(gè)提交,合并完后可以繼續(xù)揀選下一個(gè)提交
git rebase master # 將master分之上超前的提交,變基到當(dāng)前分支
git rebase --onto master 169a6 # 限制回滾范圍,rebase當(dāng)前分支從169a6以后的提交
git rebase --interactive # 交互模式,修改commit
git rebase --continue # 處理完沖突繼續(xù)合并
git rebase --skip # 跳過
git rebase --abort # 取消合并
git push origin :branchName # 刪除遠(yuǎn)程分支
git push origin --delete new # 刪除遠(yuǎn)程分支new
git branch -d branchName # 刪除本地分支,強(qiáng)制刪除用-D
git branch -d test # 刪除本地test分支
git branch -D test # 強(qiáng)制刪除本地test分支
git remote prune origin # 遠(yuǎn)程刪除了,本地還能看到遠(yuǎn)程存在,這條命令刪除遠(yuǎn)程不存在的分支
git push -u origin branchName # 提交分支到遠(yuǎn)程origin主機(jī)中
git fetch -p #拉取遠(yuǎn)程分支時(shí),自動(dòng)清理 遠(yuǎn)程分支已刪除,本地還存在的對(duì)應(yīng)同名分支。
git merge branchName # 合并分支 - 將分支branchName和當(dāng)前所在分支合并
git merge origin/master # 在本地分支上合并遠(yuǎn)程分支。
git rebase origin/master # 在本地分支上合并遠(yuǎn)程分支。
git merge test # 將test分支合并到當(dāng)前分支
git branch -m old new #重命名分支
git branch # 列出本地分支
git branch -r # 列出遠(yuǎn)端分支
git branch -a # 列出所有分支
git branch -v # 查看各個(gè)分支最后一個(gè)提交對(duì)象的信息
git branch --merge # 查看已經(jīng)合并到當(dāng)前分支的分支
git branch --no-merge # 查看為合并到當(dāng)前分支的分支
git remote show origin # 可以查看remote地址,遠(yuǎn)程分支
git branch test # 新建test分支
git branch newBrach 3defc69 # 指定哈希3defc69,新建分支名字為newBrach
git checkout -b newBrach origin/master # 取回遠(yuǎn)程主機(jī)的更新以后,在它的基礎(chǔ)上創(chuàng)建一個(gè)新的分支
git checkout -b newBrach 3defc69 # 以哈希值3defc69,新建 newBrach 分支,并切換到該分支
git branch --set-upstream dev origin/dev # 將本地dev分支與遠(yuǎn)程dev分支之間建立鏈接
git branch --set-upstream master origin/next # 手動(dòng)建立追蹤關(guān)系
git checkout test # 切換到test分支
git checkout -b test # 新建+切換到test分支
git checkout -b test dev # 基于dev新建test分支,并切換
git fetch <遠(yuǎn)程主機(jī)名> <分支名> # fetch取回所有分支(branch)的更新
git fetch origin remotebranch[:localbranch] # 從遠(yuǎn)端拉去分支[到本地指定分支]
git merge origin/branch # 合并遠(yuǎn)端上指定分支
git pull origin remotebranch:localbranch # 拉去遠(yuǎn)端分支到本地分支
git push origin branch # 將當(dāng)前分支,推送到遠(yuǎn)端上指定分支
git push origin localbranch:remotebranch # 推送本地指定分支,到遠(yuǎn)端上指定分支
git push origin :remotebranch # 刪除遠(yuǎn)端指定分支
git checkout -b [--track] test origin/dev # 基于遠(yuǎn)端dev分支,新建本地test分支[同時(shí)設(shè)置跟蹤]
克隆項(xiàng)目同時(shí)克隆submodule
git clone https://github.com/jaywcjlove/handbook.git --depth=1 --recurse-submodules
克隆項(xiàng)目,之后再手動(dòng)克隆 submodule 子項(xiàng)目
git submodule add --force '倉庫地址' '路徑'
# 其中,倉庫地址是指子模塊倉庫地址,路徑指將子模塊放置在當(dāng)前工程下的路徑。
# 注意:路徑不能以 / 結(jié)尾(會(huì)造成修改不生效)、不能是現(xiàn)有工程已有的目錄(不能順利 Clone)
git submodule init # 初始化submodule
git submodule update # 更新submodule(必須在根目錄執(zhí)行命令)
git submodule update --init --recursive # 下載的工程帶有submodule
當(dāng)使用git clone下來的工程中帶有submodule時(shí),初始的時(shí)候,submodule的內(nèi)容并不會(huì)自動(dòng)下載下來的,此時(shí),只需執(zhí)行如下命令:
git submodule foreach git pull # submodule 里有其他的 submodule 一次更新
git submodule foreach git pull origin master # submodule更新
git submodule foreach --recursive git submodule init
git submodule foreach --recursive git submodule update
git rm -rf node_modules/
git是一個(gè)分布式代碼管理工具,所以可以支持多個(gè)倉庫,在git里,服務(wù)器上的倉庫在本地稱之為remote。個(gè)人開發(fā)時(shí),多源用的可能不多,但多源其實(shí)非常有用。
git remote add origin1 git@github.com:yanhaijing/data.js.git
git remote # 顯示全部源
git remote -v # 顯示全部源+詳細(xì)信息
git remote rename origin1 origin2 # 重命名
git remote rm origin # 刪除
git remote show origin # 查看指定源的全部信息
當(dāng)開發(fā)到一定階段時(shí),給程序打標(biāo)簽是非常棒的功能。
git tag -a v0.1 -m 'my version 1.4' # 新建帶注釋標(biāo)簽
git push origin --tags # 一次性推送所有分支
git push origin v1.5 # 推送單個(gè)tag到orgin源上
git tag -v v1.4.2.1 # 驗(yàn)證標(biāo)簽,驗(yàn)證已經(jīng)簽署的標(biāo)簽
git show v1.5 # 看到對(duì)應(yīng)的 GPG 簽
git tag # 列出現(xiàn)有標(biāo)簽
git tag v0gi.1 # 新建標(biāo)簽
git checkout tagname # 切換到標(biāo)簽
git tag -d v0.1 # 刪除標(biāo)簽
git push origin :refs/tags/v0.1 # 刪除遠(yuǎn)程標(biāo)簽
git pull --all # 獲取遠(yuǎn)程所有內(nèi)容包括tag
git --git-dir='<絕對(duì)地址>/.git' describe --tags HEAD # 查看本地版本信息
git config format.pretty oneline #顯示歷史記錄時(shí),每個(gè)提交的信息只顯示一行
git config color.ui true #彩色的 git 輸出
git log #查看最近的提交日志
git log --pretty=oneline #單行顯示提交日志
git log --graph --pretty=oneline --abbrev-commit
git log -num #顯示第幾條log(倒數(shù))
git reflog #查看所有分支的所有操作記錄
git log --since=1.day #一天內(nèi)的提交;你可以給出各種時(shí)間格式,比如說具體的某一天(“2008-01-15”),或者是多久以前(“2 years 1 day 3 minutes ago”)。
git log --pretty="%h - %s" --author=自己的名字 #查看自己的日志
git log -p -2 #展開兩次更新顯示每次提交的內(nèi)容差異
git log --stat #要快速瀏覽其他協(xié)作者提交的更新都作了哪些改動(dòng)
git log --pretty=format:"%h - %an, %ar : %s"#定制要顯示的記錄格式
git log --pretty=format:'%h : %s' --date-order --graph # 拓?fù)漤樞蛘故?
git log --pretty=format:'%h : %s - %ad' --date=short #日期YYYY-MM-DD顯示
git log <last tag> HEAD --pretty=format:%s # 只顯示commit
git config --global format.pretty '%h : %s - %ad' --date=short #日期YYYY-MM-DD顯示 寫入全局配置
選項(xiàng) | 說明 | 選項(xiàng) | 說明 |
---|---|---|---|
%H | 提交對(duì)象(commit)的完整哈希字串 | %ad | 作者修訂日期(可以用 -date= 選項(xiàng)定制格式) |
%h | 提交對(duì)象的簡(jiǎn)短哈希字串 | %ar | 作者修訂日期,按多久以前的方式顯示 |
%T | 樹對(duì)象(tree)的完整哈希字串 | %cn | 提交者(committer)的名字 |
%t | 樹對(duì)象的簡(jiǎn)短哈希字串 | %ce | 提交者的電子郵件地址 |
%P | 父對(duì)象(parent)的完整哈希字串 | %cd | 提交日期 |
%p | 父對(duì)象的簡(jiǎn)短哈希字串 | %cr | 提交日期,按多久以前的方式顯示 |
%an | 作者(author)的名字 | %s | 提交說明 |
%ae | 作者的電子郵件地址 | - | - |
Pretty Formats
git commit --amend # 改變最近一次提交
git rebase -i HEAD~3 # 修改最近三次的提交說明,或者其中任意一次
git commit --amend # 保存好了,這些指示很明確地告訴了你該干什么
git rebase --continue # 修改提交說明,退出編輯器。
pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file
改成
pick 310154e updated README formatting and added blame
pick f7f3f6d changed my name a bit
cd ..
rm -rf repo.git
Github官方教程
git help * # 獲取命令的幫助信息
git status # 獲取當(dāng)前的狀態(tài),非常有用,因?yàn)間it會(huì)提示接下來的能做的操作
1. git fatal: protocol error: bad line length character: No s
解決辦法:更換remote地址為 http/https 的
2. The requested URL returned error: 403 Forbidden while accessing
解決github push錯(cuò)誤的辦法:
#vim 編輯器打開 當(dāng)前項(xiàng)目中的config文件
vim .git/config
#修改
[remote "origin"]
url = https://github.com/jaywcjlove/example.git
#為下面代碼
[remote "origin"]
url = https://jaywcjlove@github.com/jaywcjlove/example.git
3. git status 顯示中文問題
在查看狀態(tài)的時(shí)候 git status 如果是中文就顯示下面的情況
\344\272\247\345\223\201\351\234\200\346\261\202
解決這個(gè)問題方法是:
git config --global core.quotepath false
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: