Skip to content

Git 命令大全

1、初始化本地仓库

git init <directory>

是可选的,如果不指定,将使用当前目录。

2.克隆一个远程仓库

git clone <url>

3.添加文件到暂存区

git add <file>

要添加当前目录中的所有文件,请使用 . 代替,代码如下:

git add .

4.提交更改

git commit -m "<message>"
git commit -m "<message>"

如果要添加对跟踪文件所做的所有更改并提交。

git commit -a -m "<message>"# orgit commit -am "<message>"
git commit -a -m "<message>"# orgit commit -am "<message>"

5.从暂存区删除一个文件

git reset <file>

6.移动或重命名文件

git mv <current path> <new path>
git mv <current path> <new path>

7.从存储库中删除文件

git rm <file>

您也可以仅使用 --cached 标志将其从暂存区中删除

git rm --cached <file>
git rm --cached <file>

8.基本 Git 概念

默认分支名称:main

默认远程名称:origin

当前分支参考:HEAD

HEAD 的父级:HEAD^ 或 HEAD~1

HEAD 的祖父母:HEAD^^ 或 HEAD~2

9.显示分支

git branch

有用的标志:

-a:显示所有分支(本地和远程)

-r:显示远程分支

-v:显示最后一次提交的分支

10.创建一个分支

git branch <branch>

你可以创建一个分支并使用 checkout 命令切换到它。

git checkout -b <branch>
git checkout -b <branch>

11.切换到一个分支

git checkout <branch>
git checkout <branch>

12.删除一个分支

git branch -d <branch>
git branch -d <branch>

您还可以使用 -D 标志强制删除分支。

git branch -D <branch>
git branch -D <branch>

13.合并分支

git merge <branch to merge into HEAD>
git merge <branch to merge into HEAD>

有用的标志:

--no-ff:即使合并解析为快进,也创建合并提交

--squash:将指定分支中的所有提交压缩为单个提交

建议不要使用 --squash 标志,因为它会将所有提交压缩为单个提交,从而导致提交历史混乱。

14.变基分支

变基是将一系列提交移动或组合到新的基本提交的过程。

git rebase <branch to rebase from>
git rebase <branch to rebase from>

15.查看之前的提交

git checkout <commit id>
git checkout <commit id>

16.恢复提交

git revert <commit id>
git revert <commit id>

17.重置提交

git reset <commit id>
git reset <commit id>

您还可以添加 --hard 标志来删除所有更改,但请谨慎使用。

git reset --hard <commit id>
git reset --hard <commit id>

18.查看存储库的状态

git status

19.显示提交历史

git log

20.显示对未暂存文件的更改

git diff\\

您还可以使用 --staged 标志来显示对暂存文件的更改。

git diff --staged

21.显示两次提交之间的变化

git diff <commit id 01> <commit id 02>
git diff <commit id 01> <commit id 02>

22.存储更改

stash 允许您在不提交更改的情况下临时存储更改。

git stash

您还可以将消息添加到存储中。

git stash save "<message>"
git stash save "<message>"

23.列出存储

git stash list

24.申请一个藏匿处

应用存储不会将其从存储列表中删除。

git stash apply <stash id>
git stash apply <stash id>

如果不指定,将应用最新的 stash(适用于所有类似的 stash 命令)

您还可以使用格式 stash@{} 应用存储(适用于所有类似的存储命令)

git stash apply stash@{0}
git stash apply stash@{0}

25.删除一个藏匿处

git stash drop <stash id>
git stash drop <stash id>

26.删除所有藏匿处

git stash clear

27.应用和删除存储

git stash pop <stash id>
git stash pop <stash id>

28.显示存储中的更改

git stash show <stash id>
git stash show <stash id>

29.添加远程仓库

git remote add <remote name> <url>
git remote add <remote name> <url>

30.显示远程仓库

git remote

添加 -v 标志以显示远程存储库的 URL。

git remote -v

31.删除远程仓库

git remote remove <remote name>
git remote remove <remote name>

32.重命名远程存储库

git remote rename <old name> <new name>
git remote rename <old name> <new name>

33.从远程存储库中获取更改

git fetch <remote name>
git fetch <remote name>

34.从特定分支获取更改

git fetch <remote name> <branch>
git fetch <remote name> <branch>

35.从远程存储库中拉取更改

git pull <remote name> <branch>
git pull <remote name> <branch>

36.将更改推送到远程存储库

git push <remote name>
git push <remote name>

37.将更改推送到特定分支

git push <remote name> <branch>
git push <remote name> <branch>

上次更新于: