Git常用命令介绍

Git常用命令介绍

本文只包含 W3c:Git官方文档涉及到的命令。
在这里插入图片描述

Resoure

这部分就不说了。

Create Git/创建 Git

创建一个git仓库。

From existing directory

如果想在本地文件夹中创建一个仓库

cd project_dir
git add .
git init

From other repository

如果想使用已有的仓库

# clone一个本地存在的仓库
git clone existing_dir new_dir
# clone一个远程仓库,两者区别在于引用地址的方式不同,前者是ssh,后者是https。
git clone git://github.com/user/repo.git
git clone https://github.com/user/repo.git

Local Changes/查看本地变化

查看本地的变化

Changed in working directory

git status

查看工作区的变化,输出:

# 当前所在分支
On branch master 

# 当前分支的状态:
## 情况一:表示最新的分支
Your branch is up to date with 'origin/master'.
## 情况二:可能会发生冲突
Your branch is ahead of 'origin/master' by 1 commit.
  	(use "git push" to publish your local commits)
  	
# 提交状态
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
	modified:   README.md #### 表示修改
    delete:   ... #### 表示删除 
    new file:   test.js #### 表示新增
    
# 暂存区状态
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
	## 修改时:
	modified:   README.md
	## 删除时:
	deleted:   README.md
	## 新增时:
	Untracked files:
		README.md

Tracked file changes

比较工作区与暂存区的变化

git diff

比较指定文件工作区和缓存区的变化:git diff -- filePath

比较暂存区和本地库最近一次commit内容:git diff --cached

比较工作区与本地最近一次commit内容:git diff HEAD

比较两个commit之间差异:git diff [first-branch] [second-branch],详见:

# 1. 获取提交以及上传内容的版本号
>> git reflog
4cf2d2b (HEAD -> master) HEAD@{0}: commit: commit-2
c23f3ff HEAD@{1}: commit: commit-1
ade28e2 HEAD@{2}: commit: test
2c60606 (origin/master, origin/HEAD) HEAD@{3}: clone: from C:/Users/Lenovo/Desktop/gogogo-start/./vue-learning
# 2. 比较
>> git diff c23f3ff ade28e2

Add changed files

添加文件的更改到暂存区

# 添加指定更改了的文件到暂存区
git add file1 file2 file3
# 添加所有文件的更改到暂存区
git add .

Remove file

删除工作区的文件

git rm dir/ -r
	(recursive under directory)
git rm file

Commit changes

提交文件的更改到本地仓库

git commit
git commit -a -m "My Message"
	(tracked files only, auto add)
git commit -m "My message"
  • 使用git commit [file...]之后,git将会开启编辑器让你输入此次提交的提交信息。提交信息中的首行若是以#开头,则该行在提交时会被忽略。
  • -a 添加所有已跟踪文件,将所有处于已跟踪状态的文件提交,但是未跟踪的文件并不会被提交。编辑器仍旧会进入编辑模式,让你输提交信息。
  • -m 直接写入提交信息,不会进入编辑器。

Change last commit

更改上一次的提交

git commit --amend

使用场景:

在一些受管控的项目上,提交代码到 git 服务器后,还需要经过审核确认才正式合入版本,一般常用 gerrit 来进行审核确认操作。

如果提交的代码审核不通过,需要再次修改提交。由于是修改同一个问题,我们可能不希望生成多个 commit 信息,会显得改动分散,看起来改动不完善,所以想要在本地已有的 commit 信息上再次提交改动,而不是在已有的 commit 上再新增一个 commit。

使用 git commit --amend 命令可以达到在现有最新 commit 上再次提交改动的效果。

在本地提交改动后,我们再次修改代码,执行 git add 命令添加改动,如果执行 git commit -m 命令,默认会创建新的空 commit 信息,填写相应的修改说明,提交之后,会新增一个 commit 信息;而执行 git commit --amend 命令会弹出当前最新 commit 的信息,我们可以修改这个信息,也可以不修改,提交之后,用 git log 命令查看,会看到没有增加新的 commit,原先 commit 的 hash 值也没有变,这一次的修改是跟之前的修改一起提交的。

引自:kingjay

Revert changes to file

撤销工作区的修改

git checkout -- file

Revert changes (new commit)

恢复更改(新提交)

git revert HEAD

作用:git revert 是 Git 中用于撤销一个或多个提交的命令,但是会保留历史记录。它创建一个新的提交,该提交是对原提交的相反操作,以撤销原提交的更改。通过 git revert 撤销提交,可以保持历史记录的完整性,不会修改已经共享的提交历史。

语法:git revert <commit-hash1> <commit-hash2>

撤销单个提交:git revert <commit-hash1>

撤销多个提交:git revert <commit-hash1> <commit-hash2> ...

撤销最近的提交:git revert HEAD

撤销某个范围的提交:git revert <commit-hash>^...<commit-hash2>,范围左开右闭。

通过 git log查看commit 哈希码

注:撤销的每一个提交都对应着创建一个新的提交。

Return to last committed state

返回到上一次提交的状态

git reset --hard HEAD

作用:移动 HEAD 指针并可选地更改暂存区和工作目录的状态。它是一个非常强大的工具,常用于撤销提交、取消暂存文件、修改提交内容等操作。

语法:git reset --(soft|mixed|hard) <HEAD~(num)>

--hard 回退全部(HEAD,index,working tree),这会将HEAD指向的分支的最新提交撤销掉,暂存区和工作区的文件的所有更改都将丢弃。相当于回到了上一个提交的状态。

--mixed 回退部分(HEAD,index),这会将HEAD指向的分支的最新提交撤销掉,暂存区的所有文件更改都将丢弃,但工作区的文件的更改不会变化。

--soft 只回退 (HEAD),HEAD指向的分支的最新提交撤销掉,但不会覆盖工作区的更改。

History/查看历史记录

Show all commits

查看所有的提交历史记录

git log

Short Format

以短格式的形式显示

git log --pretty=-short

--pretty 指定输出格式,例如:简洁模式--pretty=oneline 短格式--pretty=short 完整格式--pretty=full 自定义格式(--pretty=format:"..."

Patches

查看所有的提交历史记录,包括diff

git log -p

例:

commit ade28e2acca022ff24a212818ce72df2bf5b9030
Author: 0Blue <1790280415@qq.com>
Date:   Sat Aug 10 19:46:01 2024 +0800

    test

diff --git a/README.md b/README.md
index e69de29..fbd2fe8 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1 @@
+### 
\ No newline at end of file

Show file commits

查看某个文件的提交历史记录

git log file

Show directory commits

查看某个文件夹的提交历史记录

git log dir/

Stats

查看所有的提交历史记录,并统计每次提交的更改

git log --stat

例:

commit 43ce6d0dfba5f4c4259da4242ccae6e5282940d9 (HEAD -> master)
Author: 0Blue <1790280415@qq.com>
Date:   Sun Aug 11 17:09:42 2024 +0800

    Revert "3"
    
    This reverts commit 351101ac9dba2124bfabaacf780d77f439c0f6cc.

 README.md | 10 +---------
 test.js   |  0
 2 files changed, 1 insertion(+), 9 deletions(-)

Who changed file

查看文件的修改历史记录,包括时间、作者以及内容

git blame file

例:

c23f3ffc (0Blue 2024-08-11 16:00:04 +0800 3) 又新增了一行

Merge/Rebase 合并

Merge branch into current

当前分支与指定分支合并

git merge branch

例:

git checkout develop
git merge feature/daohang
# 或者:
git merge feature/daohang develop

Rebase into branch

git rebase branch
git rebase master branch

作用:将多个commit记录合并为一条

例:-i 指定要合并的那一条记录的上一条 commitId

git rebase -i HEAD~4

或者使用commitId

git rebase -i e88835de905ad396f61a0dc8c040a8ac8a34f3f8

此时进入rebase交互式终端,如下:

pick ade28e2 test
pick c23f3ff commit-1
pick 351101a 3
pick 43ce6d0 Revert "3"

# Rebase 2c60606..43ce6d0 onto 2c60606 (4 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
#                    commit's log message, unless -C is used, in which case       
#                    keep only this commit's message; -c is same as -C but        
#                    opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')       
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified); use -c <commit> to reword the commit message
#
# 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.

注释对关键字进行了说明,pick:使用这次的commit

squash:使用commit,但合并到以前的commit中

d/drop:移除这一次提交

我们想合并最近4条到最后一条中,则修改成如下并 wq 保存退出。

s ade28e2 test
s c23f3ff commit-1
s 351101a 3
pick 43ce6d0 Revert "3"

接着会进入到另一个交互式终端,如果commit message 无异议可以直接 wq 保存退出

Abort rebase

放弃之前rebase的操作

git rebase --abort

Merge tool to solve conflicts

合并工具,以解决冲突,详情配置请看:

git mergetool

To view the merge conflicts

查看合并冲突

# against base file
git diff --base $file
# against your changes
git diff --ours $file
# against other changes
git diff --theirs $file

To discard conflicting patch

丢弃冲突的补丁

git rebase --skip
git reset --hard

After resolving conflicts

在解决冲突后的继续执行

git add $conflicting_file
# do for all resolved files
git rebase --continue

Remote Update / Publish 远程更新/发布

List remotes

查看远程仓库地址

git remote -v

Show information

查看指定远程仓库的详细信息

git remote show remote

例:git remote show origin

Add remote

用于将一个新的远程仓库添加到你的 Git 项目中

git remote add path/url

git remote add <name> <path/url>

name 是你为远程仓库指定的名称,path/url 是远程仓库的路径或URL。例:

git remote add origin https://github.com/username/repository.git

Fetch changes

从指定的远程仓库中获取最新的提交和数据,但不会合并这些更改到你当前的工作分支

git fetch remote

语法:

git fetch remote <branch-name>:<local-branch-name>

  • remote 远程主机名
  • branch-name 要拉取远程分支名
  • local-branch-name 本地分支名

例:当我们想在远程仓库的一个分支上开发一个新需求时:

git fetch origin feature/template:feature/template_layout

此时,工作目录下,就会有feature/template_layout,在当前分支开发,完成后上传分支。

Feth + merge

拉取远程分支并合并指定分支的更改到当前分支

git pull remote branch

Publish local to remote

将本地分支推送到远程仓库

git push remote branch

Delete remote branch

删除远程分支

git push remote :branch

删除Git本地分支:注意,删除本地分支不会删除远程分支。

git branch -d "branch name"
# 或者
git branch --delete <branch>

删除Git远程分支:删除了远程仓库的分支,但其引用仍然存在于团队成员的本地代码仓库中。

git push remote -d "branch name"
# 或者
git push <remote_name> --delete <branch_name>
# Git版本早于1.7.0版本适用如下命令:
git push <remote_name> :<branch_name>

如果还想删除本地引用,如右:git remote prune origin删除远程上不存在的分支的引用。

Publish tags

git push origin/upstream --tags

Branching/Tagging 分支

这里推荐一篇文章,帮助我们更好的理解分支。

Git从安装到基本使用(保姆教程)

List branches

查看本地分支

git branch

Switch to branch

从当前分支,切换到其他分支

git checkout branch

创建并切换到新建分支

git checkout -b branch

Delete branch

删除分支

git branch -d branch

Tag current commit

用于在 Git 仓库中创建一个新的轻量级标签,标签是 Git 中用于标记特定提交的一种机制。它通常用于标记某个重要的里程碑,如发布版本号等。

git tag tagname

Useful Commands 有用的命令

Finding Regressions

使用二进制搜索查找引入错误的提交,这个命令使用二分搜索算法来查找项目历史中哪个提交引入了一个错误。

# $id is a broken version
git bisect bad $id
# to mark it as bad or good
git bisect bad/good
# $id is the last working version
git bisect good $id
# once you're done
git bisect reset
# to start
git bisect start
# to launch gitk and mark it
git bisect visualize

:假设你想找到破坏了一个已知在你的项目的 v2.6.13-rc2 版本中工作的特性的提交。你启动了一个 bisect 会话,如下所示:

git bisect start
git bisect bad                 # 当前版本是坏的
git bisect good v2.6.13-rc2    # v2.6.13-rc2 已知为好

一旦你指定了至少一个坏的和一个好的提交,git bisect 就会在这个历史范围的中间选择一个提交,检查它,并输出类似于以下的内容:

Bisecting: 675 revisions left to test after this (roughly 10 steps) | 在这之后,还有675次修订需要测试(大约10步)

你现在应该编译检出的版本并进行测试。如果该版本工作正常,请输入

git bisect good

如果该版本被破坏,请输入

git bisect bad

然后,git bisect会做出如下反应:

Bisecting: 337 revisions left to test after this (roughly 9 steps) | 在这之后还有337个修订要测试(大约9个步骤)。

不断重复这个过程:编译树,测试它,根据它是好是坏,运行 git bisect goodgit bisect bad 来要求下一个需要测试的提交。

最终,将没有更多的修订可以检查,该命令将打印出第一个坏提交的描述。引用 refs/bisect/bad 将被留在该提交处。

Check for Errors and Cleanup Repository

检查错误和清理存储库

# 验证数据库中对象的连接性和有效性
git fsck
# 清理不必要的文件,优化本地仓库
gc --prune

Search Working Directory for foo()

在工作目录中搜索foo()

git grep "foo()"

结尾!!

  • 27
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿选不出来

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值