Skip to content

Instantly share code, notes, and snippets.

@sh7ning
Last active November 2, 2021 03:02
Show Gist options
  • Save sh7ning/6e44d1d49b52e857da18f2520e6ea804 to your computer and use it in GitHub Desktop.
Save sh7ning/6e44d1d49b52e857da18f2520e6ea804 to your computer and use it in GitHub Desktop.
Git使用笔记

Git分支开发工作流

git 配置

  1. git配置:

    git config --global user.name <用户名>
    git config --global user.email <邮箱>
    git config --global init.defaultBranch master
    git config --global push.default simple
    git config --global credential.helper store #不用每次都输入密码
    git config --global core.editor /usr/bin/vim
    # 如果文件内容没变,但是git st看起来有修改,那么可能是换行符不同
    # 解决办法: `file $file` `dos2unix $file`  `find . -type f -exec dos2unix {} \;`
    git config --global core.filemode false
    git config --global core.ignorecase true
    git config --global alias.ci commit
    git config --global alias.st status
    git config --global merge.ff false
    
    # git config --global pull.ff only
    
    git config --global http.sslverify false # git clone 不用校验 https
    

    大小写不敏感,mac默认文件系统中敏感设置了也没什么卵用

    git config -l

  2. 生成ssh-key

    ssh-keygen -t rsa -C "test@test.com"

    后边全部回车就可以

  3. 在github个人设置,Add SSH Key

    ssh -T git@github.com
    

    这里如果出现 hi,你的名字 就说明配置成功

  4. 配置vim为git的diff工具

    • vim /usr/local/bin/git_diff_wrapper

      #!/bin/sh
      vimdiff "$2" "$5"
      
    • 增加权限

      chmod +x /usr/local/bin/git_diff_wrapper

    • vim ~/.gitconfig

      [diff]
        external = git_diff_wrapper
      [pager]
        diff =
      
    • 配置merge diff工具

      git config --global merge.tool vimdiff  
      git config --global mergetool.prompt false
      git config --global difftool.prompt false
      

      git mergetool

git使用

  1. 增加一个git仓库

    git init
    git remote add <主机名:origin> <网址:http://172.17.100.45/root/test.git>
    git push -u origin master
    
  2. 移除一个git仓库 url

git remote remove origin
  1. 查看分支

    • 查看所有远程分支

      git branch -r
      git branch -vv #查看追踪对应关系
      git branch --set-upstream 8090 origin/8090 #建立分支追踪
      
    • 查看所有分支 git branch -a

  2. 新建分支

    git branch 新分支 来源分支(默认当前分支)
    
  3. 切换分支

    git checkout 分支
    
  4. 前两步等同于,新建并切换分支

    git checkout -b 分支
    git checkout -b 本地分支名 远程分支名
    
  5. 删除

    git rm file_name  #从版本控制中删除,并删除磁盘上的文件
    git rm --cached file_name #不删除磁盘上的
    
  6. 比较

    git diff #比较还没commit的文件
    git diff --cached #比较commit后还没push的文件
    git diff origin/master HEAD 跟远程比较
    
  7. 删除分支

    • 删除远程分支 git push origin :分支

    • 删除本地分支 git branch -d 分支

      -D 强制删除

  8. 合并分支,将分支合并到当前分支

    git merge --no-ff -m "merge with no-ff" 分支
    git merge 分支
    
  9. 为当前分支打tag

```
//打tag
git tag -a 标签名 -m '注释'
git push --tags
//列出本地所有的tag
git tag
//列出远程所有的tag
git show-ref --tag
//删除本地tag
git tag -d 标签名
//删除远程tag
git push origin --delete tag 标签名
git push origin :refs/tags/标签名(Git v1.7.0以前)
	
//批量删除远程所有的标签
git show-ref --tag | awk '/(.*)(s+)(.*)$/ {print ":" $2}' | xargs git push origin
//批量删除本地所有 v1.1.0.x 的标签
git tag | xargs git tag -d
//批量删除和远程所有的标签
git tag -d $(git tag -l) && git fetch && git push origin --delete $(git tag -l) && git tag -d $(git tag -l)

// 获取最新的版本号(如v0.0.1) tag
git describe --tags --match "v[0-9]*"
```
  1. 克隆仓库
```
git clone https://github.com/你的项目仓库.git [本地目录]
git add file文件名
git commit 文件 -m '注释'
git commit -am '注释'
```
  1. 拉取分支
`git pull <远程主机名> <远程分支名>:<本地分支名>`
> 如果是本地当前分支合并的话则 冒号后面的部分可以省略
  1. 推送分支
`git push <远程主机名> <本地分支名>:<远程分支名>`

> -u 如果当前分支与多个主机存在追踪关系,则可以使用-u选项指定一个默认主机

`git push -u origin master`
  1. 配置 gogs

Dockerfile.gogs

FROM gogs/gogs

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/' /etc/apk/repositories \
            && echo 'http://mirrors.ustc.edu.cn/alpine/edge/testing' >> /etc/apk/repositories \
                # 这里还要装更多的扩展,才能方便在composer安装的时候依赖都存在。
                && apk add --no-cache openssl php7 php7-json php7-phar php7-iconv php7-mbstring php7-openssl \
                && ln -sf /usr/bin/php7 /usr/bin/php \
                && curl -sS https://getcomposer.org/installer | php \
                && mv composer.phar /usr/local/bin/composer \
                && apk add --no-cache sudo \
                && echo 'git ALL=(root) NOPASSWD: /usr/local/bin/composer, /usr/bin/git' >> /etc/sudoers

post-receive hook

#!/bin/bash

wechat_reponame="wechat"
development_revision="master"

dirname=$(basename $PWD)
replace=
reponame=${dirname/.git/$replace}

while read oldrev newrev refname
do
    # delete branch, escape
    if [ "$newrev" == "0000000000000000000000000000000000000000" ]; then
        continue
    fi
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    
    if [ "$wechat_reponame" == "$reponame" -a "$development_revision" == "$branch" ]; then
        echo $(date '+%Y-%m-%d %H:%M:%S') 项目: $wechat_reponame, 分支: $branch 开始部署 >> /tmp/git-post.log
        # cd /www/yadou.net/wechat/
        wechat_dir=/www/yadou.net/wechat
        sudo git --work-tree=$wechat_dir --git-dir=/data/git/gogs-repositories/sh7ning/wechat.git checkout -f
        sudo composer --working-dir=$wechat_dir/ctx_base install --no-dev >/dev/null 2>&1
        sudo composer --working-dir=$wechat_dir/ctx_base dump-autoload -o >/dev/null 2>&1
    fi
done
  1. 还原代码和文件修改
git checkout -- 文件名(.表示当前和子目录目录所有)

如果文件是删除要恢复

git checkout origin/分支名 -- 文件名(.表示当前和子目录目录所有)
@sh7ning
Copy link
Author

sh7ning commented Mar 27, 2019

Step 1: From your project repository, check out a new branch and test the changes.

git checkout -b xx-master master
git pull https://github.com/xx/yy.git master

Step 2: Merge the changes and update on GitHub.

git checkout master
git merge --no-ff xx-master
git push origin master

@sh7ning
Copy link
Author

sh7ning commented Jun 28, 2020

git 自动补全

wget -O ~/.git-completion.bash https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash

vi .bashrc

if [ -f ~/.git-completion.bash ]; then
    source ~/.git-completion.bash
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment