目录
1. 初始化仓库
git init
- 功能:在一个目录中初始化一个新的 Git 仓库。
- 例子:
$ git init
Initialized empty Git repository in /path/to/your/project/.git/
2. 克隆仓库
git clone [URL]
- 功能:从远程仓库克隆一个项目到本地。
- 例子:
$ git clone https://github.com/username/repository.git
Cloning into 'repository'...
remote: Enumerating objects: 27, done.
remote: Counting objects: 100% (27/27), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 27 (delta 6), reused 27 (delta 6), pack-reused 0
Unpacking objects: 100% (27/27), done.
3. 添加文件到暂存区
git add [file]
- 功能:将文件添加到暂存区。
- 参数:
.
:添加当前目录下的所有文件。
- 例子:
$ git add file.txt
4. 提交更改
git commit -m "commit message"
- 功能:将暂存区的文件提交到仓库。
- 参数:
-a
:自动将所有已跟踪的文件添加到暂存区。-m
:添加提交信息。
- 例子:
$ git commit -m "Add new feature"
[master 1234567] Add new feature
1 file changed, 1 insertion(+)
5. 查看状态
git status
- 功能:显示工作区和暂存区的状态。
- 例子:
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: file.txt
no changes added to commit (use "git add" and/or "git commit -a")
6. 查看提交历史
git log
- 功能:显示提交历史记录。
- 参数:
-n
:显示最近的 n 次提交。--oneline
:每条记录显示在一行。
- 例子:
$ git log --oneline
1234567 Add new feature
abcdefg Fix bug
7. 查看文件差异
git diff
- 功能:显示工作区与暂存区之间的差异。
- 参数:
[file]
:指定文件。
- 例子:
$ git diff file.txt
diff --git a/file.txt b/file.txt
index 1234567..abcdefg 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,4 @@
Hello World
This is a test file.
+Added a new line.
8. 切换分支
git checkout [branch]
- 功能:切换到指定的分支。
- 参数:
-b
:创建并切换到新的分支。
- 例子:
$ git checkout -b new-feature
Switched to a new branch 'new-feature'
9. 创建新分支
git branch [branch]
- 功能:创建一个新的分支。
- 例子:
$ git branch new-feature
10. 删除分支
git branch -d [branch]
- 功能:删除指定的分支。
- 例子:
$ git branch -d new-feature
Deleted branch new-feature (was 1234567).
11. 合并分支
git merge [branch]
- 功能:将指定分支合并到当前分支。
- 例子:
$ git merge new-feature
Updating 1234567..abcdefg
Fast-forward
file.txt | 1 +
1 file changed, 1 insertion(+)
12. 拉取远程仓库的最新更改
git pull [remote] [branch]
- 功能:从远程仓库拉取最新的更改并合并到当前分支。
- 参数:
[remote]
:远程仓库的名称,默认为origin
。[branch]
:远程分支的名称。
- 例子:
$ git pull origin main
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/username/repository
1234567..abcdefg main -> origin/main
Updating 1234567..abcdefg
Fast-forward
file.txt | 1 +
1 file changed, 1 insertion(+)
13. 推送本地更改到远程仓库
git push [remote] [branch]
- 功能:将本地分支的更改推送到远程仓库。
- 参数:
[remote]
:远程仓库的名称,默认为origin
。[branch]
:远程分支的名称。
- 例子:
$ git push origin main
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 343 bytes | 343.00 KiB/s, done.
Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/username/repository.git
1234567..abcdefg main -> main
14. 查看远程仓库信息
git remote -v
- 功能:显示远程仓库的信息。
- 例子:
$ git remote -v
origin https://github.com/username/repository.git (fetch)
origin https://github.com/username/repository.git (push)
15. 添加远程仓库
git remote add [name] [URL]
- 功能:添加一个新的远程仓库。
- 例子:
$ git remote add upstream https://github.com/upstream/repo.git
16. 撤销未提交的更改
git checkout -- [file]
- 功能:撤销工作区中的更改,恢复到最近一次提交的状态。
- 例子:
$ git checkout -- file.txt
17. 撤销已提交的更改
git revert [commit]
- 功能:撤销指定的提交,生成一个新的提交来撤销更改。
- 例子:
$ git revert 1234567
[main 7654321] Revert "Add new feature"
1 file changed, 1 deletion(-)
18. 查看分支列表
git branch
- 功能:显示本地的所有分支。
- 例子:
$ git branch
* main
new-feature