Git 常用命令和教程
常用命令:http://hi.baidu.com/sunboy_2050/item/ffab7396672895d11a49dfcc
精簡(jiǎn)教程:http://lugir.com/git-basic.html
Git 常用命令
Git?是一個(gè)很強(qiáng)大的分布式版本控制系統(tǒng)。它不但適用于管理大型開源軟件的源代碼,管理私人的文檔和源代碼也有很多優(yōu)勢(shì)。
?
Git常用操作命令:
1)?遠(yuǎn)程倉庫相關(guān)命令
檢出倉庫:$ git clone git://github.com/jquery/jquery.git
查看遠(yuǎn)程倉庫:$ git remote -v
添加遠(yuǎn)程倉庫:$ git remote add [name] [url]
刪除遠(yuǎn)程倉庫:$ git remote rm [name]
修改遠(yuǎn)程倉庫:$ git remote set-url --push?[name]?[newUrl]
拉取遠(yuǎn)程倉庫:$ git pull [remoteName] [localBranchName]
推送遠(yuǎn)程倉庫:$ git push [remoteName] [localBranchName]
?
*如果想把本地的某個(gè)分支test提交到遠(yuǎn)程倉庫,并作為遠(yuǎn)程倉庫的master分支,或者作為另外一個(gè)名叫test的分支,如下:
$git push origin test:master ? ? ? ? //?提交本地test分支作為遠(yuǎn)程的master分支
$git push origin test:test ? ? ? ? ? ? ?//?提交本地test分支作為遠(yuǎn)程的test分支
?
2)分支(branch)操作相關(guān)命令
查看本地分支:$ git branch
查看遠(yuǎn)程分支:$ git branch -r
創(chuàng)建本地分支:$ git branch [name] ----注意新分支創(chuàng)建后不會(huì)自動(dòng)切換為當(dāng)前分支
切換分支:$ git checkout [name]
創(chuàng)建新分支并立即切換到新分支:$ git checkout -b [name]
刪除分支:$ git branch -d [name] ---- -d選項(xiàng)只能刪除已經(jīng)參與了合并的分支,對(duì)于未有合并的分支是無法刪除的。如果想強(qiáng)制刪除一個(gè)分支,可以使用-D選項(xiàng)
合并分支:$ git merge [name] ----將名稱為[name]的分支與當(dāng)前分支合并
創(chuàng)建遠(yuǎn)程分支(本地分支push到遠(yuǎn)程):$ git push origin [name]
刪除遠(yuǎn)程分支:$ git push origin :heads/[name]?或?$ gitpush origin :[name]?
?
*創(chuàng)建空的分支:(執(zhí)行命令之前記得先提交你當(dāng)前分支的修改,否則會(huì)被強(qiáng)制刪干凈沒得后悔)
$git symbolic-ref HEAD refs/heads/[name]
$rm .git/index
$git clean -fdx
?
3)版本(tag)操作相關(guān)命令
查看版本:$ git tag
創(chuàng)建版本:$ git tag [name]
刪除版本:$ git tag -d [name]
查看遠(yuǎn)程版本:$ git tag -r
創(chuàng)建遠(yuǎn)程版本(本地版本push到遠(yuǎn)程):$ git push origin [name]
刪除遠(yuǎn)程版本:$ git push origin :refs/tags/[name]
合并遠(yuǎn)程倉庫的tag到本地:$ git pull origin --tags
上傳本地tag到遠(yuǎn)程倉庫:$ git push origin --tags
創(chuàng)建帶注釋的tag:$ git tag -a [name] -m 'yourMessage'
?
4)?子模塊(submodule)相關(guān)操作命令
添加子模塊:$ git submodule add [url] [path]
???如:$git submodule add git://github.com/soberh/ui-libs.git src/main/webapp/ui-libs
初始化子模塊:$ git submodule init ?----只在首次檢出倉庫時(shí)運(yùn)行一次就行
更新子模塊:$ git submodule update ----每次更新或切換分支后都需要運(yùn)行一下
刪除子模塊:(分4步走哦)
?1)?$ git rm --cached [path]
?2)?編輯“.gitmodules”文件,將子模塊的相關(guān)配置節(jié)點(diǎn)刪除掉
?3)?編輯“?.git/config”文件,將子模塊的相關(guān)配置節(jié)點(diǎn)刪除掉
?4)?手動(dòng)刪除子模塊殘留的目錄
?
5)忽略一些文件、文件夾不提交
在倉庫根目錄下創(chuàng)建名稱為“.gitignore”的文件,寫入不需要的文件夾名或文件,每個(gè)元素占一行即可,如
target
bin
*.db
?
?
?
?
=====================
Git 常用命令
git branch 查看本地所有分支
git status 查看當(dāng)前狀態(tài)?
git commit 提交?
git branch -a 查看所有的分支
git branch -r 查看本地所有分支
git commit -am "init" 提交并且加注釋?
git remote add origin git@192.168.1.119:ndshow
git push origin master 將文件給推到服務(wù)器上?
git remote show origin 顯示遠(yuǎn)程庫origin里的資源?
git push origin master:develop
git push origin master:hb-dev 將本地庫與服務(wù)器上的庫進(jìn)行關(guān)聯(lián)?
git checkout --track origin/dev 切換到遠(yuǎn)程dev分支
git branch -D master develop 刪除本地庫develop
git checkout -b dev 建立一個(gè)新的本地分支dev
git merge origin/dev 將分支dev與當(dāng)前分支進(jìn)行合并
git checkout dev 切換到本地dev分支
git remote show 查看遠(yuǎn)程庫
git add .
git rm 文件名(包括路徑) 從git中刪除指定文件
git clone git://github.com/schacon/grit.git 從服務(wù)器上將代碼給拉下來
git config --list 看所有用戶
git ls-files 看已經(jīng)被提交的
git rm [file name] 刪除一個(gè)文件
git commit -a 提交當(dāng)前repos的所有的改變
git add [file name] 添加一個(gè)文件到git index
git commit -v 當(dāng)你用-v參數(shù)的時(shí)候可以看commit的差異
git commit -m "This is the message describing the commit" 添加commit信息
git commit -a -a是代表add,把所有的change加到git index里然后再commit
git commit -a -v 一般提交命令
git log 看你commit的日志
git diff 查看尚未暫存的更新
git rm a.a 移除文件(從暫存區(qū)和工作區(qū)中刪除)
git rm --cached a.a 移除文件(只從暫存區(qū)中刪除)
git commit -m "remove" 移除文件(從Git中刪除)
git rm -f a.a 強(qiáng)行移除修改后文件(從暫存區(qū)和工作區(qū)中刪除)
git diff --cached 或 $ git diff --staged 查看尚未提交的更新
git stash push 將文件給push到一個(gè)臨時(shí)空間中
git stash pop 將文件從臨時(shí)空間pop下來
---------------------------------------------------------
git remote add origin git@github.com:username/Hello-World.git
git push origin master 將本地項(xiàng)目給提交到服務(wù)器中
-----------------------------------------------------------
git pull 本地與服務(wù)器端同步
-----------------------------------------------------------------
git push (遠(yuǎn)程倉庫名) (分支名) 將本地分支推送到服務(wù)器上去。
git push origin serverfix:awesomebranch
------------------------------------------------------------------
git fetch 相當(dāng)于是從遠(yuǎn)程獲取最新版本到本地,不會(huì)自動(dòng)merge
git commit -a -m "log_message" (-a是提交所有改動(dòng),-m是加入log信息) 本地修改同步至服務(wù)器端 :
git branch branch_0.1 master 從主分支master創(chuàng)建branch_0.1分支
git branch -m branch_0.1 branch_1.0 將branch_0.1重命名為branch_1.0
git checkout branch_1.0/master 切換到branch_1.0/master分支
du -hs
-----------------------------------------------------------
mkdir WebApp
cd WebApp
git init
touch README
git add README
git commit -m 'first commit'
git remote add origin git@github.com:daixu/WebApp.git
git push -u origin master
?
Git 常用命令圖表?
?
?
http://www.cnblogs.com/1-2-3/archive/2010/07/18/git-commands.html
?
參考推薦:
Windows環(huán)境中使用版本管理工具Git
Git 命令參數(shù)及用法詳解
GitRef(推薦)
?
教程
創(chuàng)建 git 倉庫
初始化 git 倉庫
cd?project?# 進(jìn)入到項(xiàng)目目錄
git?init?# 初始化 git 倉庫。此命令會(huì)在當(dāng)前目錄新建一個(gè) .git 目錄,用于存儲(chǔ) git 倉庫的相關(guān)信息
?
初始化提交
touch?READMEgit?add .?# 將當(dāng)前目錄添加到 git 倉庫中, 使用 git add -A 則是添加所有改動(dòng)的文檔
git?commit?-m?"Initial commit"
git?remote add origin?git@github.com:lugir/repo.git?# 設(shè)置倉庫
?
修補(bǔ)提交(修補(bǔ)最近一次的提交而不創(chuàng)建新的提交)
git?commit?--amend?-m?"commit message."?
提交沖突時(shí)可以合并后再推送
git?pull?# 獲取遠(yuǎn)程版本庫提交與本地提交進(jìn)行合并git?push?# 提交
?
使用別人的倉庫
git?clone http://path/to/git.git?# clone 的內(nèi)容會(huì)放在當(dāng)前目錄下的新目錄?
將代碼從本地回傳到倉庫
git?push?-u?origin master?
使用 git status 查看文件狀態(tài)
git?status?
查看提交日志
git?log?# 查看提交信息git?log?--pretty=oneline?# 以整潔的單行形式顯示提交信息
git?log?--stat?# 查看提交信息及更新的文件
?
Git 分支
git?branch?# 查看分支git?branch?6.x-1.x?# 添加分支 6.x-1.x
git?branch checkout master?# 切換到主分支
git?branch?-d?6.x-1.x?# 刪除分支 6.x-1.x
git?push origin :branchname?# 刪除遠(yuǎn)端分支
?
Git 標(biāo)簽
git?tag?# 查看分支git?tag?6.x-1.0?# 添加標(biāo)簽 6.x-1.0
git?show?6.x-1.0?# 查看標(biāo)簽 6.x-1.0 的信息
git?tag?-a?6.x-1.0?965e066?# 為之前提交的信息記錄 965e066 加上標(biāo)簽
git?push?--tags?# 提交時(shí)帶上標(biāo)簽信息
git?push origin :/refs/tags/tagname?# 刪除遠(yuǎn)端標(biāo)簽
?
從 git 倉庫中導(dǎo)出項(xiàng)目
git?archive?--format?tar?--output?/path/to/file.tar master?# 將 master 以 tar 格式打包到指定文件?
使用 Git 的一些基本守則:
當(dāng)要commit/提交patch時(shí):
- 使用 git diff --check 檢查行尾有沒有多余的空白
- 每個(gè) commit 只改一件事情。如果一個(gè)文檔有多個(gè)變更,使用 git add --patch 只選擇文檔中的部分變更進(jìn)入 stage
- 寫清楚 commit message
?
轉(zhuǎn)載于:https://www.cnblogs.com/madordie/p/4164694.html
總結(jié)
以上是生活随笔為你收集整理的Git 常用命令和教程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Atitit。DD dragdrop拖拽
- 下一篇: 随便写写2014创业记(二)