Git基本操作

1. Git初始化及倉庫創建和操作
1.1 基本信息設置
1. 設置用戶名

git config --global user.name 'itcastphpgit1'

2. 設置用戶名郵箱

git config --global user.email '[email protected]'

1.2 初始化一個新的Git倉庫
1. 創建文件夾
mkdir test

2.在文件內初始化git(創建git倉庫)
cd test
git init

3.向倉庫添加文件
touch a1.php                         # 創建a1.php文件到工作目錄

git add a1.php                      # 添加a1.php到暫存區

git commit -m  '第一次提交文件'     # 添加a1.php到倉庫

git remote add origin  [email protected]:morixinguan/Y.X.YANG.git   #提交本地倉庫到GitHub
注意:如果這步出現無權限問題,則按照網上的方法配置ssh key即可。

git push origin master                              #添加到遠程origin倉庫master分支
注意:git push origin master -f在git push origin master出問題( ! [rejected]        master -> master (fetch first))後輸入,意爲強制推送

4.刪除倉庫文件
rm -rf a1.php
git rm a1.php
git commit -m '第一次提交文件'

5.將遠程倉庫(github對應的項目)複製到本地
git clone https://github.com/JunLuo-BIT/Machine-Learning.git

6.分支的創建與合併
6.1 分支的創建並切換(git checkout test)到新創建的分支
git checkout -b test

查看分支:git branch

6.2 分支合併
git merge test  #git merge 命令用於合併指定分支到當前分支。注意這裏是切換到了master主分支,將指定分支test合並過來。合併後,可以發現master主分支的內容和test分支的最新提交是完全一樣的。

6.3 刪除分支
git branch -d test 

總結

在本地新建一個分支: git branch newBranch 
切換到你的新分支: git checkout newBranch 
將新分支發佈在github上: git push origin newBranch 
在本地刪除一個分支: git branch -d newBranch 
在github遠程端刪除一個分支: git push origin :newBranch (分支名前的冒號代表刪除) 
/git push origin –delete newBranch 
注意刪除遠程分支後,如果有對應的本地分支,本地分支並不會同步刪除!

注意,在新建、刪除、合併分支之後都得git push origin newBranch,推送到github服務器。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章