zoukankan      html  css  js  c++  java
  • Git常用命令

    1. 安装Git,配置信息

    配置本机用户名,和邮箱

    git config --global user.name "<name>" 
    git config --global user.email "<email>"        
    

    2. 创建版本库

    cd到合适的目录

    # 初始化为git仓库
    git init
    

    3. 版本控制

    3.1 增删文件

    # 增加或更新监控文件(file为文件名,文件夹名, * . )
    git add <file>
    
    # 取消监控文件
    git rm <file>
    
    # 查看哪些文件使用clean后将删除
    git clean -n
    
    # 删除untracked(未监控)的文件,一般在pull后才会使用!
    git clean -f
    
    # 删除untracked(未监控)的文件和目录,一般在pull后才会使用!
    git clean -df
    

    3.2 本地提交

    # 查询工作空间状态
    git status
    
    # 查询不同
    git diff
    
    # 本地提交(desc简要描述这次的提交)
    git commit -m "<desc>"
    
    # (没add&commit)丢弃修改(file为文件名,文件夹名都可)
    git checkout -- <file>
    
    # (add&commit)丢弃暂存区修改
    git reset HEAD <file>
    

    3.3 回退

    # 查看日志(可以最后加数字,显示最近几个)
    git log
    
    # 查看简要日志
    git log --pretty=oneline
    
    # 回退到上一个版本
    git reset --hard^
    
    # 回退到上100个版本
    git reset --hard~100
    
    # 回退到版本号指定的版本,不需要输入完整
    git reset --hard <commit_version>
    

    3.4 远程库推送

    # 创建SSH Key,然后把id_rsa.pub添加到GitHub账户内
    ssh-keygen -t rsa -C "<email>"
    
    # 关联远程库(git用户名/远程库名)
    git remote add origin git@github.com:<gitname/rep>
    
    # 第一次推送至远程仓库(branch_name为master或分支名)
    git push -u origin <branch_name>
    
    # 推送至远程仓库
    git push origin <branch_name>
    
    # 从远程库克隆
    git clone <clone_address>
    
    # 拉取远端并合并本地仓库
    git pull
    
    # 合并多个commit,将后面的commit合并到前面,并重写commit message
    git rebase -i <commit_version>
    

    3.5 分支管理

    # 查看当前分支
    git branch
    
    # 拉取远程分支
    git fetch origin <origin_branch_name>:<local_branch_name>
    
    # 切换分支
    git checkout <branch_name>
    
    # 创建并切换分支
    git checkout -b <branch_name>
    
    # 删除分支
    git branch -d <branch_name>
    
    # 合并其他分支
    git merge <other_branch_name>
    
    # 合并其他分支的某个提交
    git cherry-pick <commit_version>
    
    # 推送分支到远程仓库
    git push origin <branch_name>
    git push origin <local_branch_name>:<origin_branch_name>
    
  • 相关阅读:
    Leetcode 58. 最后一个单词的长度 双指针
    Leetcode 125. 验证回文串 双指针
    拜托,大厂做项目可不简单!
    被问懵了:一个进程最多可以创建多少个线程?
    面对祖传屎山代码应该采用的5个正确姿势
    VUE代码格式化配置vetur、eslint、prettier的故事
    如何快速实现一个虚拟 DOM 系统
    NodeJS 进程是如何退出的
    [堆][启发式合并]luogu P3261 [JLOI2015]城池攻占
    [Trie][堆]luogu P5283 [十二省联考2019]异或粽子
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/11583773.html
Copyright © 2011-2022 走看看