zoukankan      html  css  js  c++  java
  • Git 的简单使用

    git 简单使用

    • git clone

      git@github.com:Break0504/git-test.git

    • 强制覆盖本地

      git fetch --all && git reset --hard origin/master

    • 提交到主干

      git add . && git status && git commit -a -m "提交的一些说明" && git push --all origin
      git add . && git status && git commit -a -m "update" && git push --all origin
      git push --all origin && git push --tags

    • 添加多个远程仓库地址

    比如我有2个地址

    git@github.com:Break0504/start.git
    git@gitee.com:Break0504/doc.git

    先添第一个地址

    git remote add origin git@github.com:Break0504/start.git
    

    再添第二个地址,更多地址也类似

    git remote set-url --add origin git@gitee.com:Break0504/doc.git
    

    提交所有

    git push origin --all
    
    • 查看、创建分支 test

      git branch
      git branch develop
      git branch release
      git branch hotfix

    • 提交分支

      git push origin develop

    • 切换分支

      git checkout test
      git checkout master

    • 合并分支
      把 development 合并到 master

      git checkout master
      git merge develop

    • 删除分支

      git branch -a
      git branch -d test/3.0.1 && git push origin --delete test/3.0.1
      git branch -D hotfix/3.0.5

    • 删除tag

      git tag -d 3.0.1 && git push origin --delete tag 3.0.1
      git tag

    git lib

    • 新项目

      git clone http://ip地址/test.git
      cd test
      touch README.md
      git add README.md
      git commit -m "add README"
      git push -u origin master

    • 老项目提交到远程的空仓库

      cd existing_folder
      git init
      git remote add origin http://ip地址/test.git
      git add .
      git commit -m "Initial commit"
      git push -u origin master

    • 切换远程仓库

      cd existing_repo
      git remote rename origin old-origin
      或者 git remote remove old-origin
      git remote add origin http://ip地址/ds/test.git
      git push -u origin --all
      git push -u origin --tags

    配置

    • 查看配置,系统、用户、某项目本地仓库

      git config --system --list
      git config --global --list
      git config --local --list

    • 设置本地用户

      git config --global user.name "用户名"
      git config --global user.email "用户邮箱"

    • .gitignore 配置参考
      请特别注意,一定要配置 .gitignore文件(git 提交过滤一些无用的文件)

    ## --------------------------------------------------------------------
    ## Refresh .gitignore file
    ## git rm -r --cached . && git add . && git commit -m 'update .gitignore'
    ## --------------------------------------------------------------------
    
    .svn/
    *.svn
    
    ## Java
    *.class
    *.war
    
    
    ## Dubbo
    .dubbo-registry/
    
    ## Python
    *.py[cod]
    *.egg
    *.egg-info
    ## *.so
    dist
    build
    
    
    ## Logs File
    *.log
    logs/
    */logs/
    
    ## Gradle
    .gradle/
    gradle/
    gradlew
    gradlew.bat
    build/
    */build/
    bin/
    */bin/
    /data/release/
    */data/release/
    
    ## Maven
    target/
    */target/
    
    ## Eclipse Project files
    .project
    .classpath
    .settings/
    
    ## IntelliJ IDEA Project files
    .idea
    .idea/
    *.iml
    *.ipr
    *.iws
    out/
    */out/
    
    ## NodeJs
    node_modules/
    public/
    
    ## Vue
    _vux*/
    
    ## MacOS
    .DS_Store
    
    ## Windows
    Thumbs.db
    ehthumbs.db
    Desktop.ini
    
    
    *.bak
    *.temp
    *.tmp
    
    • 换行符
      团队统一规范代码的换行符为类 UNIX 格式,即 LF

    windows、ma c系统都请配置

    ## 提交时转换为LF,检出时不转换
    ## 拒绝提交包含混合换行符的文件
    git config --global core.autocrlf input
    git config --global core.safecrlf true
    

    eclipse、idea 等 ide 开发工具配置换行符为 LF,请自行百度谷歌,谢谢

    mac 下可以使用 dos2unix 批量转换某目录下的文件换行符为 LF

    brew install dos2unix
    
    cd /target
    find . -name '*' -exec dos2unix {} ;
    
    或者只是转换指定的后缀文件
    find . -name '*.java' -exec dos2unix {} ;
    find . -name '*.xml' -exec dos2unix {} ;
    find . -name '*.pro*ies' -exec dos2unix {} ;
    

    windows 的 git 命令行工具也自带 dos2unix,同样可以使用上面(或下面)命令,来转换当前目录下的所有文件,换行符改为 LF

    find . -type f -exec dos2unix {} ;
    

    git flow 开发流

    1. 简单介绍

    https://www.cnblogs.com/cnblogsfans/p/5075073.html

    https://www.git-tower.com/learn/git/ebook/cn/command-line/advanced-topics/git-flow

    工具

    mac 或者 linux 推荐终端命令使用 git flow
    macOS 安装:brew install git-flow
    windows 推荐使用图形工具 
    
    1. source tree 图形工具 https://www.sourcetreeapp.com

    2. eclipse 原生支持,但需要开启:

      help,install new software
      选择对应版本的地址,比如目前2018.4月的最新版 oxygen 对应的是 http://download.eclipse.org/releases/oxygen
      然后搜索 git flow,勾选 GitFlow support,安装重启即可

    3. git messege 格式 git-message.md

      git commit -m 'init:初始化'
      git commit -m 'feat(#user):添加登录功能'
      git commit -m 'opt(#user):优化登录提示功能'
      git commit -m 'fix(#user):修复登录异常功能'
      git commit -m 'test(#user):添加测试用户逻辑,记得删除,不推荐'
      git commit -m 'test(#user):删除测试用户逻辑'
      git commit -m 'refactor(#ALL):重构成dubbo项目'
      git commit -m 'docs:添加前端调用文档'
      git commit -m 'docs(#user):添加登录接口文档'

    master 分支

    项目负责人、admin 才能修改该远程分支。其他人只有查看、下载权,不能提交

    develop 分支

    开发人员可以修改该远程分支

    其他

    feature 分支
    开发人员可以修改,不推荐提交发布到远程仓库
    release 分支
    项目负责人创建,push 到远程仓库以方便下载测试、修改
    测试人员下载测试
    开发人员下载修改、提交
    项目负责人创建结束、删除本地和远程仓库

    注意

    • master 跟 develop 分支是长期存在 origin 远程仓库里的
    • 其他类似 feature、release 分支,不推荐 push 到 origin 远程仓库里
    • 即使 release 分支测试的时候要 push 到远程 origin 里,也请在测试完成后删掉远程仓库里的分支

    权限

    这里是指远程仓库 gitlab 的权限(guest、reporter、developer、master)
    其实还有 admin 和 owner (项目的创建人)
    在 gitlab 创建项目,master 分支默认是受保护的

    developer 权限

    开发人员配置该角色
    

    master 权限

    项目负责人配置该角色
    

    git merge 合并与冲突解决

    中断合并

    git merge feature/test1
    Auto-merging hello.java
    CONFLICT (content): Merge conflict in hello.java
    Automatic merge failed; fix conflicts and then commit the result.
    ...
    

    有时冲突文件太多,没办法(或不想立即)一个个手动解决。完全可以通过 git merge --abort 来简单地退出合并

    git status -sb
    git merge --abort
    git status -sb
    

    当然,也可以使用回退来处理,参考 简单回退

    单边合并

    git merge -Xtheirs 或者 git merge -Xours
    

    若有冲突,使用对方文件覆盖本地文件

    feature/test1 合并到 develop。使用 feature/test1 分支 覆盖 合并到 develop

    git checkout develop
    git merge -Xtheirs feature/test1
    

    误删文件夹,还原到某个提交点,相当于创建新文件,不会冲突

    git checkout 3e48555
    

    强制合并

    如果分支合并时出现了冲突,而你的 remote 仓库是 github 或者 bitbucket,那么只能强上了

    git branch -m master old-master
    git branch -m develop master
    git checkout master
    git push -f origin master        
    

    简单回退

    参考 http://gitbook.liuhui998.com/4_9.html

    develop 分支,回退到本地仓库的某个历史提交版本,比如 commit id 为 d373d3c

    git checkout develop
    git reset --hard d373d3c
    

    若同时需要覆盖远程仓库,则强制提交

    git push origin develop –-force
    

    撤销

    撤销最近一次提交

    git revert HEAD
    

    撤销上上次提交

    git revert HEAD^
    

    修改最近的提交 message

    git commit --amend
    

    删除历史文件

    参考 https://help.github.com/articles/removing-sensitive-data-from-a-repository/

    https://gitlab.com/gitlab-org/gitlab-ce/issues/30093

    https://gitlab.com/gitlab-org/gitlab-ce/issues/19376

    https://www.cnblogs.com/shines77/p/3460274.html

    使用 git filter-branch,比较慢,推荐下面的bfg

    获取所有远程分支
    git branch -r | grep -v '->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
    git pull --all
    
    删除历史文件或文件夹 data/release,注意文件路径前面不能有/
    git filter-branch --force --index-filter 'git rm --cached -r --ignore-unmatch data/release' --prune-empty --tag-name-filter cat -- --all
    
    推到远程仓库
    git push origin --force --all
    git push origin --force --tags
    
    清理和回收空间
    du -h .git
    git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
    git reflog expire --expire=now --all
    git gc --prune=now
    du -h .git
    

    更快更强的方法,推荐 bfg

    https://rtyley.github.io/bfg-repo-cleaner/

    下载bfg-1.13.0.jar
    
    cd /tmp
    git clone --mirror  git@ip地址:test/test.git
    java -jar /data/app/bfg-1.13.0.jar --strip-blobs-bigger-than 2M ds-wbp.git
    或者 java -jar /data/app/bfg-1.13.0.jar --delete-files *.war ds-wbp.git
    
    注意运行中的提示,可以看到类似如下日志,gitlab 会用到里面的文件进行清理
    In total, 100 object ids were changed. Full details are logged here:
    /private/tmp/test.git.bfg-report/2019-01-07/10-09-47
    
    cd test.git
    du -hs 
    git reflog expire --expire=now --all && git gc --prune=now --aggressive
    git push
    du -hs 
    
    进入 gitlab,设置,仓库,cleanup,上传上文提到的目录中的 object-id-map.old-new.txt 文件,清理
    
    最后,删除临时文件,rm -rf /tmp/test.git
  • 相关阅读:
    php公立转农历
    php判断客户端浏览器类型
    php为图片添加水印
    php类精确验证身份证号码
    webpack脚手架搭建(简单版)
    《深入浅出Node.js》第4章 异步编程
    js 实现继承相关
    设计模式:观察者模式
    bind 方法实现
    用 CSS 实现字符串截断
  • 原文地址:https://www.cnblogs.com/breakfei/p/14085840.html
Copyright © 2011-2022 走看看