git 代码管理常用命令

  • cd到希望存储代码的目录路径,新建本地仓库
    cd ~/<your path>
    git init
    
  • 配置用户名和邮箱
    git config user.name 'your name'
    git config user.email 'your email'
    
  • 在gitee平台创建工程对用的仓库
    略。。。
  • 指定仓库地址与名称
    git remote add origin https://gitee.com/<your name>/renran.git
    git push -u origin "远程分支名"
    
  • 使用ssh连接仓库
    ssh-keygen -t rsa -C 'your email'
    cat /home/<your path>/.ssh/id_rsa.pub     # 把公钥复制到码云平台上
    
    # 切换项目的连接地址
    git remote remove origin # 删除仓库地址
    git remote add origin git@gitee.comvetra/<your name>.git  # 新仓库地址
    
  • 提交代码
    git add .
    git commit -m "描述说明"
    git push -u origin 远程分支名
    
  • 撤销commit及add操作
    git reset --hard HEAD~1    # ~1表示最近一次commit
    
  • 查看当前分支
    git branch -a
    
  • 创建本地分支
    git fetch origin branch1:branch2      # 需要手动切换分支
    
  • 删除本地分支
    git branch -D 2.4.5    # 删除本地2.4.5分支 
    
  • 创建新的分支并提交新分支
    git checkout -b newB
    git push -u origin newB
    
  • 创建本地分支与远程分支关联
    git fetch --all
    git branch -a
    git checkout -b <Local Branch Name> origin/<Branch Name>
    
  • 创建本地分支与远程分支关联(常用)
    首先,获取远程所有分支
    git fetch 
    
    创建与远程分支关联的本地分支(可以同名,也可以不同名;建议同名,方便管理)
    git checkout -b 本地分支名 origin/远程分支名
    
  • 提交单个文件到分支
    # 不需要进行git add,直接进行commit
    git commit <file> -m "your comment"
    git push
    
posted @ 2021-12-22 17:21  vetra  阅读(44)  评论(0)    收藏  举报