• git常用命令


    git使用文档

    建立项目

    1. 新建项目

      进入gitlab.git
      选择LDAP,用自己的域账号登录
      点击右上角的 加号(+)新建项目
      填写项目名称
      选择组为 Online_Web
      “Visibility Level”是项目权限,可以根据项目需要自行选择
      点击 Creat Project
      
    2. 完善结构

      按照页面提示的命令在终端操作即可
      
            git config --global user.name "your name"
            git config --global user.email "your email"
    
            mkdir test
            cd test
            git init
            touch README
            git add README
            git commit -m 'first commit'
            git remote add origin git@gitlab.xxx/test.git
    
            git push -u origin master
    
    
        需要注意的是,如果没有指定host,需要修改命令中gitlab的地址
    
    1. 添加ssh key

      点击右上角的 人形 图标
      选择上面导航栏的 SSH Keys
      点击右上角的 Add SSH Keys 进入下一页
      点击 the SSH help page 进入帮助页面
      点击左侧的 SSH 即可
      
      按照页面提示操作,在终端输入以下命令
      
            ssh-keygen -t rsa -C "$your_email"
            cat ~/.ssh/id_rsa.pub
    
    
        将显示的内容粘贴到 添加SSH Key的页面
        添加SSH Key就是建立本机和gitlab之间的信任关系,以便以后在获取代码和提交代码时不用输入
        用户名和密码
    

    开发流程

    1. 获取代码

      git clone http://gitlab.xxx.git
      
    2. 提交代码

      git status   //先查看一下状态
      git commit -a -m "提交的说明" //提交到本地
      git push //提交到远程服务器
      
      注意:第一次提交的时候,最好提交.gitignore文件
      
    3. 更新代码

      git fetch
      git merge origin/master  (或者是对应的分支)
      
      注意:尽量不要使用 git pull
    4. 回滚代码

      git log  // 找到要回滚的版本
      git reset --hard 版本号   // 回滚到指定版本
      
      有关git log 推荐使用:
      git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s     %Cgreen(%cr)%Creset' --abbrev-commit --date=relative
      
    5. 处理冲突

      git 发生冲突的情况比较少见,一旦出现可以根据不同的类型查询帮助文档即可
      

    持续集成

    1. 构建hudson

      请参照web_bus项目进行配置
      http://hudson.corp.elong.com:8080/view/%E7%BD%91%E7%AB%99/job/web-bus/configure
      
    2. 生成部署包

      在hudson里点击 Build with Parameters
      修改为新的版本号
      点击 Build 开始打包
      

    上线流程

    1. tag相关

      //创建版本号为1.0.0的版本
      git tag -a v1.0.0 -m “标签的描述”
      //显示tag列表
      git tag -l
      //删除指定标签
      git tag -d v1.0.0 
      //将指定标签提交到git服务器
      git push origin v1.0.0
      
    2. 分支相关

      //创建分支
      git branch name
      //切换分支
      git checkout name
      //创建+切换分支
      git checkout -b name
      //合并某分支到当前分支
      git merge name
      //删除分支
      git branch -d name
      //删除远程分支
      git push origin : name
      //查看分支
      git branch
      
    3. 开发流程

      默认会创建master分支
      完成第一次上线后,在master上打tag
      然后根据tag创建develop分支
      常规开发都在develop上进行
      每次上线之后需要将代码合并到master上,然后打tag
      
      如果在两次常规上线之间需要修复bug并且上线
      可以根据master上一个tag创建一个fixbug分支
      上线fixbug分支后,需要合并到master上并且打tag
      同时需要合并到develop分支上
      然后根据需要可以选择删除fixbug分支

    git : 添加、查看和删除远端项目地址

    添加远端项目地址
    $ git remote add origin git@github.com:xxx/xxx.git

    查看远端项目地址
    $ git remote -v
    origin git@github.com:xxx/xxx.git (fetch)
    origin git@github.com:xxx/xxx.git (push)

    删除远端项目地址
    $ git remote rm origin

    git : 回滚到任意版本

    查看以往3次log记录

    $ git log -3

      

    回滚到指定的版本
    $ git reset --hard e377f60e28c8b84158

    回退到上个版本
    $ git reset --hard HEAD^

    回退到前3次提交之前,以此类推,回退到n次提交之前
    $ git reset --hard HEAD~3

    退到/进到 指定commit的sha码
    $ git reset --hard commit_id

      

    强制提交
    $ git push -f origin master

    强推到远程
    $ git push origin HEAD --force

    撤销指定文件到指定版本

    # 查看指定文件的历史版本
    git log <filename>
    # 回滚到指定commitID
    git checkout <commitID> <filename>

    删除最后一次远程提交

    方式一:使用revert

    git revert HEAD
    git push origin master

    方式二:使用reset

    git reset --hard HEAD^
    git push origin master -f

    git统计项目中各成员代码量

    查看git上个人代码量
    git log --author="username" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s ", add, subs, loc }' -

    统计每个人的增删行数
    git log --format='%aN' | sort -u | while read name; do echo -en "$name "; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s ", add, subs, loc }' -; done

    查看仓库提交者排名前 5
    git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5

    贡献者统计:
    git log --pretty='%aN' | sort -u | wc -l

    提交数统计:
    git log --oneline | wc -l

    回滚代码:
    git reset --hard cad0324fc462898033e1372724c0418ee651b1f7

    git连接新仓库并提交代码步骤

    1. git init //初始化仓库
    2. git add .(文件name) //添加文件到本地仓库
    3. git commit -m "first commit" //添加文件描述信息
    4. git remote add origin + 远程仓库地址 //链接远程仓库,创建主分支
    5. git pull origin master // 把本地仓库的变化连接到远程仓库主分支
    6. git push -f origin master //把本地仓库的文件推送到远程仓库

    怎么更换git远程仓库地址

    方法一 : 通过命令直接修改远程仓库地址
    git remote 查看所有远程仓库
    git remote xxx 查看指定远程仓库地址
    git remote set-url origin 你新的远程仓库地址

    方法二: 先删除在添加你的远程仓库
    git remote rm origin
    git remote add origin 你的新远程仓库地址

  • 相关阅读:
    国外优秀的icon设计站点
    HDU 2289 Cup (二分)
    HDU 1709 The Balance (母函数 * *)
    HDU 2152 Fruit (母函数)
    POJ 3294 Life Forms (后缀数组)
    HDU 2152 选课时间(题目已修改,注意读题) (母函数)
    HDU 3278 Puzzle (蛋疼。。。。)
    HDU The Rotation Game
    HDU 2069 Coin Change (母函数 | 背包 )
    HDU 2899 Strange fuction (二分)
  • 原文地址:https://www.cnblogs.com/zhuochong/p/9895798.html
Copyright © 2020-2023  润新知