1、創建一個文件夾test,並在test下創建一個a.md文件,然後在git bash下用cd命令進入到剛纔創建的test文件夾,
2、初始化 git 倉庫
3、查看狀態
4、把a.md文件添加到本地Git倉庫
5、設置下自己的用戶名與郵箱
-
git config —global user.name "JasonLi"
-
git config —global user.email "[email protected]"
6、正式提交文件
-
git commit -m ‘first commit’
-m 代表是提交信息
7、查看所有產生的 commit 記錄
8、把本地 test 項目與 GitHub 上的 test 項目進行關聯(切換到 test 目錄)
(查看我們當前項目有哪些遠程倉庫)
9、向遠程倉庫進行代碼提交(前提是你已經配置好公鑰和密鑰,配置方法見第三部分)
提交時,可能出現的問題:
-
$ git push origin master
-
To github.com:JasonLi-cn/test.git
-
! [rejected] master -> master (fetch first)
-
error: failed to push some refs to '[email protected]:JasonLi-cn/test.git'
-
hint: Updates were rejected because the remote contains work that you do
-
hint: not have locally. This is usually caused by another repository pushing
-
hint: to the same ref. You may want to first integrate the remote changes
-
hint: (e.g., 'git pull ...') before pushing again.
-
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
說明在遠程倉庫中存在本地倉庫沒有的文件,所以需要先pull操作
此時可能會遇到的問題:
-
$ git pull origin master
-
From github.com:JasonLi-cn/test
-
* branch master -> FETCH_HEAD
-
fatal: refusing to merge unrelated histories
解決方法:
-
git pull origin master --allow-unrelated-histories
然後就可以 push了!!!
三、公鑰和密鑰配置方法
在Git bash中執行:
會生成兩個文件 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 添加成功之後,輸入
進行測試,如果出現以下提示證明添加成功了。
-
$ ssh -T [email protected]
-
Hi JasonLi-cn! You've successfully authenticated, but GitHub does not provide shell access.
四、其它常用命令
-
git branch aaa 新建分枝aaa
-
git branch 查看分枝
-
git checkout aaa 切換到分枝aaa
-
git checkout -b aaa 新建並切換到aaa
-
git merge aaa 把aaa分支的代碼合並過來(當前所在分枝,比如master)
-
git branch -d aaa 刪除分枝aaa
-
git branch -D aaa 強制刪除aaa
-
git tag v1.0 加版本號
-
git tag 查看版本號
-
git checkout v1.0 切換到版本v1.0