Git 是一个开源的分布式版本控制系统,它可以帮助开发者跟踪代码的变更历史。以下是一些常用的 Git 命令行操作:

  1. 初始化仓库

    git init
    
  2. 克隆远程仓库

    git clone [url]
    
  3. 添加文件到暂存区

    git add [file]
    
  4. 提交更改

    git commit -m "[commit message]"
    
  5. 查看提交历史

    git log
    
  6. 查看当前状态

    git status
    
  7. 拉取远程仓库的更新

    git pull [remote] [branch]
    
  8. 推送本地更改到远程仓库

    git push [remote] [branch]
    
  9. 查看分支

    git branch
    
  10. 创建新分支

    git branch [branch-name]
    
  11. 切换分支

    git checkout [branch-name]
    
  12. 合并分支

    git merge [branch-name]
    
  13. 删除分支

    git branch -d [branch-name]
    
  14. 解决合并冲突
    当 Git 无法自动合并时,你需要手动解决冲突,然后再次提交。

  15. 查看远程仓库

    git remote -v
    
  16. 添加远程仓库

    git remote add [remote-name] [url]
    
  17. 重命名分支

    git branch -m [old-branch-name] [new-branch-name]
    
  18. 查看文件差异

    git diff
    
  19. 撤销工作目录的更改

    git checkout -- [file]
    
  20. 查看当前配置

    git config --list
    
  21. 设置全局配置

    git config --global user.name "[your name]"
    git config --global user.email "[your email]"
    
  22. 创建标签

    git tag [tag-name]
    
  23. 删除标签

    git tag -d [tag-name]
    
  24. 推送标签到远程仓库

    git push origin [tag-name]
    
  25. 检出特定标签

    git checkout [tag-name]
    

这些命令是 Git 操作的基础,通过它们你可以进行日常的版本控制任务。