git使用基礎操作

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

2、初始化 git 倉庫

[plain] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. git init  
3、查看狀態

[plain] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. git status  
4、把a.md文件添加到本地Git倉庫

[plain] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. git add a.md  
5、設置下自己的用戶名與郵箱

[plain] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. git config —global user.name "JasonLi"   
  2. git config —global user.email "[email protected]"  

6、正式提交文件

[plain] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. git commit -m ‘first commit’  

-m 代表是提交信息

7、查看所有產生的 commit 記錄

[plain] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. git log  
8、把本地 test 項目與 GitHub 上的 test 項目進行關聯(切換到 test 目錄)

[plain] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. git remote add origin [email protected]:JasonLi-cn/test.git  
(查看我們當前項目有哪些遠程倉庫)
[plain] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. git remote -v  
9、向遠程倉庫進行代碼提交(前提是你已經配置好公鑰和密鑰,配置方法見第三部分

[plain] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. git push origin master  
提交時,可能出現的問題:
[plain] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  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 '[email protected]: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
 在CODE上查看代碼片派生到我的代碼片
  1. git pull origin master  
此時可能會遇到的問題:

[plain] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  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
 在CODE上查看代碼片派生到我的代碼片
  1. git pull origin master --allow-unrelated-histories  
然後就可以 push了!!!

三、公鑰和密鑰配置方法

在Git bash中執行:

[plain] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  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
 在CODE上查看代碼片派生到我的代碼片
  1. ssh -T [email protected]  

  進行測試,如果出現以下提示證明添加成功了。

[plain] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. $ ssh -T [email protected]  
  2. Hi JasonLi-cn! You've successfully authenticated, but GitHub does not provide shell access.  
四、其它常用命令

[plain] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  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  
發佈了71 篇原創文章 · 獲贊 49 · 訪問量 49萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章