Git 是现代软件开发中最重要的版本控制工具之一,它帮助开发者高效地管理项目,支持分布式协作和版本控制。无论是个人项目还是团队开发,Git 都能提供强大的功能来跟踪、管理代码变更,并保障项目的稳定性与可持续发展。本篇文章从基础命令讲起,逐步深入,帮助你全面了解并掌握 Git,最终达到精通。
Git 是一个分布式版本控制系统,用于跟踪文件的变化,尤其是程序代码的变化。它由 Linus Torvalds(Linux 操作系统的创建者)于 2005 年开发。Git 允许开发者在本地创建代码仓库,进行文件修改、提交等操作,直到准备好与远程仓库同步。Git 提供强大的分支管理功能,可以在不干扰主分支的情况下开发新功能或修复 bug。
Git 支持多种操作系统,包括 Windows、macOS 和 Linux。以下是不同操作系统的安装方法:
git --version
,如果显示 Git 的版本号,表示安装成功。brew install git
git --version
,如果显示 Git 的版本号,表示安装成功。sudo apt update
sudo apt install git
sudo yum install git
Git 安装完成后,第一次使用时需要进行一些基本配置,主要包括设置用户名和邮箱。
命令:
git config --global user.name ""
git config --global user.email ""
示例命令:
git config --global user.name "John Doe"
git config --global user.email "[email protected]"
效果:
git config --list
可以查看所有配置信息。命令:
git clone <repository_url>
示例命令:
git clone https://github.com/example/repo.git
效果:
Cloning into 'repo'...
remote: Counting objects: 50, done.
remote: Compressing objects: 100% (25/25), done.
remote: Total 50 (delta 15), reused 20 (delta 5), pack-reused 0
Unpacking objects: 100% (50/50), done.
命令:
git status
示例命令:
git status
效果:
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
命令:
git log
示例命令:
git log
效果:
commit 1234567890abcdef1234567890abcdef12345678
Author: John Doe <[email protected]>
Date: Tue Oct 28 18:32:01 2024 +0800
Fixed the bug in index.html
commit 0987654321abcdef0987654321abcdef09876543
Author: Jane Smith <[email protected]>
Date: Mon Oct 27 12:15:45 2024 +0800
Added new feature to main.js
命令:
git branch <branch_name>
示例命令:
git branch feature-xyz
效果:
feature-xyz
,但并不切换到该分支。命令:
git checkout <branch_name>
示例命令:
git checkout feature-xyz
效果:
feature-xyz
。命令:
git branch -d <branch_name>
示例命令:
git branch -d feature-xyz
效果:
feature-xyz
,如果分支已经合并到当前分支,则可以成功删除。如果没有合并,Git 会提示你需要使用 -D
强制删除。命令:
git merge <branch_name>
示例命令:
git merge feature-xyz
效果:
feature-xyz
分支的更改合并到当前分支。如果没有冲突,Git 会自动合并并创建一个新的提交。Updating abcdef1..1234567
Fast-forward
file1 | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
如果合并过程中出现冲突,Git 会标记出冲突文件,需要手动解决冲突后再提交。
命令:
git commit -m ""
示例命令:
git commit -m "Fix bug in header component"
效果:
[master 1234567] Fix bug in header component
1 file changed, 2 insertions(+), 1 deletion(-)
命令:
git diff
示例命令:
git diff
效果:
diff --git a/index.html b/index.html
index abcdef1..1234567 100644
--- a/index.html
+++ b/index.html
@@ -1,3 +1,3 @@
- Welcome to My Website
+ Welcome to Our Website
Git 使用暂存区来暂存更改,等待提交。你可以使用 git add
将修改的文件添加到暂存区,然后使用 git commit
提交更改。
命令:
git add .
示例命令:
git add .
效果:
git commit
提交这些更改。命令:
git add <file_name>
示例命令:
git add index.html
效果:
index.html
文件的更改添加到暂存区,准备提交。分支是 Git 中一个非常强大的功能,它允许你在不影响主分支(通常是 master
或 main
)的情况下进行开发。每个分支都是代码的一个独立版本,开发者可以在不同分支上并行开发,最后再将它们合并。
命令:
git branch <branch_name>
示例命令:
git branch feature-login
效果:
feature-login
的新分支,但不切换到该分支。命令:
git checkout <branch_name>
示例命令:
git checkout feature-login
效果:
feature-login
分支,开始在该分支上进行开发。命令:
git branch -d <branch_name>
示例命令:
git branch -d feature-login
效果:
feature-login
分支。如果该分支已经合并到当前分支,删除将成功。如果尚未合并,Git 会发出警告。当开发者在不同分支上进行开发并且最终需要将这些更改合并时,Git 提供了合并操作。合并时,如果两个分支修改了相同的部分,Git 会提示冲突,并要求开发者手动解决。
命令:
git merge <branch_name>
示例命令:
git merge feature-login
效果:
feature-login
分支的更改合并到当前分支。如果没有冲突,Git 会自动完成合并,并创建一个新的提交。如果合并时发生冲突,Git 会标记出冲突文件,开发者需要手动编辑冲突部分,解决后再执行提交。
查看冲突文件:
git status
编辑冲突文件,删除冲突标记,修改内容。
暂存解决冲突的文件:
git add <conflicted_file>
完成合并并提交:
git commit
Git 的基本工作流包括以下步骤:
Git Flow 是一种非常流行的 Git 工作流,适用于持续集成和团队合作的项目。Git Flow 使用几个固定的分支来管理开发周期:
在开源项目中,通常使用 Fork 和 Pull Request(PR)工作流。开发者可以 fork 一个远程仓库,修改自己的分支,最后通过 Pull Request 将修改提交给原仓库的所有者进行合并。
git merge
和 git rebase
都用于合并分支,但它们的工作方式不同。
命令:
git rebase <branch_name>
示例命令:
git rebase master
效果:
master
分支的顶部,从而产生一个线性的提交历史。Git 使用标签(Tag)来标记特定的提交点,通常用于标记发布版本。
命令:
git tag <tag_name>
示例命令:
git tag v1.0
效果:
v1.0
的标签。在 Git 中,.gitignore
文件用于指定 Git 不需要跟踪的文件或目录。它通常用于忽略临时文件、编译产物、操作系统生成的文件等不需要版本控制的内容。合理使用 .gitignore
能有效保持 Git 仓库的整洁,避免不必要的文件进入版本控制系统。
.gitignore
文件命令:
echo "" >> .gitignore
示例命令:
echo "node_modules/" >> .gitignore
效果:
node_modules/
目录添加到 .gitignore
文件中,表示 Git 将忽略所有 node_modules
目录下的文件。.gitignore
文件不存在,它会自动创建并将指定的内容追加到文件末尾。cat .gitignore
可以看到文件内容:$ cat .gitignore
node_modules/
注意事项:
.gitignore
是一个文本文件,你可以手动编辑该文件来添加其他需要忽略的文件或目录模式。>>
操作符会将内容追加到 .gitignore
文件,如果该文件已经存在,内容将不会被覆盖。.gitignore
文件在项目开发中,可能会有多个文件需要被忽略,或者更改忽略规则。你可以手动编辑 .gitignore
文件,添加新的忽略规则。
命令:
nano .gitignore
示例命令:
nano .gitignore
效果:
.gitignore
文件,允许你进行手动编辑。你可以在文件中添加或删除需要忽略的文件或目录模式。*.log
*.tmp
node_modules/
dist/
这样,所有 .log
、.tmp
文件以及 node_modules/
和 dist/
目录下的文件都会被 Git 忽略。提示:
nano
命令时,编辑完文件后需要按 Ctrl + O
保存文件,按 Ctrl + X
退出编辑器。如果某个文件已经被 Git 跟踪,但之后你希望忽略它,可以通过修改 .gitignore
文件来实现。需要先将该文件从 Git 的索引中移除,然后才能正确忽略。
命令:
git rm --cached <file_name>
示例命令:
git rm --cached node_modules/
效果:
node_modules/
目录从 Git 的索引中移除,但不会删除本地文件。node_modules/
目录将被 .gitignore
忽略,不再被 Git 跟踪。git status
会看到该文件或目录从暂存区移除的提示:$ git status
Changes to be committed:
(use "git restore --staged ..." to unstage)
deleted: node_modules/
注意事项:
--cached
参数仅会从 Git 的索引中删除文件,文件本身会保留在本地工作区。.gitignore
文件和 git rm
操作:git commit -m "Remove node_modules from version control"
在 .gitignore
文件中,可以使用不同的模式来指定忽略哪些文件或目录。以下是常见的忽略规则:
*
:匹配零个或多个字符。例如,*.log
会忽略所有 .log
文件。/*
:匹配目录中的所有文件。例如,build/*
会忽略 build/
目录下的所有文件。!
:在忽略规则中,使用 !
来指定不忽略某个文件或目录。例如,!important.log
会保留 important.log
文件。#
:注释行以 #
开头,Git 会忽略注释行。例如,# Ignore logs
。命令:
echo "*.log" >> .gitignore
echo "!important.log" >> .gitignore
效果:
*.log
忽略所有 .log
文件。!important.log
保留 important.log
文件,即使它符合 .log
模式。注意事项:
.gitignore
中添加规则时,一定要注意规则的顺序,因为后面的规则会覆盖前面的规则。例如,如果先添加 *.log
忽略所有 .log
文件,再添加 !important.log
来保留 important.log
。.gitignore
配置以下是一些常见的 .gitignore
配置,适用于不同的开发环境和工具。
.gitignore
# 忽略 node_modules 目录
node_modules/
# 忽略编译的日志文件
npm-debug.log*
# 忽略临时文件
*.tmp
*.swp
.gitignore
# 忽略虚拟环境目录
venv/
# 忽略 Python 编译的文件
*.pyc
*.pyo
.gitignore
# 忽略 Maven 构建输出目录
target/
# 忽略 IntelliJ IDEA 配置文件
.idea/
# 忽略 Eclipse 配置文件
.project
.classpath
.gitignore
文件一旦你修改了 .gitignore
文件并确保它包含了所有需要忽略的文件和目录,记得将它提交到 Git 仓库,以便其他开发者也能够使用相同的忽略规则。
命令:
git add .gitignore
git commit -m "Add .gitignore to ignore unnecessary files"
git push origin <branch_name>
示例命令:
git add .gitignore
git commit -m "Add .gitignore to ignore node_modules"
git push origin main
效果:
.gitignore
文件添加到暂存区,提交到版本控制中,并推送到远程仓库。.gitignore
文件将自动应用到他们的本地仓库,确保不再跟踪忽略的文件。命令:
git log
示例命令:
git log
效果:
显示当前分支的提交历史,包括每个提交的哈希值、提交作者、提交时间和提交信息。输出格式如下:
commit 1234567890abcdef1234567890abcdef12345678 (HEAD -> master)
Author: John Doe <[email protected]>
Date: Thu Oct 28 12:15:42 2024 +0000
Fix bug in header component
commit abcdef1234567890abcdef1234567890abcdef12
Author: Jane Smith <[email protected]>
Date: Wed Oct 27 15:22:08 2024 +0000
Add new feature to handle user login
额外选项:
git log --oneline
:简洁模式,只显示提交的哈希值和提交信息。git log --graph
:以图形化方式显示提交历史。Git 提供了多种方式撤销更改,视不同的场景而定。
命令:
git checkout -- <file_name>
示例命令:
git checkout -- index.html
效果:
index.html
中的未暂存更改,恢复到上次提交时的状态。命令:
git reset <file_name>
示例命令:
git reset index.html
效果:
index.html
文件从暂存区移除,但不影响文件内容。如果你已经执行了 git add
,此命令将取消暂存,但不会丢失文件中的更改。命令:
git reset --hard <commit_hash>
示例命令:
git reset --hard abcdef1234567890abcdef1234567890abcdef12
效果:
abcdef1234567890abcdef1234567890abcdef12
,并且丢弃所有当前工作目录中的更改。请谨慎使用,因为此命令会删除所有未提交的更改。命令:
git remote -v
示例命令:
git remote -v
效果:
origin
)和 URL 地址。输出示例如下:origin https://github.com/example/repo.git (fetch)
origin https://github.com/example/repo.git (push)
命令:
git pull
示例命令:
git pull origin master
效果:
master
分支拉取最新的更改并自动合并到本地当前分支。如果没有冲突,Git 会自动合并。remote: Enumerating objects: 20, done.
remote: Counting objects: 100% (20/20), done.
remote: Compressing objects: 100% (14/14), done.
From https://github.com/example/repo
* branch master -> FETCH_HEAD
Updating abcdef1..1234567
Fast-forward
file1 | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
命令:
git push <remote_name> <branch_name>
示例命令:
git push origin master
效果:
将本地 master
分支的提交推送到远程仓库。确保本地分支与远程分支一致后,执行推送操作。
终端输出:
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 329 bytes | 329.00 KiB/s, done.
To https://github.com/example/repo.git
1234567..89abcdef master -> master
命令:
git clone <repository_url>
示例命令:
git clone https://github.com/example/repo.git
效果:
origin
指向该仓库。Cloning into 'repo'...
remote: Enumerating objects: 123, done.
remote: Counting objects: 100% (123/123), done.
remote: Compressing objects: 100% (90/90), done.
remote: Total 123 (delta 10), reused 90 (delta 6), pack-reused 0
Receiving objects: 100% (123/123), 3.64 MiB | 2.50 MiB/s, done.
Resolving deltas: 100% (10/10), done.
Git 子模块允许你将一个 Git 仓库作为另一个 Git 仓库的子目录。它特别适合将第三方库或依赖项目引入到你的项目中。
命令:
git submodule add <repository_url> <path>
示例命令:
git submodule add https://github.com/example/other-repo.git submodules/other-repo
效果:
submodules/other-repo
)下。命令:
git submodule update --remote
示例命令:
git submodule update --remote
效果:
Git 钩子是 Git 中的一个机制,允许在执行某些操作前或后运行自定义脚本。常见的钩子包括 pre-commit
和 post-commit
。
命令:
cd .git/hooks
示例命令:
cd .git/hooks
效果:
.git/hooks
目录,所有的钩子脚本都存放在该目录中。pre-commit
命令:
echo "#!/bin/sh" > .git/hooks/pre-commit
echo "echo 'Running pre-commit hook...'" >> .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
效果:
pre-commit
钩子,每次提交之前,都会运行这个脚本。"Running pre-commit hook..."
,并且该钩子需要执行权限。Git 允许你为常用命令设置别名,帮助提高工作效率。
命令:
git config --global alias.<alias_name> <git_command>
示例命令:
git config --global alias.co checkout
效果:
git checkout
命令设置别名 git co
,以后可以使用 git co
来代替 git checkout
。命令:
git config --list
示例命令:
git config --list
效果:
Git LFS 用于管理大型文件(如二进制文件、视频文件等),它将这些大文件存储在 Git 外部的专用服务中,从而避免仓库变得过于庞大。
命令:
git lfs install
示例命令:
git lfs install
效果:
Git LFS initialized.
命令:
git lfs track "*.psd"
示例命令:
git lfs track "*.psd"
效果:
.psd
文件类型的文件,将它们存储在 Git LFS 中。Tracking "*.psd" to Git LFS.
命令:
git add <file_name>
git commit -m "Add large file"
示例命令:
git add image.psd
git commit -m "Add large PSD file"
效果:
image.psd
)添加到 Git LFS 管理的文件中,然后提交。命令:
git branch -d <branch_name>
示例命令:
git branch -d feature-xyz
效果:
feature-xyz
,如果该分支未完全合并到当前分支,Git 会阻止删除。使用 -D
强制删除:git branch -D feature-xyz
命令:
git push <remote_name> --delete <branch_name>
示例命令:
git push origin --delete feature-xyz
效果:
feature-xyz
分支。命令:
git merge <branch_name>
示例命令:
git merge feature-xyz
效果:
feature-xyz
分支的修改合并到当前分支。如果没有冲突,Git 会自动合并修改并创建一次新的提交。命令:
git status
示例命令:
git status
效果:
both modified
,需要手动编辑冲突文件。解决冲突步骤:
打开冲突文件,手动解决冲突。Git 会在冲突部分标记出不同的修改,类似如下:
<<<<<<< HEAD
// Your changes here
=======
// Changes from the other branch
>>>>>>> feature-xyz
在解决冲突后,标记为已解决:
git add <file_name>
完成合并并提交:
git commit
命令:
git clone <repository_url>
git checkout -b <new_branch>
git add <file_name>
git commit -m "Your commit message"
git push origin <new_branch>
示例命令:
git clone https://github.com/example/repo.git
git checkout -b feature-xyz
git add index.html
git commit -m "Add new feature to index"
git push origin feature-xyz
效果:
Git Flow 是一种常见的 Git 工作流程,它定义了多个分支类型和对应的管理策略,旨在简化团队协作。
命令:
git flow init
示例命令:
git flow init
效果:
命令:
git flow feature start <feature_name>
示例命令:
git flow feature start feature-xyz
效果:
feature-xyz
,用于开发新功能。命令:
git flow feature finish <feature_name>
示例命令:
git flow feature finish feature-xyz
效果:
feature-xyz
分支的开发,并将其合并回 develop
分支。命令:
git flow release start <release_name>
示例命令:
git flow release start 1.0.0
效果:
1.0.0
,用于准备发布版本。Git 提供了 stash
命令,用于将当前的更改暂时保存,之后可以恢复,以便切换到其他任务。
命令:
git stash
示例命令:
git stash
效果:
命令:
git stash list
示例命令:
git stash list
效果:
stash@{0}: WIP on master: 1234567 Add feature X
stash@{1}: WIP on feature-xyz: abcdef0 Fix bug Y
命令:
git stash apply <stash_index>
示例命令:
git stash apply stash@{0}
效果:
命令:
git stash drop <stash_index>
示例命令:
git stash drop stash@{0}
效果:
rebase
用于将一组提交应用到另一个分支上,常用于整理提交历史。
命令:
git rebase <base_branch>
示例命令:
git rebase master
效果:
master
分支的最新提交上。Git Bisect 是一个二分查找工具,用于定位某个 bug 引入的提交。
命令:
git bisect start
示例命令:
git bisect start
效果:
命令:
git bisect good <commit_hash>
git bisect bad <commit_hash>
示例命令:
git bisect good abcdef1234567890
git bisect bad 1234567890abcdef
效果:
Git 是一种强大的版本控制工具,掌握了其基本命令和高级特性后,可以大大提高开发效率并方便团队协作。通过本篇文章,你已经学会了 Git 的核心命令及其使用方法,希望你能够灵活运用 Git 在实际项目中进行版本管理。
- 本节内容已经全部介绍完毕,希望通过这篇文章,大家对
Git
有了更深入的理解和认识。- 感谢各位的阅读和支持,如果觉得这篇文章对你有帮助,请不要吝惜你的点赞和评论,这对我们非常重要。再次感谢大家的关注和支持!点我关注❤️