git提交本地代碼到遠程新分支


有時候我們回滾項目代碼后,常常將本地回滾的分支提交到遠程分支,命令如下:
1.建立本地倉庫
查看當前項目根目錄中有沒有 .git文件(隱藏文件),如果沒有,右鍵->Git bash here ,然后輸入命令git init建立本地倉庫

git init
1
2.將代碼提交到本地倉庫

git add .
git commit -m "new branch commit"
1
2
3.在本地倉庫中建立一個與遠程倉庫的別名,以便之后提交代碼而不是每次都要輸入遠程倉庫地址。指令結尾是git的倉庫地址,我使用的是SSH連接方式

git remote add origin git@XX.XX.XX.12:gyjia/hotcodeserver.git

1
2
使用SSH的話,提交會出現以下問題:

git@gitee.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

1
2
3
4
5
6
所以需要聲明公鑰:
1)、重新生成ssh
ssh-keygen -t rsa -C “xxxxx@xxxxx.com” (你的賬號)
2)、查看你的 public key
cat ~/.ssh/id_rsa.pub
(以ssh-rsa 開頭,以賬號的注冊郵箱結尾的)
3)、將它添加到碼雲,添加地址 https://gitee.com/profile/sshkeys
4)、終端輸入
ssh -T git@gitee.com
5)、完成

當然,如果不想驗證公鑰,則git remote add origin 時直接使用http鏈接即可

4.本地的代碼提交的遠程倉庫上,步驟如下:

1)首先要建立本地的分支,並切換到該分支上(本地建立完分支,默認是在master分支上)

git branch hello_git_branch
1
git checkout hello_git_branch
1
2)push到遠程倉庫上面

git push origin hello_git_branch
1
這里的含義是將hello_git_branch這個分支提交到遠程倉庫上面。如果遠程倉庫沒有這個分支,那么也會新建一個該分支。 還有一種方法,可以指定提交到遠程倉庫的某個分支上。如下,是將hello_git_branch分支提交到遠程倉庫的master上面

git push origin hello_git_branch:master
1
拓展:

如果本地當前是在hello_git_branch分支上面,此時想把遠程倉庫的master與我的hello_git_branch分支合並(merge),可以使用如下命令:
---------------------


免責聲明!

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



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