Git常用命令总结

提交代码

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常用命令总结_第1张图片

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

2、查看分支

git branch

3、切换分支

git checkout branchname

合并代码

Git常用命令总结_第2张图片

1、拉取分支

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

2、回退分支修改

git reset --hard commitid

3、查看log

git log --oneline

291e427 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

你可能感兴趣的:(算法模板,git,github)