Git常用命令

212 阅读3分钟

Git 是一个强大的版本控制系统,广泛应用于软件开发中。以下是一些常用的 Git 命令,涵盖了版本管理、分支操作、远程仓库等方面:

1. 基础操作

  • git init
    初始化一个新的 Git 仓库。

    git init
    
  • git clone <repository_url>
    克隆远程仓库到本地。

    git clone https://github.com/user/repo.git
    
  • git status
    查看当前工作目录和暂存区的状态,显示哪些文件被修改、哪些文件被暂存。

    git status
    
  • git add <file>
    将修改的文件添加到暂存区。

    git add file.txt
    
    • git add .:添加当前目录及子目录中的所有文件。
  • git commit -m "message"
    提交暂存区的文件到本地仓库,并附上提交信息。

    git commit -m "提交信息"
    
  • git push
    将本地提交推送到远程仓库。

    git push origin main
    
  • git pull
    拉取远程仓库的最新代码,并自动合并。

    git pull origin main
    
  • git fetch
    从远程仓库获取更新,但不自动合并。

    git fetch origin
    
  • git log
    查看提交历史。

    git log
    

2. 分支管理

  • git branch
    列出、创建或删除分支。

    • git branch:列出所有分支。
    • git branch <branch_name>:创建新分支。
    • git branch -d <branch_name>:删除本地分支。
  • git checkout <branch_name>
    切换到指定分支。

    git checkout dev
    
  • git checkout -b <branch_name>
    创建并切换到一个新分支。

    git checkout -b feature
    
  • git merge <branch_name>
    合并指定分支到当前分支。

    git merge dev
    
  • git rebase <branch_name>
    将当前分支的修改基于指定分支之上,常用于清理提交历史。

    git rebase main
    
  • git branch -m <old_branch_name> <new_branch_name>
    重命名分支。

    git branch -m old_branch new_branch
    

3. 远程仓库操作

  • git remote -v
    查看远程仓库的URL。

    git remote -v
    
  • git remote add <remote_name> <repository_url>
    添加一个新的远程仓库。

    git remote add origin https://github.com/user/repo.git
    
  • git remote remove <remote_name>
    删除一个远程仓库。

    git remote remove origin
    
  • git push -u <remote_name> <branch_name>
    推送并设置上游分支,使得以后推送时可以使用 git push

    git push -u origin main
    
  • git pull origin <branch_name>
    从远程仓库拉取指定分支。

    git pull origin main
    

4. 版本回退

  • git reset <commit_id>
    将当前分支回退到指定提交,并可以选择保留或丢弃修改。

    • git reset --hard <commit_id>:回退并丢弃工作区的修改。
    • git reset --soft <commit_id>:回退并保留修改在暂存区。
    • git reset --mixed <commit_id>:回退并保留修改在工作区。
    git reset --hard abc1234
    
  • git revert <commit_id>
    创建一个新的提交,用于撤销指定的提交。

    git revert abc1234
    
  • git checkout -- <file>
    恢复工作区文件到上次提交的状态,丢弃未提交的修改。

    git checkout -- file.txt
    

5. 暂存区操作

  • git stash
    将当前修改(包括暂存区和工作区)存储起来,以便后续恢复。

    git stash
    
  • git stash list
    查看所有暂存的更改。

    git stash list
    
  • git stash apply
    恢复最近的暂存更改。

    git stash apply
    
  • git stash pop
    恢复并删除最近的暂存更改。

    git stash pop
    

6. 文件操作

  • git rm <file>
    删除文件,并将删除操作添加到暂存区。

    git rm file.txt
    
  • git mv <old_name> <new_name>
    重命名文件。

    git mv old_name.txt new_name.txt
    

7. 查看差异

  • git diff
    查看工作区和暂存区之间的差异。

    git diff
    
  • git diff --staged
    查看暂存区和最近提交之间的差异。

    git diff --staged
    

8. 标签管理

  • git tag
    查看所有标签。

    git tag
    
  • git tag <tag_name>
    创建一个新标签。

    git tag v1.0
    
  • git push origin <tag_name>
    推送标签到远程仓库。

    git push origin v1.0
    
  • git push origin --tags
    推送所有本地标签到远程仓库。

    git push origin --tags
    
  • git tag -d <tag_name>
    删除本地标签。

    git tag -d v1.0
    

9. 查看历史

  • git log
    查看提交历史。

    git log
    
  • git log --oneline
    查看简洁的提交历史。

    git log --oneline
    
  • git log --graph
    查看提交历史的图形化表示。

    git log --graph
    

10. Git 配置

  • git config --global user.name "Your Name"
    配置 Git 用户名。

    git config --global user.name "Your Name"
    
  • git config --global user.email "youremail@example.com"
    配置 Git 用户邮箱。

    git config --global user.email "youremail@example.com"
    

这些命令是 Git 最常用的命令,涵盖了版本控制、分支管理、远程操作等核心功能。掌握这些命令后,你就可以更高效地使用 Git 来管理你的代码库。