git使用基礎操做

從今天開始,正式進軍github!linux

學習參考網址 :http://blog.csdn.net/googdev/article/details/52575079git

很是感謝大牛stormzhang,他的文章很是贊!!!github

 

1、Git 安裝shell

Mac:https://sourceforge.net/projects/git-osx-installer/
Windows:https://git-for-windows.github.io/
Linux:apt-get install gitwindows

2、Git基礎操做bash

(注:Windows環境)ssh

一、建立一個文件夾test,並在test下建立一個a.md文件,而後在git bash下用cd命令進入到剛纔建立的test文件夾,ide

二、初始化 git 倉庫學習

 

[plain] view plain copy測試

  1. git init  

三、查看狀態

 

[plain] view plain copy

  1. git status  

四、把a.md文件添加到本地Git倉庫

 

[plain] view plain copy

  1. git add a.md  

五、設置下本身的用戶名與郵箱

 

[plain] view plain copy

  1. git config —global user.name "JasonLi"   
  2. git config —global user.email "lijingxuan92@126.com"  

 

六、正式提交文件

 

[plain] view plain copy

  1. git commit -m ‘first commit’  

-m 表明是提交信息

七、查看全部產生的 commit 記錄

 

[plain] view plain copy

  1. git log  

八、把本地 test 項目與 GitHub 上的 test 項目進行關聯(切換到 test 目錄)

 

[plain] view plain copy

  1. git remote add origin git@github.com:JasonLi-cn/test.git  

(查看咱們當前項目有哪些遠程倉庫)

[plain] view plain copy

  1. git remote -v  

九、向遠程倉庫進行代碼提交(前提是你已經配置好公鑰和密鑰,配置方法見第三部分

 

[plain] view plain copy

  1. git push origin master  

提交時,可能出現的問題:

[plain] view plain copy

  1. $ git push origin master  
  2. To github.com:JasonLi-cn/test.git  
  3.  ! [rejected]        master -> master (fetch first)  
  4. error: failed to push some refs to 'git@github.com:JasonLi-cn/test.git'  
  5. hint: Updates were rejected because the remote contains work that you do  
  6. hint: not have locally. This is usually caused by another repository pushing  
  7. hint: to the same ref. You may want to first integrate the remote changes  
  8. hint: (e.g., 'git pull ...') before pushing again.  
  9. hint: See the 'Note about fast-forwards' in 'git push --help' for details.  

說明在遠程倉庫中存在本地倉庫沒有的文件,因此須要先pull操做

 

[plain] view plain copy

  1. git pull origin master  

此時可能會遇到的問題:

 

[plain] view plain copy

  1. $ git pull origin master  
  2. From github.com:JasonLi-cn/test  
  3.  * branch            master     -> FETCH_HEAD  
  4. fatal: refusing to merge unrelated histories  

解決方法:

[plain] view plain copy

  1. git pull origin master --allow-unrelated-histories  

而後就能夠 push了!!!

 

3、公鑰和密鑰配置方法

在Git bash中執行:

[plain] view plain copy

  1. ssh-keygen -t rsa  

會生成兩個文件 id_rsa 和 id_rsa.pub , id_rsa 是密鑰,id_rsa.pub 就是公鑰。

 

第一步先在 GitHub 上的設置頁面,點擊最左側 SSH and GPG keys ,而後點擊右上角的 New SSH key 按鈕,

在 Key 那欄把 id_rsa.pub 公鑰文件裏的內容複製粘貼進去就能夠了,

Title 那欄不須要填寫,點擊 Add SSH key 按鈕。

SSH key 添加成功以後,輸入

 

[plain] view plain copy

  1. ssh -T git@github.com  

  進行測試,若是出現如下提示證實添加成功了。

[plain] view plain copy

  1. $ ssh -T git@github.com  
  2. Hi JasonLi-cn! You've successfully authenticated, but GitHub does not provide shell access.  

4、其它經常使用命令

[plain] view plain copy 

  1. git branch aaa 新建分枝aaa  
  2. git branch 查看分枝  
  3. git checkout aaa 切換到分枝aaa  
  4. git checkout -b aaa 新建並切換到aaa  
  5. git merge aaa 把aaa分支的代碼合併過來(當前所在分枝,好比master)  
  6. git branch -d aaa 刪除分枝aaa  
  7. git branch -D aaa 強制刪除aaa  
  8. git tag v1.0 加版本號  
  9. git tag 查看版本號  
  10. git checkout v1.0 切換到版本v1.0  
相關文章
相關標籤/搜索