Git 命令及示例

发布于:2024-04-10 ⋅ 阅读:(27) ⋅ 点赞:(0)

目录

1. 基础命令

2. 分支管理

3. 远程操作

4. 撤销修改

5. 标签

6. 高级命令


1. 基础命令

  • git init 初始化一个新的Git仓库。 示例:git init
  • git clone [url] 克隆一个仓库到本地。 示例:git clone https://github.com/user/repo.git
  • git add [file] 将文件添加到暂存区。 示例:git add README.md
  • git commit -m "[commit message]" 提交暂存区的内容到仓库。 示例:git commit -m "Initial commit"
  • git status 查看仓库当前的状态。 示例:git status
  • git log 查看提交历史。 示例:git log
  • git diff 查看未暂存的文件更新了哪些部分。 示例:git diff

2. 分支管理

  • git branch 列出所有本地分支。 示例:git branch
  • git branch [branch-name] 创建新分支。 示例:git branch feature-x
  • git checkout [branch-name] 切换到指定分支。 示例:git checkout feature-x
  • git merge [branch] 将指定分支合并到当前分支。 示例:git merge feature-x
  • git branch -d [branch-name] 删除分支。 示例:git branch -d feature-x

3. 远程操作

  • git remote add [shortname] [url] 添加一个新的远程仓库。 示例:git remote add origin https://github.com/user/repo.git
  • git fetch [remote-name] 从远程仓库下载新分支与数据。 示例:git fetch origin
  • git pull [remote-name] [branch-name] 从远程仓库提取数据并尝试合并到当前分支。 示例:git pull origin master
  • git push [remote-name] [branch-name] 将本地分支的更新推送到远程仓库。 示例:git push origin master

4. 撤销修改

  • git checkout -- [file] 放弃单个文件的修改。 示例:git checkout -- README.md
  • git reset [file] 从暂存区移除文件,但不影响文件当前的修改。 示例:git reset README.md
  • git reset --hard [commit] 撤销到指定提交,放弃所有修改。 示例:git reset --hard 0d1d7fc32

5. 标签

  • git tag 列出所有标签。 示例:git tag
  • git tag [tag-name] 创建一个新标签。 示例:git tag v1.0.0
  • git push [remote-name] [tag-name] 推送一个标签到远程仓库。 示例:git push origin v1.0.0

6. 高级命令

  • git stash 暂时存储未提交的修改,以便清理工作目录。 示例:git stash
  • git rebase [branch] 重新排列提交,可以用来清理提交历史。 示例:git rebase master
  • git cherry-pick [commit] 选择一个提交,将其作为一个新的提交引入当前分支。 示例:git cherry-pick 4a202b3