github的使用---git版本控制


git 安裝

ubuntu下: sudo apt-get install git 源碼安裝

  

windows下載git軟件即可

git核心原理

git常用命令

 

git add
添加提交任務到暫存區
git commit -m "commit info"
添加提交任務到版本庫
git log
查看提交記錄
git diff
查看工作區和暫存區的差異
git diff --cached
查看暫存區和版本庫的差異
git diff HEAD
查看工作區和版本庫的差異
git status -s
簡短輸出,第一個M表示暫存區和版本庫內容不一致;第二個M表示工作區和暫存區內容不一致
git checkout -- file.txt
把暫存區的file.txt文件恢復到工作區,覆蓋工作區之前的修改。checkout命令主要是把歷史某個版本檢出到工作
區。慎用
git reset HEAD
暫存區的目錄樹被版本庫里的內容重置,但是工作區不受影響。放棄之前git add的提交。
git reset --hard SHA1號/HEAD
工作區和暫存區的目錄樹被版本庫里的內容重置。放棄之前git add和個git commit的提交。
git rm file.txt
刪除文件
git blame file.txt
查看文件提交歷史信息,方便定位bug
git show-ref
查看所包含的引用
git merge
進行合並操作
git push -u origin master
向遠程版本庫origin的master分支提交
git pull
把遠程版本庫的master分支拉到本地,數據同步服務器端
git tag -m "my first tag" mytagv1.0
制作里程碑
git cat-file tag mytagv1.0
查看mytagv1.0提交信息
git tag -l -n1
查看所有tag,n1顯示一行信息
git tag -d mytagv1.0
刪除tag
git branch newbranch
創建分支
git checkout newbranch
切換到newbranch分支
git branch -d newbranch
刪除分支,如沒合並,則失敗
git branch -D newbranch
強制刪除分支
git push origin :newbranch
先刪除本地分支,再刪除遠程版本庫對應分支
git show-ref
查看本地引用
git checkout -b hello-1 origin/hello-1
創建跟蹤遠程分支的本地分支,隨后可以pull和push遠程分支
git remote add new-remote file:///path/hello-1.git
創建遠程版本庫
git remote -v
查看遠程版本庫

 

 

 github協作開發GitHub(網址 https://github.com/)是一個面向開源及私有軟件項目的托管平台,因為只支持Git作為唯一的版本庫格式進行托管,故名GitHub

github地址:

https://github.com/

 創建帳號

 

創建項目
  1.創建項目
  2.下載項目

1. 網頁下載zip文件
2. git clone https://github.com/xwpfullstack/itcast.git

  linux終端配置

1 配置環境
設置系統全局用戶

Global setup:
git config --global user.name "Your Name"
git config --global user.email xwp_fullstack@163.com

每個項目也可以單獨在配置文件里設置用戶

vim .git/config

  查看環境配置

git config -l

  刪除全局配置

git config --unset --global user.name
git config --unset --global user.email

  

ssh配置
1.生成ssh-key,在終端下執行以下命令

itcast$ ssh-keygen
~/.ssh/id_rsa是私鑰文件
~/.ssh/id_rsa.pub是公鑰文件

 

2.把公鑰上傳到github上

 


配置好ssh公鑰后,在終端下執行以下命令

 

ssh -T git@github.com Hi gotgithub! You've successfully authenticated, but GitHub does not provide shell access. 出
現上述提示證明配置好ssh

 

.ssh-key的作用
用戶命令行下提交代碼時不需要再輸入賬號和密碼,默認https協議是需要輸入的
修改項目中./git/config文件,把url換成 url = git@github.com:注冊名/項目名.git
例如: url = git@github.com:xwpfullstack/test.git

 

第一個項目

1 新建空項目
先在github頁面創建一個項目world

git clone https://github.com/xwpfullstack/world.git
cd world
echo "world"" >> README.md
git add README.md

 git commit -m "first commit"
 git remote add origin https://github.com/xwpfullstack/world.git
 git push -u origin maste

把一個已經存在的git管理的項目放到github上托管

git remote add origin https://github.com/xwpfullstack/world.git
git push -u origin master

  

下載項目
三種下載方式:

https協議下載
git clone https://github.com/username/test.git
Git-daemon協議下載
git clone git://github.com/username/test.git
打包下載

 

 分支

 

git branch -a
查看所有分支,含遠程
git branch
查看本地分支
git branch -r
查看遠程分支
git branch newbranch
創建新分支
git branch -d oldbranch
刪除分支oldbranch
git push origin newbranch
把新分支同步到遠程倉庫
git push origin :oldbranch
刪除遠程oldbranch分支
git checkout -b newbranch
切換到另一分支,如果不存在則創建
git checkout branch
切換到另一分支
git clone新項目時
無論當前項目在什么分支,都是下載默認master分支
git checkout master
git merge anthorbranch
git push origin maste
合並開發分支到master分支
git pull盡量少使用,隱含了合並信息
get featch 下載遠程origin/master更新
git diff master origin/master
查看本地和遠程庫的區別
get merge origin/master
合並遠端更新到本地

  

tag
tag

git tag v1.0 -m "tag v1"
創建一個標簽
git push origin --tags
把所有標簽同步到遠程庫
git checkout v1.0
檢出v1.0版本
git reset --hard v1.0
使代碼重置,回到v1.0。此功能慎用,后開發的代碼會丟失
git tag -d v1.0
刪除標簽v1.0
git push origin :v1.0
刪除遠程庫里標簽
git tag -l -n1
查看所有tag

  


 HEAD
HEAD

指向當前提交點,游標指針
HEAD^ 上次提交點,父提交點
git checkout HEAD^
切換到上次提交點,不在分支
git reset --hard HEAD^
撤銷本次提交,恢復到上次提交狀態,新創建的文件會丟棄
4.5.7 忽略監控文件
.gitignore一般把.o和臨時文件設置進去,忽略代碼監控,如某項目.gitignore:
#過濾數據庫文件、sln解決方案文件、配置文件
*.mdb
*.ldb

*.sln
*.config

#過濾文件夾Debug,Release,obj
Debug/
Release/
obj/

 

然后調用git add. ,執行 git commit即可。
問題:.gitignore只適用於尚未添加到git庫的文件。如果已經添加了,則需用git rm
移除后再重新commit。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM