git提交代碼的正確步驟命令:git如何提交代碼到分支

到目前為止,我們一直工作在本地倉庫,這也是 git 的特色,不需要中央伺服器就能完整的使用 git 所有功能,為了和別人協作開發,必須學習關於遠程倉庫的知識。

在本地 git init 初始化的倉庫中,運行 git remote -v,什麼也有沒有,因為倉庫還沒有添加遠端的站點,先要找一個存放我們代碼的遠端倉庫,去 github.com 或 gitee.com 註冊一個賬號,新建一個倉庫取名為 git-demo。

大白話 git 教程-09-遠端倉庫設置和提交

創建以後,由於沒有選擇任何初始化的文件,github 提示了一些命令行操作。

大白話 git 教程-09-遠端倉庫設置和提交

首選有 https 和 ssh 兩種選擇,https 會通過用戶名和密碼來訪問倉庫,而 ssh 依靠我們的公鑰來訪問方庫,推薦使用 ssh 方式,對 ssh 公鑰不清楚的請訪問 linux 教程-網路操作 這個章節,如果是新註冊的用戶,先去用戶中心把自己的公鑰 ~/.ssh/id_rsa.pub 的內容放上去。

現在可以把這個遠程的倉庫地址添加到本地了,執行下面的命令:

wangbo@wangbo-VirtualBox:~/test/git-demo$ git remote add origin git@github.com:developdeveloper/git-demo.git
wangbo@wangbo-VirtualBox:~/test/git-demo$ git remote -v
origin	git@github.com:developdeveloper/git-demo.git (fetch)
origin	git@github.com:developdeveloper/git-demo.git (push)

origin 什麼意思呢? 它只是這個遠端倉庫地址的別名而已,你可以取其他的名稱,比如可以改成 gitgub:

wangbo@wangbo-VirtualBox:~/test/git-demo$ git remote rename origin github
wangbo@wangbo-VirtualBox:~/test/git-demo$ git remote -v
github	git@github.com:developdeveloper/git-demo.git (fetch)
github	git@github.com:developdeveloper/git-demo.git (push)

為了習慣呢,還是改回 origin 來講解,遠端倉庫地址有了,接下來可以使用 git push 命令把我們本地的倉庫推送到遠端了,這樣別人就能看到了。

wangbo@wangbo-VirtualBox:~/test/git-demo$ git push origin master
......
Enumerating objects: 28, done.
Counting objects: 100% (28/28), done.
Compressing objects: 100% (20/20), done.
Writing objects: 100% (28/28), 2.60 KiB | 444.00 KiB/s, done.
Total 28 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To github.com:developdeveloper/git-demo.git
 * [new branch]      master -> master

刷新一下網頁發現我們的倉庫內容已經同步到了 github 的倉庫,有了遠端倉庫被人怎麼使用呢? 首選需要把倉庫 clone 成本地倉庫:

git clone git@github.com:developdeveloper/git-demo.git // 工作區名稱為 git-demo
或
git clone git@github.com:developdeveloper/git-demo.git learn-git // 工作區名稱為 learn-git

現在來看看分支的情況,這次需要加一個 a 參數:

wangbo@wangbo-VirtualBox:~/test/git-demo$ git branch -a
  feature_print
* master
  remotes/origin/master

發現多了一個遠程分支 remotes/origin/master。

而在 clone 的倉庫 learn-git 里執行 git branch -a 結果如下:

wangbo@wangbo-VirtualBox:~/test/learn-git$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

這裡並沒有 feature_print 分支,因為 feature_print 分支我們並沒有推送到遠端,我們可以把它也推上去,執行:

wangbo@wangbo-VirtualBox:~/test/git-demo$ git push origin feature_print
......
To github.com:developdeveloper/git-demo.git
 * [new branch]      feature_print -> feature_print
wangbo@wangbo-VirtualBox:~/test/git-demo$ git branch -a
  feature_print
* master
  remotes/origin/feature_print
  remotes/origin/master

多了一個遠端的分支 remotes/origin/feature_print,切換到 learn-git 工作區,因為分支並不能自動的同步,我們需要執行 git fetch 同步命令才行。

wangbo@wangbo-VirtualBox:~/test/learn-git$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
wangbo@wangbo-VirtualBox:~/test/learn-git$ git fetch
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
From github.com:developdeveloper/git-demo
 * [new branch]      feature_print -> origin/feature_print
wangbo@wangbo-VirtualBox:~/test/learn-git$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature_print
  remotes/origin/master

怎麼獲得一個對應的本地 feature_print 分支呢?

wangbo@wangbo-VirtualBox:~/test/learn-git$ git switch feature_print
Branch 'feature_print' set up to track remote branch 'feature_print' from 'origin'.
Switched to a new branch 'feature_print'
wangbo@wangbo-VirtualBox:~/test/learn-git$ git branch -a
* feature_print
  master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature_print
  remotes/origin/master

你也可以使用 git checkout 來操作:

git checkout -b feature_print
或
git checkout -b feature_print origin/feature_print

現在克隆的倉庫也有本地的 feature_print 分支了,需要更新 feature_print 上的代碼,可以直接輸入 git pull,但這個命令其實分為 2 個步驟,第一步是 git fetch 更新遠端的代碼到本地 remotes/origin/feature_print 分支上,第二步是和本地 remotes/origin/feature_print 分支進行 merge 合併操作,也可以通過 git pull –rebase 指定為 rebase 合併操作,對 merge 和 rebase 不清楚的請參考前面分支合併的章節內容。

現在 git-demo 工作區更新一下 readme.md 文件並推送到遠端。

wangbo@wangbo-VirtualBox:~/test/git-demo$ echo add remote info >> readme.md
wangbo@wangbo-VirtualBox:~/test/git-demo$ git add .
wangbo@wangbo-VirtualBox:~/test/git-demo$ git commit -m 'add info'
[feature_print 07f8e9a] add info
 1 file changed, 1 insertion(+)
wangbo@wangbo-VirtualBox:~/test/git-demo$ git push origin feature_print
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 286 bytes | 286.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:developdeveloper/git-demo.git
   5a8323a..07f8e9a  feature_print -> feature_print

切換回 learn-git 工作區,確保你在 feature_print 分支下,執行 git pull –rebase:

wangbo@wangbo-VirtualBox:~/test/learn-git$ git branch
* feature_print
  master
wangbo@wangbo-VirtualBox:~/test/learn-git$ git pull --rebase
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
Unpacking objects: 100% (3/3), 266 bytes | 266.00 KiB/s, done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
From github.com:developdeveloper/git-demo
   5a8323a..07f8e9a  feature_print -> origin/feature_print
Updating 5a8323a..07f8e9a
Fast-forward
 readme.md | 1 +
 1 file changed, 1 insertion(+)
Current branch feature_print is up to date.

你已經獲得了最新的代碼,查看 readme.md 文件確認一下:

wangbo@wangbo-VirtualBox:~/test/learn-git$ cat readme.md
learn git
make changes
add remote info

你已經學會了基本的本地和遠端同步內容的方法。關於遠端倉庫還有一些補充命令:

1. git remote show origin 查看 origin 遠端的信息

2. git remote rm origin 可以刪除 origin 遠端

3. git fetch origin 可以更新 origin 遠端的內容

4. git fetch -p 或 git fetch origin -p 如果遠端的分支不存在了,本地也刪除對應的分支

5. git push origin master 同步到遠端是 git push origin master:master 的簡寫,冒號後面表示遠端的分支

6. git push origin –delete feature_print 和 git push origin :feature_print 都可以刪除遠端的分支,後者理解成把 nothing 推向到遠端

7. git push -f origin master 可以強行用本地分支內容覆蓋遠端的分支內容,因為默認本地分支需要同步遠端內容後才能推送到遠端

8. git remote set-url origin git@github.com:zhangsan/abc.git 修改遠端倉庫的 url 地址

注意,遠端倉庫的操作非常重要,下個章節繼續學習遠端倉庫的同步操作。

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/220597.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-09 12:11
下一篇 2024-12-09 12:11

相關推薦

發表回復

登錄後才能評論