常用Git操作

Git是一个广泛使用的分布式版本控制系统。以下是一些常用的Git命令和用法:

Git基础操作

  1. 初始化仓库:

    git init
    
  2. 克隆仓库:

    git clone <repository-url>
    
  3. 检查仓库状态:

    git status
    
  4. 添加文件到暂存区:

    git add <file>
    
  5. 提交更改:

    git commit -m "Commit message"
    
  6. 查看提交历史:

    git log
    
  7. 创建新分支:

    git branch <branch-name>
    
  8. 切换分支:

    git checkout <branch-name>
    
  9. 合并分支:

    git merge <branch-name>
    
  10. 推送更改到远程仓库:

    git push <remote> <branch>
    
  11. 从远程仓库拉取更改:

    git pull <remote> <branch>
    
  12. 查看当前分支:

    git branch
    
  13. 删除分支:

    git branch -d <branch-name>
    
  14. 创建标签:

    git tag <tag-name>
    
  15. 查看标签:

    git tag
    
  16. 查看文件差异:

    git diff <file>
    
  17. 撤销未提交的更改:

    git checkout -- <file>
    
  18. 撤销已提交的更改:

    git revert <commit>
    
  19. 删除文件:

    git rm <file>
    
  20. 移动或重命名文件:

    git mv <old-file> <new-file>
    

Git远程命令操作

Git提供了一系列远程命令,用于与远程仓库进行交互和同步。以下是一些常用的Git远程命令操作:

  1. 添加远程仓库:

    git remote add <remote-name> <remote-url>
    
  2. 查看远程仓库:

    git remote -v
    
  3. 重命名远程仓库:

    git remote rename <old-name> <new-name>
    
  4. 删除远程仓库:

    git remote remove <remote-name>
    
  5. 获取远程仓库的更新:

    git fetch <remote-name>
    
  6. 拉取远程仓库的更改并合并到当前分支:

    git pull <remote-name> <branch-name>
    
  7. 推送本地分支到远程仓库:

    git push <remote-name> <branch-name>
    
  8. 推送本地标签到远程仓库:

    git push <remote-name> <tag-name>
    
  9. 推送所有本地分支到远程仓库:

    git push <remote-name> --all
    
  10. 推送所有本地标签到远程仓库:

    git push <remote-name> --tags
    
  11. 设置本地分支跟踪远程分支:

    git branch --set-upstream-to=<remote-name>/<branch-name>
    
  12. 查看远程分支:

    git branch -r
    
  13. 删除远程分支:

    git push <remote-name> --delete <branch-name>
    
  14. 获取远程仓库的详细信息:

    git remote show <remote-name>
    
  15. 更新远程仓库的URL:

    git remote set-url <remote-name> <new-url>
    

Git配置

Git提供了一系列配置命令,用于设置和管理Git的各种配置选项。以下是一些常用的Git配置命令:

  1. 设置全局用户名:

    git config --global user.name "Your Name"
    
  2. 设置全局邮箱地址:

    git config --global user.email "your@email.com"
    
  3. 设置本地仓库用户名:

    git config user.name "Your Name"
    
  4. 设置本地仓库邮箱地址:

    git config user.email "your@email.com"
    
  5. 查看全局配置:

    git config --global --list
    
  6. 查看本地仓库配置:

    git config --local --list
    
  7. 查看所有配置(全局和本地):

    git config --list
    
  8. 设置默认文本编辑器:

    git config --global core.editor "vim"
    
  9. 设置默认分支名称:

    git config --global init.defaultBranch "main"
    
  10. 设置命令别名:

    git config --global alias.st status
    git config --global alias.co checkout
    git config --global alias.ci commit
    
  11. 配置换行符:

    git config --global core.autocrlf true  # 在Windows上
    git config --global core.autocrlf input  # 在Unix/Linux上
    
  12. 配置大小写敏感:

    git config --global core.ignorecase false
    
  13. 配置推送策略:

    git config --global push.default simple
    
  14. 配置SSL验证:

    git config --global http.sslVerify false
    
  15. 配置代理服务器:

    git config --global http.proxy <http://proxy.example.com:8080>
    

请注意,--global选项用于设置全局配置,适用于当前用户的所有Git仓库。如果你想为特定仓库设置不同的配置,可以在仓库目录下运行没有--global选项的命令,这将仅对该仓库生效。

可以使用git config --list命令查看当前的所有配置,包括全局配置和本地仓库特定的配置。这有助于你了解当前的Git配置状态。

提交代码到你远端仓库

第1步:创建SSH Key。在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsaid_rsa.pub这两个文件,如果已经有了,可直接跳到下一步。如果没有,打开Shell(Windows下打开Git Bash),创建SSH Key:

$ ssh-keygen -t rsa -C "youremail@example.com"

如果一切顺利的话,可以在用户主目录里找到.ssh目录,里面有id_rsaid_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。

第2步:登陆GitHub,打开“Account settings”,“SSH Keys”页面:

然后,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容:然后执行

git init
git add README.md
git commit -m "first commit"
git branch -M <branch-name>
git remote add  <remote-name> <remote-url>
git push <remote-name> <branch-name>
  • 17
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值