复习使用git(二)

删除远程分支

git push origin --delete 分支名

撤销修改

  1. 撤销工作区的修改

已修改,但尚未添加(add),使用 git restore 文件名 撤销工作区的修改。

Note:
“git checkout – 文件名”,checkout 检出的意思,检出原来的工作区的文件,覆盖现在的文件,这样就相当于撤销了文件的修改。

“git checkout .” 撤销所有修改。一般的 . 即根目录,相当于根目录文件路径,所以会撤销根目录下所有文件的修改。

git checkout 是老早之前git使用的命令了,推荐使用 git restore 文件名,直接撤销工作区的修改。

撤销暂存区的修改

已 add 但未 commit

add 之后文件被添加到暂存区

git restore --staged 文件名

Note:
第一步:
“git reset HEAD – test.txt”
第二步:
“git checkout – test.txt”

以上命令是老早之前的git使用的方法,推荐直接使用 git restore 命令。

  1. 撤销本地仓库的修改

git commit 之后已经提交到了仓库,只是暂时未同步到远程仓库,这时候查看 git log,刚才提交的 commit 已经生成了一条记录。

commit 678bd15a8246f221df909cb1dd9cb0256fda019c (HEAD -> main)
Author: X-Morris <940036850@qq.com>
Date:   Tue Feb 28 11:10:46 2023 +0800

    Remove the test file content.

commit cf7f43cd656615aca33211fc1161df79691dc62d (origin/main, origin/HEAD)
Author: X-Morris <940036850@qq.com>
Date:   Tue Feb 28 10:43:41 2023 +0800

    Add the test file.

commit d29e6873d7bace285bef6d07eab3e8a99a02413c
Author: X-Morris <940036850@qq.com>
Date:   Tue Feb 28 09:39:54 2023 +0800

    Add html.txt.

git reset --hard commitId
git push

➜  OhMyDog git:(main) git reset --hard cf7f43cd656615aca33211fc1161df79691dc62d
HEAD is now at cf7f43c Add the test file.
➜  OhMyDog git:(main) git push       
Everything up-to-date

这时候上一条commit已经在记录git log 中消失了。git reset --hard 需谨慎使用。

查看某个文件的修改记录

git log -p 文件名 查看详细的修改记录

git blame 文件名 查看修改的作者

创建于合并分支

git 创建与合并分支

分支管理策略

示例图来自廖雪峰老师的git分支管理策略
在这里插入图片描述
如上图,清晰明了,很好理解。

猜你喜欢

转载自blog.csdn.net/Morris_/article/details/129258077