代码改变世界

git 命令整理

2022-09-30 09:21  默默不语  阅读(17)  评论(0编辑  收藏  举报

Git

  • 工作区(working tree):本地编辑器
  • 暂存区(index): git add 操作后进入暂存区,可用 git status 查看
  • 本地仓库(repository): git commit 后进入本地仓库

  1. 克隆

    git clone

  2. 添加代码到暂存区

    git add filename/foldername

  3. 提交暂存区到本地仓库

    git commit -m commit_message

  4. 设置用户信息

    git config --global user.name ''
    git config --global user.email 
    
  5. push代码

    • git push 远程主机名 本地分支名:远程分支名
    • git push 远程主机名 本地分支名(本地和远程分支名相同)
  6. 将指定分支应用到其他分支

    git checkout branch1
    git cherry pick branch2
    将 branch2 的最新提交应用到 branch1 上
    
  7. 设置代理

    git config --local http.proxy http://xxxx.xxx.xx
    git config --local https.proxy http://xxxx.xxx.xx
    
  8. 取消代理

    git config --global --unset http.proxy
    git config --global --unset https.proxy
    
  9. 列出分支

    git branch

  10. 创建分支

    git branch branch_name

  11. 切换分支

    git checkout -b branch_name
    git checkout tags/<tag> -b <branch>
    
  12. 删除分支

    git branch -d branch_name

  13. 回退

    • git reset
      • --mixed:默认参数,重置暂存区文件与上一次提交(commit)保持一致,工作区文件保持不变
      • --soft:回退到某个版本
      • --hard:撤销工作区中所有未提交的修改内容,将暂存区和工作区的内容都回退到上一次的版本,并删除之前所有的提交信息(谨慎使用)
  14. 添加多个远程仓库

    • 添加一个新的远程仓库

      git remote add <name> <url>

    • 为指定仓库添加新的地址

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

  15. 删除远程仓库

    • 删除名为 的远程仓库

      git remote remove <name>

    • 删除 下的指定链接

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

  16. 修改 commit,去除上一次的 commit 记录, 修改 commit 信息

    git commit --amend

  17. 暂存没有提交的工作

    • 暂存未提交的内容: git stash
    • 列出所有暂存的工作: git stash list
    • 恢复某个暂时保存的内容: git stash apply stash@
    • 恢复最近一次 stash 的内容: git stash pop
    • 丢弃最近一次 stash 的内容: git stash drop
    • 清除所有 stash 的内容: git stash clear
  18. git config file

    [core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
    [remote "origin"]
        url = https://github.com/mattermost/mattermost-server.git
        fetch = +refs/heads/*:refs/remotes/origin/*
    
  19. 切换到某个 tag 上

    git checkout tag_name

  20. 从 tag 切到原分支

    git switch -