【嵌入式开发 Linux 常用命令系列 4.1 -- git push 远程分支与本地分支查看】


概述

在日常工作中,将代码 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 compile
 Date: Wed Dec 6 14:38:57 2023 +0800
 1 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: 遇到问题还是要多查查,小时候学的口号:“不懂就要问” 应该改为 “不懂就去查”… 在查找分析结束后自己最好复盘下。即便别人告诉你答案,你还是不知其所以然!!!

猜你喜欢

转载自blog.csdn.net/sinat_32960911/article/details/134872513
今日推荐