git學習:php
經常使用 Git 命令清單:http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
html
Git遠程操做詳解:http://www.ruanyifeng.com/blog/2014/06/git_remote.html
git
Git 工做流程:http://www.ruanyifeng.com/blog/2014/06/git_remote.html
github
git與svn的區別:npm
git分佈式,本地就可作版本管理vim
svn必須依託遠程倉庫作版本控制weex
gitlab分佈式
git使用流程圖:svn
配置信息 gitlab
#全局用戶信息 git config --global user.name [用戶名] git config --global user.email [郵箱] # 顯示當前的Git配置 $ git config --list # 編輯Git配置文件 $ git config -e [--global]
查看全局config:cd ~ -> cat .gitconfig
查看當前倉庫config :cd .git -> cat config 示例:
注:通常不手動更改config內容
git命令可用 && 鏈接, 並列執行
主幹發佈,分支開發
建立倉庫:
# 在當前目錄新建一個Git代碼庫 $ git init # 新建一個目錄,將其初始化爲Git代碼庫 $ git init [project-name] # 下載一個項目和它的整個代碼歷史,[別名]可省略,默認名與遠程倉庫相同 $ git clone [url] [別名]
增長/刪除文件
# 添加指定文件到暫存區 $ git add [file1] [file2] ... # 添加指定目錄到暫存區,包括子目錄 $ git add [dir] # 添加當前目錄的全部文件到暫存區 $ git add . # 添加每一個變化前,都會要求確認 # 對於同一個文件的多處變化,能夠實現分次提交 $ git add -p # 刪除工做區文件,而且將此次刪除放入暫存區 $ git rm [file1] [file2] ... # 中止追蹤指定文件,但該文件會保留在工做區 $ git rm --cached [file] # 更名文件,而且將這個更名放入暫存區 $ git mv [file-original] [file-renamed]
代碼提交
# 提交暫存區到倉庫區 $ git commit -m [message] # 提交暫存區的指定文件到倉庫區 $ git commit [file1] [file2] ... -m [message] # 提交工做區自上次commit以後的變化,直接到倉庫區 $ git commit -a # 提交時顯示全部diff信息 $ git commit -v # 使用一次新的commit,替代上一次提交 # 若是代碼沒有任何新變化,則用來改寫上一次commit的提交信息 $ git commit --amend -m [message] # 重作上一次commit,幷包括指定文件的新變化 $ git commit --amend [file1] [file2] ...
分支管理:
#查看本地當前分支 $ git branch #查看全部分支 $ git branch -a #查看遠程分支 $ git branch -r #建立新分支 ,建議分支名規範:[項目名]-[日期]-[建立人]-[修改內容] #如:lagou-20171016-yangpeng-bugfix $ git branch [分支名] #切換分支 $ git checkout [分支名] #建立並切換分支 $ git checkout -b [分支名]
#取遠程分支並分化一個新分支
$ git checkout -b test origin/master
# 合併指定分支到當前分支,通常在master上操做(須要有管理員權限) #默認使用當前分支與 [branch] 合併,合併指定分支到當前分支 $ git merge [branch]
# 合併其餘分支的某個文件到當前分支
$ git merge --patch [branch] [file.name]
$ git merge -p [branch] [file.name] # 切換到上一個分支 $ git checkout - # 創建追蹤關係,在現有分支與指定的遠程分支之間 $ git branch --set-upstream [branch] [remote-branch] # 選擇一個commit,合併進當前分支 $ git cherry-pick [commit] # 刪除分支 $ git branch -d [branch-name] #強制刪除分支 $ git branch -D [branch-name] # 刪除遠程分支 $ git push origin --delete [branch-name] $ git branch -dr [remote/branch]
遠程同步:
# 下載遠程倉庫的全部變更 $ git fetch [origin]# 顯示全部遠程倉庫 $ git remote -v # 顯示某個遠程倉庫的信息 $ git remote show [remote] # 增長一個新的遠程倉庫,並命名 $ git remote add [shortname] [url] # 取回遠程倉庫的變化,並與本地分支合併 $ git pull [remote] [branch] # 上傳本地指定分支到遠程倉庫 (注:先執行git fetch [origin] ,) #(注:分支提交前首先先創建聯繫 git branch --set-upstream [branch] [remote-branch] # 務必先與master合併 git checkout master ,git pull ,git checkout [當前分支], git merge [master] ..) $ git push [remote] [branch] # 強行推送當前分支到遠程倉庫,即便有衝突 $ git push [remote] --force # 推送全部分支到遠程倉庫 $ git push [remote] --all
標籤:主要標識某次提交(適用場景如:給項目各個測試階段加標籤,灰度測試、beta測試(b tag),release (R tag))
# 列出全部tag $ git tag # 新建一個tag在當前commit $ git tag [tag] # 新建一個tag在指定commit $ git tag [tag] [commit] # 刪除本地tag $ git tag -d [tag] # 刪除遠程tag $ git push origin :refs/tags/[tagName] # 查看tag信息 $ git show [tag] # 提交指定tag $ git push [remote] [tag] # 提交全部tag $ git push [remote] --tags # 新建一個分支,指向某個tag $ git checkout -b [branch] [tag]
查看信息
# 顯示有變動的文件 $ git status # 顯示當前分支的版本歷史 $ git log # 顯示commit歷史,以及每次commit發生變動的文件 $ git log --stat # 搜索提交歷史,根據關鍵詞 $ git log -S [keyword] # 顯示某個commit以後的全部變更,每一個commit佔據一行 $ git log [tag] HEAD --pretty=format:%s # 顯示某個commit以後的全部變更,其"提交說明"必須符合搜索條件 $ git log [tag] HEAD --grep feature # 顯示某個文件的版本歷史,包括文件更名 $ git log --follow [file] $ git whatchanged [file] # 顯示指定文件相關的每一次diff $ git log -p [file] # 顯示過去5次提交 $ git log -5 --pretty --oneline # 顯示全部提交過的用戶,按提交次數排序 $ git shortlog -sn # 顯示指定文件是什麼人在什麼時間修改過 $ git blame [file] # 顯示暫存區和工做區的差別 $ git diff # 顯示暫存區和上一個commit的差別 $ git diff --cached [file] # 顯示工做區與當前分支最新commit之間的差別 $ git diff HEAD # 顯示兩次提交之間的差別 $ git diff [first-branch]...[second-branch] # 顯示今天你寫了多少行代碼 $ git diff --shortstat "@{0 day ago}" # 顯示某次提交的元數據和內容變化 $ git show [commit] # 顯示某次提交發生變化的文件 $ git show --name-only [commit] # 顯示某次提交時,某個文件的內容 $ git show [commit]:[filename] # 顯示當前分支的最近幾回提交 $ git reflog
撤銷
# 恢復暫存區的指定文件到工做區 $ git checkout [file] # 恢復某個commit的指定文件到暫存區和工做區 $ git checkout [commit] [file] # 恢復暫存區的全部文件到工做區 $ git checkout . # 重置暫存區的指定文件,與上一次commit保持一致,但工做區不變 $ git reset [file] # 重置暫存區與工做區,與上一次commit保持一致 $ git reset --hard # 重置當前分支的指針爲指定commit,同時重置暫存區,但工做區不變 $ git reset [commit] # 重置當前分支的HEAD爲指定commit,同時重置暫存區和工做區,與指定commit一致 $ git reset --hard [commit] # 重置當前HEAD爲指定commit,但保持暫存區和工做區不變 $ git reset --keep [commit] # 新建一個commit,用來撤銷指定commit # 後者的全部變化都將被前者抵消,而且應用到當前分支 $ git revert [commit] # 暫時將未提交的變化移除,稍後再移入,(適用:將本次未提交的內容懸掛,切換分支工做完後,在切回當前分支) $ git stash $ git stash pop
submodule
# 新增submodule git submodule add git@github.com:lib.git <local path> # 初始化submodule git submodule init # 更新submodule git submodule update # 修改submodule後必須經過submodule命令提交後才能 # 同步到主工程中,不然git submodule update拉取的還是以前的commit head git push --recurse-submodules=on-demand
submodules的命令很長,爲提高效率,能夠建立alias,記錄在.git/config路徑下。以下:
git config alias.spush 'push --recurse-submodules=on-demand' git config alias.supdate 'submodule update --remote --merge'
查看某次提交(commit)修改內容( git diff):
共同開發:
一、項目settings
二、collaboration,輸入在github上的用戶名,則該用戶會收到邀請郵件,接受邀請後
三、權限管理(默認成員具備全部權限),protect branchtian
本地同步remote上的新分支,須要本地新建該分支,而後進入該分支 進行 git pull
git init git remote add origin git@git.oschina.net:flqbestboy/js-1024.git git add . git commit -m "first commit" git pull --rebase origin master git push -u origin master
Git之忽略文件(ignore file):http://blog.csdn.net/benkaoya/article/details/7932370
github上發佈項目(只純網頁):
新建項目:[項目名].github.io
項目settings
github pages
直接訪問[項目名].github.io
可能出現的錯誤:
錯誤一:本地master未與遠程master關聯(雖然添加了遠程關聯庫但未關聯master)
解決:
git push --set-upstream origin master
錯誤二:
There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details git pull <remote> <branch> If you wish to set tracking information for this branch you can do so with git branch --set-upstream master origin/<branch>
解決方案:
git branch --set-upstream master origin/master
fatal: the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead.
解決方案:
git branch --set-upstream-to=origin/master master
錯誤三: fatal: Unable to create 'F:/HbuilderProject/projectModel/.git/index.lock': File exists.
解決:執行rm -f .git/index.lock (或者rm -f git/index.lock) 刪除後正常
錯誤四:
解決方案:
一、http://blog.csdn.net/huahua78/article/details/52330792
二、。。20171024 github 被牆,改用碼雲
錯誤五:
一、未初始化git, git init
二、沒有進入git clone 後的目錄
錯誤六:
➜ weex-project git:(release-1.0.4) ✗ git add . fatal: Unable to create '/Users/linqiang/Documents/workplace/weex-project/.git/index.lock': File exists. Another git process seems to be running in this repository, e.g. an editor opened by 'git commit'. Please make sure all processes are terminated then try again. If it still fails, a git process may have crashed in this repository earlier: remove the file manually to continue.
解決:
cd .git
rm index.lock
問題
1.修改遠程倉庫
方法有三種:
1.修改命令
git remote set-url origin [url]
例如:git remote set-url origin gitlab@gitlab.chumob.com:php/hasoffer.git
2.先刪後加
git remote rm origin
git remote add origin [url]
3.直接修改config文件
問題:多人開發時,一人刪除了一些無用的tag,push到遠程倉庫後,另外一人pull下來後,使用git tag 1.10.8, git push -tags,後遠程倉庫又從新出現已經刪除的tags
緣由:彷佛git目前也沒有提供一個直接的命令和參數選項能夠刪除本地的在遠程已經不存在的tag
解決方案:遠程回退到原版,而後本地
#刪除全部本地tag git tag -l | xargs git tag -d #從遠程倉庫中從新拉取tag git pull
爲了不以後再次出現這種狀況,建議每次提交tag時不使用提交全部的tags命令: git push -tags ,而使用僅提交當前標註到tag命令: git push origin V-1.02.003 ,此外應當給當前tag添加附註 git tag -a V-1.02 -m 「版本1.02」
碼雲上簡易的命令行入門教程:
咱們強烈建議全部的git倉庫都有一個README, LICENSE, .gitignore文件
Git 全局設置:
git config --global user.name "flqbestboy" git config --global user.email "962358354@qq.com"
建立 git 倉庫:
mkdir test cd test git init touch README.md git add README.md git commit -m "first commit" git remote add origin git@gitee.com:flqbestboy/test.git git push -u origin master
已有項目?
cd existing_git_repo git remote add origin git@gitee.com:flqbestboy/test.git git push -u origin master
自定義快捷命令
mac環境下vim ~/.zshrc, 添加自定義便可,例以下
# user defined alias alias work='cd ~/Documents/workplace && ls' alias gt='git add . ' alias gl='git pull' alias gh='git push' alias gs='git status' alias dev='npm run dev' alias build='npm run build' alias push='git add . && git commit -m "bugfix" && git pull && git push'