Git常用命令总结
创始人
2025-06-01 14:19:05
0

提交代码

1、查看修改的文件

git status

查看区别

git diff

将diff文件转成patch

git diff > patch

2、添加修改的文件

git add .

3、编辑提交信息

首次提交,加上签名sign

git commit -s

非首次提交

git commit --amend

4、推送代码到远程分支

git push https://gitee.com/username/project.git HEAD:branchname -f

更新代码

1、本地干净,无未上库修改

git pull

2、本地有未合入上库的代码修改

git pull --rebase

2.1 若有冲突,需要解

本地代码仓查找"<<<<<<"解冲突

git status
git add .
git rebase --continue
git push https://gitee.com/username/project.git HEAD:branchname -f

删除代码

1、清除本地未提交的修改

git checkout .

2、清除本地的新增文件(git status查看标为untracked files的文件)

git clean -f

3、清除本地已提交(push过)的修改

git reset --hard FETCH_HEAD

4、回退代码到某个commit id

git reset --hard commitid

拉取代码

1、拉取pr分支

在这里插入图片描述

git fetch https://gitee.com/project.git pull/xxxx/head:pr_xxxx

2、查看分支

git branch

3、切换分支

git checkout branchname

合并代码

在这里插入图片描述

1、拉取分支

git fetch https://gitee.com/project.git pull/xxxx/head:pr_xxxx

2、回退分支修改

git reset --hard commitid

3、查看log

git log --oneline291e427 update website
8c8f3f4 update website
1693a6f update clear-logs.sh version
3759b84 update clear-logs.sh
fc36a2a add links
1d795e6 fix && update clear-logs.sh 0.0.2
9536dab add dingtalk script
3a51aaa fix shellcheck problem
2db6ad3 add clear logs scripts
e57b0e6 fix && add batch del
17cb931 fix && add batch del
cf7e875 add redis script
fe4bbcb Initial commit

4、合并commit

# 合并指定版本号(上步中cf7e875不参与合并)
git rebase -i cf7e875

5、进入编辑界面

pick 17cb931 fix && add batch del
pick e57b0e6 fix && add batch del
pick 2db6ad3 add clear logs scripts
pick 3a51aaa fix shellcheck problem
pick 9536dab add dingtalk script
pick 1d795e6 fix && update clear-logs.sh 0.0.2
pick fc36a2a add links
pick 3759b84 update clear-logs.sh
pick 1693a6f update clear-logs.sh version
pick 8c8f3f4 update website# Rebase cf7e875..291e427 onto cf7e875 (10 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

6、修改信息

除了最上面的保持"pick"状态其他全部改成"s"

pick 17cb931 fix && add batch del
s e57b0e6 fix && add batch del
s 2db6ad3 add clear logs scripts
s 3a51aaa fix shellcheck problem
s 9536dab add dingtalk script
s 1d795e6 fix && update clear-logs.sh 0.0.2
s fc36a2a add links
s 3759b84 update clear-logs.sh
s 1693a6f update clear-logs.sh version
s 8c8f3f4 update website

:wq 保存退出

7、编辑注释

修改完 :wq 保存退出

8、提取patch

有几个^就会打几个patch,从最近一次打起

git format-patch HEAD^

9、打patch

9.1、git diff生成的patch

git apply --reject patch

9.2、git format-patch生成的patch

git am -3 xxx.patch

相关内容

热门资讯

鲍威尔出席美联储国际金融司活动... 美联储主席鲍威尔(Jerome Powell)周一在美联储国际金融司(IF)成立75周年活动上发表讲...
关税政策推进受阻,特朗普政府求... 6月2日,美国特朗普政府请求联邦上诉法院阻止此前哥伦比亚特区联邦地区法院裁定其关税政策“违法”的命令...
关税政策推进受阻 特朗普政府求... 当地时间6月2日,美国特朗普政府请求联邦上诉法院阻止此前哥伦比亚特区联邦地区法院裁定其关税政策“违法...
英国央行Catherine M... 英国央行Catherine Mann:必须考虑量化紧缩(QT)和一系列利率政策的影响。
原创 香... 在新加坡香格里拉酒店举行的亚太安全论坛上,美国国防部长赫格塞思(Lloyd Austin)于5月31...
美方掐断C919发动机技术,中... 在国际舞台上,当两大经济体相遇,总会引发一场关于技术、资本和市场的角力。最近,中美之间的博弈又一次走...
6800万镑!埃泽“豪言”震惊... 足坛风云变幻,总有那么些球员,他们的名字与豪门绯闻紧密相连,每一次表态都能在转会市场上掀起波澜。这不...
北京警察连续输错5次密码:这就... 几天前,北京丰台的民警上门拦截电信网络诈骗时,发生了紧张的一幕。被骗事主开门时还在与骗子共享屏幕,刚...
美媒公布最新民调:全球对中国好... 【环球网报道 记者 赵建东】美国Axios新闻网2日援引晨间咨询公司(Morning Consult...
韩国新总统最快4日上任,学者:... 韩国总统大选3日即将迎来正式投票。根据选前多项“封关”民调结果,共同民主党候选人李在明仍以明显优势领...