Git 命令(简化你的开发🤗)或

2025-05-26

Git 命令(简化您的开发🤗)

或者

Git 是大多数程序员常用的版本控制系统 (VCS)。在这篇博客中,我将分享一些使用 Git 基本命令的技巧,让使用 Git 更加轻松。

首先要了解 git 的使用方法,即系统本地是如何运作的。

Git 中的工作流程

这张图有助于理解在本地使用 git 时实际发生的情况。首先从工作区开始,工作区存放的是 git 未处理的文件,也就是所有未跟踪文件。暂存区存放的是下次提交的文件。仓库包含所有项目提交。使用仓库,我们可以查看某个时间点的文件。

Git 命令:

1. 在你的目录中初始化 git

您需要做的第一步是初始化存储库。

git init
Enter fullscreen mode Exit fullscreen mode

2. git 配置

此命令可帮助您分别设置用于提交的用户名和用户电子邮件地址。

git config --global user.name "Shivam164"
git config --global user.email "sp...oo6@gmail.com"
Enter fullscreen mode Exit fullscreen mode

3. git 状态

该命令用于查看工作区和暂存区的状态。

 git status
Enter fullscreen mode Exit fullscreen mode

3. git add

此命令用于将文件添加到暂存区。

要将工作区中所有未跟踪的文件转移到暂存区,我们使用:

git add .
Enter fullscreen mode Exit fullscreen mode

此处“.”指定暂存所有未跟踪的文件。

要指定应该跟踪的文件,我们可以使用它们的文件名:

git add abc.txt
Enter fullscreen mode Exit fullscreen mode

仅为了暂存 abc.txt。

4. git 提交

git commit 命令会捕获项目阶段性更改的快照。通常,我们会在每次提交后保留一条简短的信息,以便将来以及其他人理解该提交。

⇒ 最好保持提交为现在时且简短(约 50 个字符)。

git commit -m "first commit"
Enter fullscreen mode Exit fullscreen mode

这里的“-m”表示消息。
要暂存所有更改并在单个命令中提交,我们可以使用:

git commit -am "committing all changes"
Enter fullscreen mode Exit fullscreen mode

在某些情况下,你无法用几句话解释你的工作,即使在这种情况下,我们也不应该只用一行代码写很长的信息,因为在 GitHub 上阅读这些信息会很困难。对于这种情况,在暂存某个文件后,我们不会像上面那样直接提交。我们会这样写:

git commit
Enter fullscreen mode Exit fullscreen mode

当我们编写不带 -m 的 git commit 时,您将看到一个名为 Vim 的控制台文本编辑器。

Vim

现在您可以在这里开始编写提交信息了,只需按下 I(插入)即可。即使在长信息中,也应该在开头到长信息之前保留一行简短的内容,然后在一行空格之后编写长信息。编写完信息后,您可以先按“ESC”键,然后按“:wq”键保存并退出,以关闭此 Vim。

Git 长提交信息

其他有用的 git commit 命令之一:

git commit --amend -m "commit message'
Enter fullscreen mode Exit fullscreen mode

此命令将本次提交与上一次提交合并,并将产生一条新的提交消息。

5. git 日志

git log 命令显示存储库历史记录中的所有提交。

有很多种方法可以查看提交历史记录:

  • 最基本的:显示从结束到开始的所有提交,以及与提交相关的所有信息。

    git log
    

    它显示基于 HEAD 的提交。

  • 要查看最近提交的一些数量:

    git log -5
    

    查看最后 5 次提交。

  • 要查看从某个提交到其他提交的提交:我们使用这些提交的哈希值。

    git log af156ff..32509d0
    
  • 要查看提交的状态:

    git log --stat
    

    此命令提供基本信息以及每次提交之间所做更改的数量摘要。

  • 最方便的 git log 命令之一:

    git log --all --oneline --graph --decorate
    

    ⇒ all:查看所有提交

    ⇒ oneline:将所有提交的数据保存在一行内。

    ⇒ 图表:以图形形式保存提交,在处理分支时非常有用。

    ⇒ 装饰:使其在视觉上更好。

6. git 显示

git show 命令将有助于查看特定提交的详细信息。

git show 1c16945
Enter fullscreen mode Exit fullscreen mode

git show

7. git diff

git diff 命令是查看不同提交之间的区别。

git diff 1c16945..9dcff6
Enter fullscreen mode Exit fullscreen mode

查看哈希值为 1c16945 和 9dcff6 的提交之间的区别。

git diff

我们也可以直接使用它。

git diff
Enter fullscreen mode Exit fullscreen mode

这将显示工作区和目录的文件的差异。

我们可以使用--staged 和 git diff

git diff --staged
Enter fullscreen mode Exit fullscreen mode

这将显示工作区和暂存区文件的差异。

8. git 存储

git stash 暂时搁置(或存储)您对工作副本所做的更改,以便您可以处理其他事情,然后返回并稍后重新应用它们。

只能存储暂存区中的文件。


git stash
Enter fullscreen mode Exit fullscreen mode

使用 git stash 之后,如果我们使用命令 git status 我们将不会在暂存区中看到任何文件。

我们还可以在使用 git stash 时发出一条消息,以便我们以后能够回忆起对存储的文件所做的事情。

git stash save "changed title"
Enter fullscreen mode Exit fullscreen mode

git 存储

查看所有隐藏的更改:

git stash list
Enter fullscreen mode Exit fullscreen mode

要查看某些特定的存储,假设索引为 1

git stash show -p stash@{1}
Enter fullscreen mode Exit fullscreen mode

要检索存储更改:

  • 检索最后存储的零钱。
git stash pop
Enter fullscreen mode Exit fullscreen mode
  • 使用特定索引检索存储更改
git stash pop stash@{1}
Enter fullscreen mode Exit fullscreen mode

要删除隐藏的更改:

  • 删除所有
git stash clear
Enter fullscreen mode Exit fullscreen mode
  • 删除一些特定的
git stash drop stash@{0}
Enter fullscreen mode Exit fullscreen mode

9. git 分支

git branch 命令用于处理 git 中的分支。

创建新分支:

  • 从 HEAD 当前指向的分支:

    git branch newBranch
    
  • 从 HEAD 当前未指向的分支:

    git branch newBranch fromBranch
    
  • 创建新分支并切换到该分支:

    git checkout -b newBranch fromBranch
    

切换到不同的分支:

git checkout toSwitchBranchName
Enter fullscreen mode Exit fullscreen mode

比较不同的分支:

git diff master..new_feature
Enter fullscreen mode Exit fullscreen mode

重命名分支:

要更改分支的名称,HEAD 当前指向:

git branch -m newBranchName
Enter fullscreen mode Exit fullscreen mode

要更改分支的名称而不考虑 HEAD:

git branch -m oldName newName
Enter fullscreen mode Exit fullscreen mode

删除分支

git branch -d branch_to_delete
Enter fullscreen mode Exit fullscreen mode
  • 如果我们当前正处于尝试删除的分支上,我们将会收到错误。

    所以我们必须确保我们不在我们想要删除的分支上。

  • 如果我们要删除的分支已取消合并且有提交,则此命令将不起作用。Git 这样做是为了防止意外删除分支。

    所以我们需要使用“-D”而不是“-d”

10. git 合并

git merge 命令允许您将 git branch 创建的独立开发线集成到单个分支中。

git merge feature/Nav
Enter fullscreen mode Exit fullscreen mode

上述命令将把 feature/Nav 分支与当前签出的分支合并。

合并分支时的一些良好做法,你可以在这里查看

目前它只包含与本地文件处理相关的命令。我很快就会更新它。👊

文章来源:https://dev.to/shivam164/git-commands-to-ease-your-development--5a2h
PREV
最佳 React 实践
NEXT
Web开发中的设计模式简介 让我们开始吧!结语