20 个 Git 命令将使您成为版本控制专家。

2025-05-24

20 个 Git 命令将使您成为版本控制专家。

对于希望在团队中高效协作并跟踪代码变更的程序员来说,版本控制至关重要。Git 是一个版本控制系统,它允许您跟踪修订、识别文件版本,并在必要时恢复旧版本。

具有一定编程经验的用户可以轻松上手 Git,但要掌握所有高级功能却并非易事。在本文中,我将向您展示一些最实用的命令,助您成为 Git 高手。

1. git 配置

git config是你必须知道的基本 Git 命令之一。该命令有助于设置电子邮件、用户名、文件格式、首选文件算法以及许多其他属性的配置值。该命令的示例如下:

# configure the user which will be used by Git
# this should be not an acronym but your full name
$ git config --global user.name "Firstname Lastname"
# configure the email address
$ git config --global user.email "your.email@example.org"
Enter fullscreen mode Exit fullscreen mode

2. git init

git init是 Git 的常用命令之一,非常适合初始化 Git 仓库。该命令用于在现有项目或新项目中创建初始 .git 目录。.git 文件夹处于隐藏状态,在 Windows 系统中,您必须禁用该功能才能看到它。在 Linux 系统中,您可以使用“ls –a”命令查看.git目录。建议不要篡改 .git 文件夹的内容。

$ git init <the name of your repository>
Enter fullscreen mode Exit fullscreen mode

3. git 克隆

此命令用于从现有 URL 获取存储库

$ git clone <the url of the repository>
Enter fullscreen mode Exit fullscreen mode

4. git add

“git add”命令用于将当前工作目录中的文件修改添加到用户索引中。该命令还用于添加准备提交到远程存储库的未跟踪文件。“git add”命令的使用示例如下。

$ git add myfile
Enter fullscreen mode Exit fullscreen mode

此命令将把 myfile 添加到暂存区。

5. git 分支

对于初学者来说,“git branch” 是 Git 命令中值得一提的。“branch” 命令可以帮助你创建、删除和列出分支。

该命令有一些重要选项:

. -v -a
提供有关所有分支的更多信息。默认情况下,列出分支只会显示本地分支的名称。

  • 添加“-a”标志将确保远程分支也包含在列表中。
    添加“-v”标志将使命令更加“详细”,并且

  • 包括 SHA-1 哈希以及分支上最新提交的提交主题。

— no-merged
返回所有尚未合并到当前 HEAD 分支的分支。

-d
删除指定的分支。

用法

#list all branches
$ git branch -a -v 
#Return all branches that has not merged
$ git branch --no-merged
#Return all branches thaat has merged
$ git branch --merged
Enter fullscreen mode Exit fullscreen mode

6. git 提交

git commit 命令捕获项目当前暂存更改的快照。

$ git commit -m “first commit”
Enter fullscreen mode Exit fullscreen mode

7. git push

git push命令可以帮助将所有修改过的本地对象推送到远程仓库,并扩展其分支。此命令的使用示例如下:

$ git push origin master
Enter fullscreen mode Exit fullscreen mode

8. git diff

git diff命令可用于创建补丁文件,或统计索引、工作目录或 Git 仓库中路径或文件之间的差异。此命令的使用示例如下:

$ git diff
Enter fullscreen mode Exit fullscreen mode

9. git 状态

git status ”命令可以帮助显示索引和工作目录中文件的状态。该命令可以轻松列出未跟踪、已修改和已暂存的文件。“git status”命令的使用示例如下:

$ git status
Enter fullscreen mode Exit fullscreen mode

10. git 显示

此命令显示指定提交的元数据和内容更改。

$ git show
Enter fullscreen mode Exit fullscreen mode

11. git 标签

此命令有助于使用简单、持久且易于理解的句柄标记特定提交。此命令的示例如下

git tag –a v2.0 –m ‘this is version 2.0 tag’
Enter fullscreen mode Exit fullscreen mode

12. git 合并

git merge ” 是一个强大的功能,它允许你将两个分支的工作合并为一个。当开发人员处理同一份代码,并希望在将更改推送到分支之前先将其集成到一起时,此功能非常有用。

$ git merge branch_name
Enter fullscreen mode Exit fullscreen mode

13. git 日志

git log ”命令列出项目中发生过的每一次提交,以查看随着时间的推移发生了什么变化,以及有关如何完成提交的一些其他信息。

$ git log
Enter fullscreen mode Exit fullscreen mode

14. git 重置

使用 git reset 来“取消跟踪”文件,使其不再具有任何指向 Git 存储库的链接。

$ git reset [commit id]
Enter fullscreen mode Exit fullscreen mode

15. git rm

此命令用于从当前工作目录中删除特定文件并暂存删除。要从当前工作目录中删除特定文件并暂存删除,请使用以下命令:

$ git rm <filename>
Enter fullscreen mode Exit fullscreen mode

16. git 远程

此命令用于将本地git仓库连接到远程服务器。

$ git remote add [variable name] [Remote Server Link]
Enter fullscreen mode Exit fullscreen mode

17. git fsck

此命令用于检查 Git 文件系统的完整性,也有助于识别损坏的对象。

$ git fsck
Enter fullscreen mode Exit fullscreen mode

18. git pull

此命令获取远程服务器上的更改并将其合并到您的工作目录。

$ git pull repository_link
Enter fullscreen mode Exit fullscreen mode

19. git 签出

git checkout ” 命令允许我们切换到现有分支,或者创建新分支并切换到新分支。为此,您要切换到的分支应该存在于本地系统中,并且在切换之前,当前分支中的更改应该已提交或存储。您也可以使用此命令检出文件。

# Switch to an existing branch:
$ git checkout <branch-name>
#Create and switch to a new branch
$ git checkout -b <branch-name>
Enter fullscreen mode Exit fullscreen mode

20. git 存储

该命令用于将所有改变的文件临时存储在工作目录中。

用法:临时保存所有修改过的跟踪文件:

$ git stash
Enter fullscreen mode Exit fullscreen mode

用法:列出所有存储:

$ git stash list
Enter fullscreen mode Exit fullscreen mode

用法:删除最新的存储:

$ git stash drop
Enter fullscreen mode Exit fullscreen mode

概括

这篇文章到此结束。现在您可以自称是版本控制专家了。但请记住,还有其他有用的 git 命令,Git 并不是唯一的版本控制工具。

感谢阅读!如有任何疑问或反馈,请在下方留言。

感谢您的阅读!
希望这篇文章对您有所帮助。请与您的朋友和同事分享。分享即关爱。

通过各种平台与我联系

文章来源:https://dev.to/devland/20-git-commands-that-will-make-you-a-version-control-pro-149p
PREV
HTMLrev 上有 1500 多个免费 HTML 网站模板
NEXT
适合初学者的 CSS 资源列表