Git基本命令學習

Git是一個由林納斯·託瓦茲爲了更好地管理linux內核開發而創立的分佈式版本控制/軟件配置管理軟件,現在已經超越CVS、SVN稱爲主流的版本控制器。許多著名的開源項目都用Git管理,比較火的託管服務如 github 。html

Git
Git :
http://git-scm.com/
Git Extensions:
http://code.google.com/p/gitextensions/
Book
Pro Git:
http://git-scm.com/book
Try Git:
https://try.github.io/levels/1/challenges/1
Tutorial:
https://www.atlassian.com/git/tutorial
GUI
Smartgit:
http://www.oschina.net/p/smartgit
SourceTree:
http://www.sourcetreeapp.com
Git for Windows :
http://msysgit.github.io
GitHub Service
https://github.com/
http://git.oschina.net/
https://bitbucket.org/
https://www.heroku.com/
https://gitcafe.com/
https://code.jd.com/ node

global config

若是是第一次啓用Git的話,須要先配置下用戶名與郵箱,用來標識本地用戶
#設置用戶名
git config --global user.name "Irving"
#設置Email
git config --global user.email 'xxx@outlook.com'
#設置顏色
git config --global color.ui auto
# 別名(Add some SVN-like aliases)
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.up rebase
git config --global alias.ci commit
jquery

#查看全部配置
git config -1
#用戶的git配置文件~/.gitconfig
linux

image
#初始化
git init
生成ssh-key
#生成RSA並設置公鑰 (添加到Github的public key)
ssh-keygen -t rsa -C 'xxx@outlook.com'
#測試是否成功
ssh -T git@git.oschina.net

ssh -T
git@github.com
git

:初始化一個新的版本庫 而後將目錄中的全部文件歸入管理,Git把這個過程稱爲stage,最後以快照的方式提交全部文件
git init
git add .
git commit -m 'initial commit'

#本地項目提交到遠程(基本的工做流程)
touch README.md
git init
git add README.md (git add . 根目錄全部文件)
git commit -m "initial commit"
git remote add origin
https://github.com/zhouyongtao/my-node-learning.git
git push -u origin master
注意:若是是 git add .會添加全部文件,若是不想提交所有文件,須要設置.gitignore文件,若是遠程的地址換了, git remote rm origin 移除從新添加便可.
github

git的狀態

image

clone and push

#克隆遠程到本地
git clone git@git.oschina.net:irving/Test.git
cd Test
ls -al
#修改代碼
echo hello Test by Irving >README.md
git status
#目錄下的全部文件所有提交到緩衝區
git add.
#代碼提交到HEAD
git commit -m 'hello Test by Irving'
#推送到遠程
git push origin master
web

git remote add origin git@git.oschina.net:irving/Test.git
git push origin master
#提交本地test分支做爲遠程master分支
git push origin test:master
#提交本地test分支做爲遠程的test分支
git push origin test:test
#刪除遠程的分支
git push origin :test
vim

branch

#查詢當前branch
git branch
#顯示所有branch,包括遠程和本地
git branch –a
#只顯示遠程branch
git branch –r
#切換到branch
git checkout branchName
#建立一個新分支
git branch newbranchname
#刪除分支
git branch -D target_branch_name
#合併某分支到當前分支
git merge branchName
#推送遠程分支
git push origin local_branch_name:remote_branch_name
服務器

:建立分支與合併分支app

#建立一個新的分支(new_branch),將它檢出(checkout)爲活動分支,而後就能夠編輯、載入和提交新的快照
git branch new_branch
git checkout new_branch
touch hello.txt
git add hello.txt
git commit -m 'add hello for new_branch'
#切換到master分支,恢復new_branch分支中剛剛作的更改,而後編輯一些文件,並將這些更改提交到master分支。
git checkout master
git add master.txt
git commit -a -m 'change files'
#合併(merge)new_branch分支到master分支,結合你的項目須要,能夠刪掉new_branch分支。
git merge new_branch
git branch -d new_branch

remote

#檢出倉庫
git clone git://github.com/jquery/jquery.git

增長一個遠程倉庫(名稱爲origin的遠程服務器,之後提交代碼的時候只須要使用 origin別名便可)
git remote add origin git://github.com/someone/xxxx.git
#查看遠程倉庫
git remote -v
#添加遠程倉庫
git remote add [name] [url]
#刪除遠程倉庫
git remote rm [name]
#修改遠程倉庫
git remote set-url –push [name] [newUrl]
#拉取遠程倉庫
git pull [remoteName] [localBranchName]
#推送遠程倉庫
git push [remoteName] [localBranchName]

tag

#查看版本
git tag
#建立版本
git tag [name]
#刪除版本
git tag -d [name]
#查看遠程版本
git tag -r
#建立遠程版本(本地版本push到遠程)
git push origin [name]
#刪除遠程版本
git push origin :refs/tags/[name]
#合併遠程倉庫的tag到本地
git pull origin –tags
#上傳本地tag到遠程倉庫
git push origin –tags
#建立帶註釋的tag
git tag -a [name] -m ‘message'

gitignore

.gitignore只適用於還沒有添加到git庫的文件,若是已經添加了,則須要git rm移除後再從新commit
git rm -f xxx
git commit
git add .gitignore
git commit
git push

#刪除.idea目錄下文件
git rm .idea/* -r
git rm hello
#提交
git commit -a -m  'delete hello file'
git push -u origin master
#增長忽略文件
vim .gitignore
#在文件中加入
.idea/**/*
更多:
https://github.com/github/gitignore

pull request

先將託管在GitHub上的項目克隆(clone)到本地,作過更改以後推送回GitHub,而後發送一個pull請求,項目的維護者就會收到郵件通知。
在GitHub上fork項目:
git clone
https://github.com/irving/request
cd request
git add (files)
git commit -m 'Explain what I changed'
git push origin master
而後到GitHub上點擊pull request按鈕

速查表

啓動&初始化

Git的配置、版本庫初始化(init)和克隆(clone)。

git config [key] [value] 配置版本庫參數
git config --global [key] [value] 爲用戶設置全局屬性
git init 將已經存在的一個目錄初始化爲Git版本庫
git clone [url] 從一個URL地址克隆(clone)一個Git版本庫
git help [command] 獲取幫助

暫存&快照

使用Git的快照(snapshots)和暫存區(staging area)。

git status 顯示下次提交的暫存區的狀態和工做目錄的更改
git add [file] 添加文件到暫存區
git reset [file] 重置暫存區的一個文件使以前的更改不被暫存
git diff 顯示未暫存的更改(即比較暫存和未暫存的項)
git diff --staged 顯示未提交的更改(即比較暫存區和版本庫)
git commit 以一個新的快照提交暫存項
git rm [file] 從工做目錄和暫存區移除文件
git gui 啓動Git GUI圖形界面

分支&合併

使用Git的分支(branch)和臨時存放(stash)。

git branch 列出當前的全部分支,前邊加*號的爲當前活動分支
git branch [branch-name] 以當前的提交建立一個新的分支
git checkout [branch] 切換到另外一個分支,並檢出到當前工做目錄
git checkout -b [branch] 建立一個新的分支並切換到該分支
git merge [branch] 進另外一個分支合併到當前活動分支,並將這次合併記錄爲一次提交
git log 顯示提交日誌
git stash 臨時存儲當前未提交的更改
git stash apply 恢復最後一次的臨時存儲

共享&更新

抓取(fetch)、合併(merge),以及從另外一個版本庫獲取更新。

git remote add [alias] [url] 爲一個URL地址添加別名
git fetch [alias] 從遠程版本庫拉取全部分支
git merge [alias]/[branch] 合併一個分支到當前活動分支,是當前活動分支更新到最新版本
git push [alias] [branch] 推送本地分支到遠程版本庫,使遠程版本庫得到更新
git pull 從當前分支跟蹤的遠程分支中合併數據到本地

檢查&比較

抓取(fetch)、合併(merge),以及從另外一個版本庫獲取更新。

git log 顯示當前分支的提交歷史
git log branchB..branchA 顯示branchA有而branchB沒有的提交
git log --follow [file] 顯示該文件的提交記錄,包括重命名
git diff branchB...branchA 顯示在branchA中而不在branchB中的不一樣
git show [SHA] 顯示人可讀格式的文件的
gitx 在GUI中顯示提交記錄

文件衝突

Error pulling origin: error: Your local changes to the following files would be overwritten by merge:

image

Refer:

OSChina Git 學習
http://git.oschina.net/oschina/git-osc/wikis/Home
經常使用Git代碼託管服務分享
http://kevinfan.blog.51cto.com/1037293/1306021
Eclipse上安裝GIT插件EGit及使用
http://lwklwc.blog.51cto.com/8051822/1411684
http://mweb.baidu.com/p/git-101.html

Git系列文章(推薦)
http://rogerdudler.github.io/git-guide/index.zh.html
https://www.gitbook.io/book/lvwzhen/git-tutorial/
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
http://www.git-tower.com/learn/ebook/command-line/introduction

Git 命令一覽圖
http://www.git-tower.com/blog/git-cheat-sheet-detail/

基礎文章
http://rogerdudler.github.io/git-guide/index.zh.html
https://www.atlassian.com/git/tutorials/setting-up-a-repository/
Git教程
http://lvwzhen.github.io/Git-Tutorial/
圖解Git
http://marklodato.github.io/visual-git-guide/index-zh-cn.html
練習
https://try.github.io/levels/1/challenges/1
http://pcottle.github.io/learnGitBranching/

相關文章
相關標籤/搜索