以下是Git的常用指令:

1. git init

说明:初始化一个新的Git仓库。

例子:

$ mkdir my_project

$ cd my_project

$ git init

Initialized empty Git repository in /path/to/my_project/.git/

备注:在my_project目录下创建了一个新的Git仓库。

2. git add <file>

说明:将文件添加到暂存区。

例子:

$ echo "Hello, Git!" > hello.txt

$ git add hello.txt

备注:将hello.txt文件添加到暂存区,准备提交。

3. git commit -m "message"

说明:提交暂存区的更改到本地仓库。

例子:

$ git commit -m "Initial commit with hello.txt"

[master (root-commit) e3d7707] Initial commit with hello.txt

 1 file changed, 1 insertion(+)

 create mode 100644 hello.txt

备注:将hello.txt文件提交到本地仓库,并添加提交信息。

4. git status

说明:查看仓库状态。

例子:

$ echo "New line" >> hello.txt

$ 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:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

备注:显示hello.txt文件已修改但尚未添加到暂存区。

5. git log

说明:查看提交历史记录。

例子:

$ git log

commit e3d770755e1b2b9030575d1d1f2c55b759a530a0

Author: Your Name <your.email@example.com>

Date:   Mon Mar 1 10:00:00 2023 +0000

    Initial commit with hello.txt

备注:显示最新的提交信息。

6. git diff

说明:查看工作区与暂存区或HEAD的差异。

例子:

$ echo "Another line" >> hello.txt

$ git diff

diff --git a/hello.txt b/hello.txt

index e69de29..d81c3e0 100644

--- a/hello.txt

+++ b/hello.txt

@@ -1 +1,3 @@

+Hello, Git!

+New line

+Another line

备注:显示hello.txt文件在工作区中的修改与暂存区或HEAD的差异。

7. git branch <branch-name>

说明:创建新分支。

例子:

$ git branch feature-branch

备注:创建名为feature-branch的新分支。

8. git checkout <branch-name>

说明:切换到指定分支。

例子:

$ git checkout feature-branch

Switched to branch 'feature-branch'

备注:切换到feature-branch分支。

9. git merge <branch-name>

说明:将指定分支合并到当前分支。

例子:

$ git checkout master

$ git merge feature-branch

Updating e3d7707..new-hash

Fast-forward

 hello.txt | 2 ++

 1 file changed, 2 insertions(+)

备注:将feature-branch分支的更改合并到master分支。

10. git remote add <remote-name> <remote-url>

说明:添加远程仓库。

例子:

$ git remote add origin https://github.com/user/repo.git

备注:添加一个名为origin的远程仓库,其地址为https://github.com/user/repo.git。

11. git remote -v

说明:查看远程仓库的详细信息。

例子:

$ git remote -v

origin  https://github.com/user/repo.git (fetch)

origin  https://github.com/user/repo.git (push)

备注:显示远程仓库origin的URL地址,包括用于拉取(fetch)和推送(push)的URL。

12. git push <remote> <branch>

说明:推送本地分支到远程仓库。

例子:

$ git push origin feature-branch

Total 0 (delta 0), reused 0 (delta 0)

To https://github.com/user/repo.git

 * [new branch]      feature-branch -> feature-branch

备注:将本地的feature-branch分支推送到远程仓库origin的feature-branch分支。

13. git pull <remote> <branch>

说明:从远程仓库拉取并合并分支。

例子:

$ git pull origin master

remote: Counting objects: 3, done.

remote: Compressing objects: 100% (2/2), done.

remote: Total 3 (delta 1), reused 0 (delta 0)

Unpacking objects: 100% (3/3), done.

From https://github.com/user/repo.git

 * branch            master     -> FETCH_HEAD

Updating e3d7707..new-hash

Fast-forward

 hello.txt | 1 +

 1 file changed, 1 insertion(+)

备注:从远程仓库origin的master分支拉取最新的代码,并与当前分支合并。

14. git stash

说明:暂存当前工作区更改。

例子:

$ echo "Temporary change" >> hello.txt

$ git stash

Saved working directory and index state WIP on master: e3d7707 Initial commit with hello.txt

备注:将当前工作区的修改暂存起来,以便稍后恢复或丢弃。

15. git stash pop

说明:恢复暂存的更改并删除stash。

例子:

$ git stash pop

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:   hello.txt

no changes added to commit (use "git add" and/or "git commit -a")

Dropped refs/stash@{0} (f724a1a099999999999999999999999999999999)

备注:恢复之前暂存的更改到工作区,并删除对应的stash记录。

16. git reset <commit>

说明:将当前HEAD重置到指定提交,可以选择性地改变暂存区和工作区。

例子:

$ git reset --hard HEAD~1

HEAD is now at e3d7707 Initial commit with hello.txt

备注:将HEAD、暂存区和工作区都重置到上一个提交,丢弃之后的所有更改。

17. git revert <commit>

说明:创建一个新的提交,撤销之前的某个提交所做的更改。

例子:

$ git revert HEAD

[master 45a677e] Revert "Initial commit with hello.txt"

 1 file changed, 1 deletion(-)

 delete mode 100644 hello.txt

备注:创建一个新的提交,撤销上一次提交所做的更改,保留更改历史。

18. git tag <tag-name>

说明:为当前提交打上标签。

例子:

$ git tag v1.0

备注:为当前HEAD指向的提交打上标签v1.0。

19. git show <tag-name>

说明:查看标签的详细信息。

例子:

$ git show v1.0

tag v1.0

Tagger: Your Name <your.email@example.com>

Date:   Mon Mar 1 11:00:00 2023 +0000

Initial commit with hello.txt

commit e3d770755e1b2b9030575d1d1f2c55b759a530a0

Author: Your Name <your.email@example.com>

Date:   Mon Mar 1 10:00:00 2023 +0000

    Initial commit with hello.txt

备注:显示标签v1.0的详细信息,包括关联的提交信息。

20. git clone <repository-url>

说明:克隆远程仓库到本地。

例子:

$ git clone https://github.com/user/repo.git

Cloning into 'repo'...

remote: Counting objects: 3, done.

remote: Compressing objects: 100% (2/2), done.

remote: Total 3 (delta 0), reused 0 (delta 0)

Unpacking objects: 100% (3/3), done.

备注:将远程仓库https://github.com/user/repo.git克隆到本地,并创建一个与远程仓库同名的目录。

21. git bisect

说明:使用二分查找法确定导致问题的提交。

例子(开始二分查找):

$ git bisect start

$ git bisect bad <bad-commit>

$ git bisect good <good-commit>

备注:开始一个二分查找会话,指定一个坏的提交和一个好的提交,Git会自动在两者之间查找导致问题的提交。

22. git archive

说明:创建一个指定提交的归档文件。

例子:

$ git archive --format=zip HEAD > archive.zip

备注:将当前HEAD指向的提交的内容打包成archive.zip归档文件。

23. git reflog

说明:查看HEAD和其他引用日志。

例子:

$ git reflog

e3d7707 HEAD@{0}: reset: moving to HEAD~1

45a677e HEAD@{1}: revert: Revert "Initial commit with hello.txt"

e3d7707 HEAD@{2}: commit (initial): Initial commit with hello.txt

备注:显示HEAD和其他引用的历史记录,包括被重置或删除的提交。

24. git cherry-pick <commit>

说明:将某个提交复制到当前分支。

例子:

$ git cherry-pick abc123

[master 1a2b3c4] Commit message from abc123

 Date:   Mon Mar 1 11:00:00 2023 +0000

 1 file changed, 1 insertion(+)

备注:将提交abc123复制到当前分支,并创建一个新的提交。

25. git blame <file>

说明:显示文件每一行的最后修改信息。

例子:

$ git blame hello.txt

^abc123 (John Doe 2023-03-01 10:00:00 +0000 1) Hello, World!

备注:显示hello.txt文件中每一行的最后修改信息,包括提交的哈希值、作者和日期。

26. git submodule

说明:管理仓库中的子模块。

常用子命令:

git submodule add <repository> <path>:添加子模块。

git submodule init:初始化子模块。

git submodule update:更新子模块到最新的提交。

例子:

$ git submodule add https://github.com/user/submodule.git my_submodule

Cloning into '/path/to/repo/my_submodule'...

remote: Counting objects: 3, done.

remote: Compressing objects: 100% (2/2), done.

remote: Total 3 (delta 0), reused 0 (delta 0)

Unpacking objects: 100% (3/3), done.

备注:将一个外部仓库作为子模块添加到当前仓库的my_submodule目录下。

27. git sparse-checkout

说明:允许只检出仓库中的部分文件或目录。

常用子命令:

git config core.sparseCheckout true:启用稀疏检出。

echo "path/to/dir/" >> .git/info/sparse-checkout:设置要检出的目录。

例子:

$ git config core.sparseCheckout true

$ echo "docs/" >> .git/info/sparse-checkout

$ git checkout master

备注:只检出docs/目录下的文件,其他目录或文件不会检出。

28. git config

说明:获取和设置仓库或全局的选项。

常用子命令:

git config --global user.name "Your Name":设置全局用户名。

git config --global user.email "your.email@example.com":设置全局邮箱地址。

git config --list:列出所有配置。

例子:

$ git config --global user.name "John Doe"

$ git config --global user.email "john.doe@example.com"

备注:配置Git的全局用户信息,这些信息将用于提交记录。

29. git notes

说明:给提交添加或查看附注。

常用子命令:

git notes add -m "My note" <commit>:为指定提交添加附注。

git notes show <commit>:查看指定提交的附注。

例子:

$ git notes add -m "This is a bug fix" abc123

$ git notes show abc123

This is a bug fix

备注:为提交abc123添加一条附注,并随后查看这条附注。

30. git filter-branch

说明:重写整个提交历史。

常用场景:删除某个文件的所有提交历史、更改作者信息等。

警告:这是一个强大的命令,使用不当可能导致数据丢失。建议在操作前备份仓库。

例子(删除某个文件的所有提交历史):

$ git filter-branch --force --index-filter \

  "git rm --cached --ignore-unmatch path/to/file" \

  --prune-empty --tag-name-filter cat -- --all

备注:这个命令会删除提交历史等信息

30. git filter-branch

说明:重写整个提交历史。可以用来执行复杂的重写操作,比如从仓库中完全移除文件或更改提交者信息。

警告:这是一个强大的命令,并且具有破坏性。在使用之前,请确保你完全理解其影响,并且已经备份了仓库。

例子(从所有提交中移除某个文件):

git filter-branch --force --index-filter \

  "git rm --cached --ignore-unmatch path/to/file" \

  --prune-empty --tag-name-filter cat -- --all

备注:这个命令会移除所有提交中path/to/file文件的跟踪记录,并且会重写整个提交历史。

31. git merge --squash

说明:合并一个分支到当前分支,但不保留原分支的提交历史,而是将所有改动压缩成一个新的提交。

例子:

git checkout feature-branch

git merge --squash master

git commit -m "Squash merge of feature-branch into master"

备注:将feature-branch分支的更改合并到当前分支,但所有更改会被压缩成一个新的提交,而不是保留feature-branch的完整提交历史。

32. git range-diff

说明:显示两个提交范围之间的差异。

例子:

git range-diff HEAD..feature-branch master..main

1

备注:比较feature-branch分支自HEAD以来的更改与main分支自master以来的更改。

33. git credential-cache

说明:缓存凭证信息,以便在之后的请求中重用。

常用子命令:

git config --global credential.helper cache:启用凭证缓存。

git config --global credential.cache-timeout <seconds>:设置缓存超时时间。

例子:

git config --global credential.helper cache

git config --global credential.cache-timeout 3600

备注:这将启用凭证缓存,并设置缓存超时时间为1小时(3600秒)。在缓存有效期内,Git将重用之前保存的凭证信息,避免重复输入。

34. git difftool

说明:使用外部工具显示差异。

常用子命令:

git difftool --tool=<tool-name>:使用指定的工具显示差异。

例子:

git difftool --tool=meld

备注:使用meld工具来显示当前工作区与暂存区之间的差异。

35. git bundle

说明:打包一个或多个引用到一个单独的文件中,方便在不同仓库之间传输。

常用子命令:

git bundle create <file> <ref>...:创建一个包含指定引用的bundle文件。

git bundle verify <file>:验证bundle文件的完整性。

例子:

git bundle create my-bundle.bundle master

备注:创建一个包含master分支的引用信息的my-bundle.bundle文件。

36. git rev-parse

说明:获取并显示git对象的SHA-1值、引用或其他相关信息。

常用场景:获取当前HEAD的SHA-1值、解析引用名等。

例子:

$ git rev-parse HEAD

abcd1234567890abcdef1234567890abcdef123456

备注:该命令会输出HEAD指向的提交的SHA-1值。

37. git update-ref

说明:更新一个引用(比如分支或标签)所指向的对象。

常用场景:手动更新引用,如分支或标签的指向。

例子:

$ git update-ref refs/heads/master abcd12345678

备注:这条命令将master分支的引用更新为指向SHA-1值为abcd12345678的提交。

38. git fsck

说明:检查仓库的完整性,查找丢失或损坏的对象。

常用子命令:

--full:执行全面的仓库检查。

--unreachable:显示不可达的对象。

例子:

$ git fsc

1

备注:该命令将检查整个仓库,并显示任何损坏或不可达的对象。

39. git archive

说明:创建一个包含指定提交或分支内容的归档文件。

常用子命令:

--format=zip:创建ZIP格式的归档文件。

--prefix=prefix/:在归档文件中添加前缀。

例子:

$ git archive --format=zip --prefix=my-project/ --output=my-project.zip master

备注:该命令将创建一个ZIP格式的归档文件,其中包含master分支的内容,并在归档文件的路径前添加my-project/前缀。

40. git instaweb

说明:通过gitweb启动一个web服务器,以便通过浏览器查看仓库。

常用子命令:

--httpd=webrick:使用Ruby的WEBrick作为web服务器。

--port=<port>:指定web服务器监听的端口号。

例子:

$ git instaweb --httpd=webrick --port=8080

备注:该命令将在本地启动一个web服务器,并在浏览器中打开仓库的gitweb界面,以便查看提交历史、分支和标签等信息。

41. git sparse-checkout

说明:允许用户仅检出仓库中的部分文件或目录。

常用子命令:

git sparse-checkout init:初始化稀疏检出。

git sparse-checkout set <path>:设置要检出的文件或目录。

例子:

$ git sparse-checkout init

$ git sparse-checkout set dir1/file1 dir2/

备注:通过稀疏检出,用户可以选择只检出仓库中的特定文件或目录,这在处理大型仓库时特别有用,可以节省磁盘空间和时间。

42. git submodule

说明:管理仓库中的子模块。子模块允许你在一个Git仓库中嵌套另一个Git仓库。

常用子命令:

git submodule add <repository> <path>:在指定路径下添加子模块。

git submodule update:更新子模块到最新提交。

git submodule init:初始化子模块,设置默认的远程仓库URL。

例子:

$ git submodule add https://github.com/user/repo.git path/to/submodule

$ git submodule update --init

备注:子模块允许你管理第三方库或其他项目作为你主项目的一部分,同时保持它们各自的版本控制历史。

43. git notes

说明:给提交添加或查看注释。

常用子命令:

git notes add [-m <msg> | -F <file> | -e] [<object>]:给指定提交添加注释。

git notes show [<object>]:查看指定提交的注释。

例子:

$ git notes add -m "Bugfix for issue #123" abcd123

$ git notes show abcd123

备注:通过给提交添加注释,你可以为代码库中的特定提交添加额外的信息或元数据,如bug修复、功能实现等。

44. git replace

说明:替换仓库中的对象,如提交或树对象。

常用场景:修复历史中的错误,而不改变现有的引用。

例子:

$ git replace abcd123 1234567 --edit

备注:该命令将创建一个新的提交对象,它是原始提交abcd123的修改版本,并用新的提交对象替换它。这样,所有指向原始提交的引用实际上都会指向新的提交,但原始的引用(如分支名)保持不变。

45. git reflog

说明:显示本地仓库的引用日志,记录HEAD和所有分支的变动。

常用子命令:

git reflog show <ref>:显示特定引用的日志。

git reflog expire --expire=<time>:过期并删除旧的引用日志条目。

例子:

$ git reflog

备注:通过引用日志,你可以查看HEAD和分支的变动历史,包括被删除的提交。这对于恢复丢失的提交或回滚错误操作特别有用。

46. git bisect

说明:使用二分搜索法查找引入错误的提交。

常用子命令:

git bisect start:开始二分搜索。

git bisect bad <commit>:标记一个已知包含错误的提交。

git bisect good <commit>:标记一个已知不包含错误的提交。

git bisect skip:跳过当前提交,继续搜索。

git bisect reset:重置二分搜索状态。

例子:

$ git bisect start

$ git bisect bad HEAD

$ git bisect good v1.0

# Git 会自动切换到中间的提交,你需要测试并标记为 good 或 bad

$ git bisect reset

备注:git bisect是一个非常强大的工具,它可以帮助你快速定位并修复引入错误的提交。

47. git rebase

说明:重新应用一系列提交到一个新的基准点。

常用子命令:

git rebase <base>:将当前分支的提交重新应用到指定的基准点上。

git rebase --interactive <base>:交互式地重新应用提交。

git rebase --continue:在解决冲突后继续rebase操作。

git rebase --abort:取消rebase操作。

例子:

$ git rebase master

# 解决可能出现的冲突

$ git rebase --continue

# 或者,如果你想取消rebase

$ git rebase --abort

备注:git rebase常用于整理提交历史,将一系列提交重新应用到一个新的基准点上,使提交历史更加清晰。但请注意,在公共分支上使用rebase可能会引发问题,因为它会改变提交的SHA-1值。

48. git cherry-pick

说明:选择并应用一个或多个提交。

常用子命令:

git cherry-pick <commit>:应用指定的提交到当前分支。

git cherry-pick <commit1> <commit2>...:应用多个指定的提交。

例子:

$ git cherry-pick abcd123

1

备注:git cherry-pick允许你从一个分支选择并提交应用到另一个分支上,而不需要合并整个分支。

49. git worktree

说明:管理多个工作目录,它们共享同一个仓库的对象。

常用子命令:

git worktree add <path> <branch>:添加一个新的工作目录,并检出指定的分支。

git worktree list:列出所有的工作目录。

git worktree prune:删除所有不再需要的工作目录。

例子:

$ git worktree add ../feature-branch feature

备注:git worktree允许你在多个目录中同时工作,每个目录可以有不同的分支或提交。这对于同时处理多个特性或修复多个问题非常有用。

50. git config

说明:获取和设置仓库或全局配置变量。

常用子命令:

git config --global user.name "Your Name":设置全局用户名。

git config --global user.email "your.email@example.com":设置全局邮箱地址。

git config --list:列出所有配置。

例子:

$ git config --global user.name "John Doe"

$ git config --global user.email john.doe@example.com