Git命令

  • Post author:
  • Post category:其他


  1. 初始化本地仓库
git init
  1. 克隆一个远程仓库
git clone <url>
  1. 添加文件到暂存区
git add <file>

要添加当前目录中的所有文件,请使用

.

代替file,代码如下:

git add .
  1. 提交更改
git commit -m "提示信息"
  1. 从暂存区删除一个文件
git  reset <file>
  1. 移动或重命名文件
git mv <current path> <new path>
  1. 从存储库中删除文件
git rm <file>

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

git rm --cached <file>

基本 Git 概念

  1. 默认分支名称:main

  2. 默认远程名称:origin

  3. 当前分支参考:HEAD

  4. HEAD 的父级:HEAD^ 或 HEAD~1

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

  6. 显示分支

git branch

有用的标志:

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

-r:显示远程分支

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

  1. 创建一个分支
git branch <branch>

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

git checkout -b <branch>
  1. 切换到一个分支
git checkout <branch>
  1. 删除一个分支
git branch -d <branch>

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

git branch -D <branch>
  1. 合并分支
git merge <branch to merge into HEAD>

有用的标志:

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

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

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

  1. 变基分支

    变基是将一系列提交移动或组合到新的基本提交的过程。
git rebase <branch to rebase from>
  1. 查看之前的提交
git checkout <commit id>
  1. 恢复提交
git revert <commit id>
  1. 重置提交
git reset <commit id>

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

git reset --hard <commit id>
  1. 查看存储库的状态
git status
  1. 显示提交历史
git log
  1. 显示对未暂存文件的更改
git diff

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

git diff --staged
  1. 显示两次提交之间的变化
git diff <commit id 01> <commit id 02>
  1. 存储更改

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

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

git stash save "<message>"
  1. 列出存储
git stash list
  1. 申请一个藏匿处

    应用存储不会将其从存储列表中删除。
git stash apply <stash id>

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

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

git stash apply stash@{0}
  1. 删除一个藏匿处
git stash drop <stash id>
  1. 删除所有藏匿处
git stash clear
  1. 应用和删除存储
git stash pop <stash id>
  1. 显示存储中的更改
git stash show <stash id>
  1. 添加远程仓库
git remote add <remote name> <url>
  1. 显示远程仓库
git remote

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

git remote -v
  1. 删除远程仓库
git remote remove <remote name>
  1. 重命名远程存储库
git remote rename <old name> <new name>
  1. 从远程存储库中获取更改
git fetch <remote name>
  1. 从特定分支获取更改
git fetch <remote name> <branch>
  1. 从远程存储库中拉取更改
git pull <remote name> <branch>
  1. 将更改推送到远程存储库
git push <remote name>
  1. 将更改推送到特定分支
git push <remote name> <branch>



版权声明:本文为moligc原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。