文章目錄
- 概述
- git push 語法
- 步驟1:git 遠程主機名查看
- 步驟2:git 遠程分支名查看
- 步驟3:git 本地分支名查看
- 示例演示
概述
在日常工作中,將代碼 git clone
本地之后,或者使用repo init && repo sync
之后不知道使用什么命令將修改好的代碼git push
到服務器上,本篇文章以自己工作中遇到的實際問題為例,介紹下如何在不知道 git push
的具體參數的情況下,一步一步查找到git push
的正確命令。
git push 語法
我們都知道 push 命令的作用是將本地當前分支的代碼推送到遠程指定的分支上,其命令的格式如下:
git push <遠程主機名> <本地分支名>:<遠程分支名>
使用 git push
命令將 <本地分支名> 的代碼推送到 <遠程主機名> 中的 <遠程分支名> 上。
- <遠程主機名> :指的是你想要推送到哪個遠程主機中,在我們克隆一個項目的時候,git 會自動幫我們把遠程主機起名為
origin
,一般情況下是不會去修改這個名字的。 - <本地分支名>:是本地分支的名字,可以使用命令
git branch
來查看本地有哪些分支。 - <遠程分支名>:指的是遠程主機中的分支名,如果遠程主機沒有這個分支,則會新創建一個。
步驟1:git 遠程主機名查看
使用 git remote
命令或者 git remote -v
命令
[10:45:53]sam@linux (*^~^*) ~/sam/workbase/npu_code/device$ git remote
arm-val
or
[10:47:31]sam@linux (*^~^*) ~/sam/workbase/npu_code/device$ git remote -v
arm-val ssh://codereview.armtech.com/arm_val/device (fetch)
arm-val ssh://codereview.armtech.com/arm_val/device (push)
所以可以看到遠程主機名為 arm-val。
步驟2:git 遠程分支名查看
使用 git branch -vv
, 可以看到遠程分支名為arm-val/arm_main
:
[10:54:31]sam@linux (*^~^*) ~/sam/workbase/npu_code/device$ git branch -vv
* test b3f0b95 [arm-val/arm_main: ahead 1] device: compile: support npu device compile
步驟3:git 本地分支名查看
HEAD
表示一個特別的指針,指向你正在工作的本地分支的指針,可以當做是本地分支的別名。git 通過它可以知道當前工作區間在哪個分支。
refs/for
表示我們提交代碼到服務器之后是需要經過 code review 之后才能進行merge 操作的,而 refs/heads
是不需要的。
所以 git push
的命令為:
git push <遠程主機名> HEAD:refs/for/<遠程分支名>
示例演示
查看遠程分支名:
[10:45:26]sam@linux (*^~^*) ~/sam/workbase/npu_code/device$ git branch -vv
* test b3f0b95 [arm-val/arm_main: ahead 1] device: compile: support npu device compile
查看遠程主機名:
[10:37:30]sam@linux (*^~^*) ~/sam/workbase/npu_code/device$ git remote -v
arm-val ssh://codereview.armtech.com/arm_val/device (fetch)
arm-val ssh://codereview.armtech.com/arm_val/device (push)
開始推代碼:
使用 git push arm-val HEAD:refs/for/arm_main
開始推代碼:
[10:37:00]sam@linux (*^~^*) ~/sam/workbase/npu_code/device$ git push arm-val HEAD:refs/for/arm_main
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 16 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 485 bytes | 485.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3)
remote: Processing changes: refs: 1, done
remote: ERROR: commit 4b80700: missing Change-Id in message footer
remote:
remote: Hint: to automatically insert a Change-Id, install the hook:
remote: gitdir=$(git rev-parse --git-dir); scp -p -P 29418 sam@codereview.armtech.com:hooks/commit-msg ${gitdir}/hooks/
remote: or, for http(s):
remote: f="$(git rev-parse --git-dir)/hooks/commit-msg"; curl -o "$f" https://codereview.armtech.com/tools/hooks/commit-msg ; chmod +x "$f"
remote: and then amend the commit:
remote: git commit --amend --no-edit
remote: Finally, push your changes again
remote:
To ssh://codereview.armtech.com/arm_val/device! [remote rejected] HEAD -> refs/for/arm_main (commit 4b80700: missing Change-Id in message footer)
error: failed to push some refs to 'ssh://codereview.armtech.com/arm_val/device'
按照提示操作:
[10:37:46]sam@linux (*^~^*) ~/sam/workbase/npu_code/device$ gitdir=$(git rev-parse --git-dir); scp -p -P 29418 sam@codereview.armtech.com:hooks/commit-msg ${gitdir}/hooks/
[10:38:01]sam@linux (*^~^*) ~/sam/workbase/npu_code/device$ git commit --amend --no-edit
[uefi_test b3f0b95] device: compile: support npu device compileDate: Wed Dec 6 14:38:57 2023 +08001 file changed, 3 insertions(+), 3 deletions(-)
修改后再次重推:
[10:38:15]sam@linux (*^~^*) ~/sam/workbase/npu_code/device$ git push arm-val HEAD:refs/for/arm_main
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 16 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 520 bytes | 520.00 KiB/s, done.
Total 4 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3)
remote: Processing changes: refs: 1, new: 1, done
remote: commit b3f0b95: Missing issue-id in commit message
remote: Commit b3f0b957b0c5dd4911707855dec98f321dcfa737 not associated to any issue
remote:
remote: Hint: insert one or more issue-id anywhere in the commit message.
remote: Issue-ids are strings matching (DPTS[WE][A-Z]*-[0-9]+)
remote: and are pointing to existing tickets on its-jira Issue-Tracker
remote:
remote: SUCCESS
remote:
remote: https://codereview.armtech.com/c/arm_val/device/+/9820 device: compile: support npu device compile [NEW]
remote:
To ssh://codereview.armtech.com/arm_val/device* [new reference] HEAD -> refs/for/arm_main
至此,成功完成了 git push 的命令。
TIPS: 遇到問題還是要多查查,小時候學的口號:“不懂就要問” 應該改為 “不懂就去查”… 在查找分析結束后自己最好復盤下。即便別人告訴你答案,你還是不知其所以然!!!