http://www.git-scm.com/download/ http://www.git-scm.com/download/win http://www.git-scm.com/download/mac https://www.kernel.org/pub/software/scm/git/git-2.19.1.tar.gz https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.19.1.tar.xz Git via Git If you already have Git installed, you can get the latest development version via Git itself: git clone https://github.com/git/git 克隆远程仓库 git clone [--recursive] 远程仓库地址 [本地路径] 克隆远程单分支 git clone --single-branch -b develop 远程仓库地址 拉取远程分支 git checkout -b develop origin/develop 拉取远程分支2 git fetch origin develop:develop git checkout develop git branch -u origin/develop 创建分支 git checkout -b newbranch git push -u origin newbranch git branch -a 查看分支关联 git branch -vv 合并newbranch到master分支 git checkout master git branch git merge newbranch git push 删除分支 git branch -a git branch -d delbranch git branch -r -d origin/delbranch git push origin :delbarnch 创建tag git tag newtag git push origin newtag git tag -l 删除tag git tag -d deltag git push origin :refs/tags/deltag 修改远程仓库地址 $ git remote -v origin [email protected]:Bob/WorkflowDesigner2.git (fetch) origin [email protected]:Bob/WorkflowDesigner2.git (push) $ git remote set-url origin [email protected]:Bob/WorkflowDesigner2.git $ git remote -v origin [email protected]:Bob/WorkflowDesigner2.git (fetch) origin [email protected]:Bob/WorkflowDesigner2.git (push) 保存临时修改 $ git stash 取出临时修改 $ git stash pop 显示所有stash $ git stash list 清空stash $ git stash clear 修复最近一次commit $ git commit --amend $ git commit --amend -m "Fixes bug #42" 回退 $ git reset --hard$ git clean -df 移除未被跟踪的文件,-xf会忽略.gitignore进行移除文件 $ git push origin HEAD --force 参数说明 --mixed 缺省,只保留源码,回退commit和index信息。 --soft 只回退commit信息 --hard 彻底回退到某个版本 HEAD 最近一次提交 HEAD^ 上一次提交。 忽略已经被提交的文件 $ git rm --cached -r logs/ $ vim .gitignore 将 logs/ 加入忽略文件列表 $ git commit -m "We really don't want Git to track this anymore!" 显示某个变更 git show git show HEAD~ 显示分支历史节点 git show-branch 按贡献值显示提交 git shortlog git shortlog -s // 按名字统计 git shortlog -s -e // 按邮箱统计 显示冲突文件 git diff --name-only --diff-filter=U git config --global alias.conflicts "diff --name-only --diff-filter=U" git conflicts 获取提交ID信息 $ git rev-parse HEAD 356b6cf8ad7f2a3235b0bbeae5f4991816ac8087 $ git rev-parse --short HEAD 356b6cf $ git rev-parse --abbrev-ref HEAD develop 配置全局信息(详细:https://git-scm.com/docs/git-config) $ git config --global user.name "Bob" $ git config --global user.email "[email protected]" $ git config --global push.default simple // git config --global push.default matching // 区别是,simple是仅推送当前分支,matching会推送全部分支 $ cat ~/.gitconfig [user] name = Bob email = [email protected] [push] default = simple $ git config --global core.excludesfile "$HOME/.gitignore_global" $ cat ~/.gitignore_global *~ .DS_Store .idea # some comment. 设置git默认编辑器: $ git config --global core.pager 'less' # 类似vim(建议) $ git config --global core.pager 'more' # 输出到shell并分页 $ git config --global core.pager 'vim -' # vim(不支持着色标记) $ git config --global core.pager 'nano -' # nano(不支持着色标记) 注:Windows的Git Console的less跟linux的more效果相同,而且没有more命令。 HTTP代理配置 格式:git config --global http.https://domain.com.proxy http://proxyUsername:[email protected]:port 比如:git config --global http.https://gitlab.com.proxy http://10.124.124.124:80 SSH代理配置 $ vim ~/.ssh/config # 添加内容: Host *.gitlab.com ProxyCommand connect -H 10.124.124.124:80 %h %p ServerAliveInterval 30 配置SSH $ ssh-keygen -t rsa -b 4096 -C "[email protected]" $ eval "$(ssh-agent -s)" Agent pid 28728 $ ssh-add -k ~/.ssh/id_rsa // ssh-add -K ~/.ssh/id_rsa $ clip < ~/.ssh/id_rsa.pub // pbcopy < ~/.ssh/id_rsa.pub 如果粘贴github上失败,去掉最后的一个换行。 自动启动ssh-agent和ssh-add # vim ~/.bash_profile // vim ~/.zshrc ------------------------ SSH_ENV="$HOME/.ssh/environment" function start_agent { echo "Initialising new SSH agent..." /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}" echo succeeded chmod 600 "${SSH_ENV}" . "${SSH_ENV}" > /dev/null # /usr/bin/ssh-add -k ~/.ssh/your_id_rsa /usr/bin/ssh-add; } # Source SSH settings, if applicable if [ -f "${SSH_ENV}" ]; then . "${SSH_ENV}" > /dev/null #ps ${SSH_AGENT_PID} doesn't work under cywgin ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || { start_agent; } else start_agent; fi ------------------------ 更改git连接方式https为ssh 复制好ssh地址,然后打开.git/config文件修改[remote "origin"]下的url Git push/pull fatal: protocol error: bad line length character: This 原因是服务器端没为git用户配置bash,解决方法是: # sudo usermod -s /bin/bash git GPG配置 备注:Windows安装:https://gpg4win.org/download.html 其他系统安装在此文件后面,可以搜索GnuPG 1. 查看已有的GPG keys $ gpg --list-secret-keys --keyid-format LONG 2. 创建一个GPG key $ gpg --full-generate-key 3. 导出GPG public key为文本格式 $ gpg --list-secret-keys --keyid-format LONG sec rsa2048/F461FA2D923C3798 2018-09-18 [SC] BA4CF1650621335540E42DB5F461FA2D923C3798 uid [ultimate] yourname (demo) ssb rsa2048/96E3D9B0C023B825 2018-09-18 [E] 记住sec中算法后面的值,输入在下面 (说明:也可以用邮箱地址替换这个KEY值) $ gpg --armor --export F461FA2D923C3798 4. 添加一个user id到GPG key $ gpg --edit-key F461FA2D923C3798 > adduid > quit Save changes? (y/N) y 可以使用help了解更多命令。 5. 给Git配置提交和tag签名 $ git config --global user.signingkey F461FA2D923C3798 $ git config --global commit.gpgsign true 提交会自动签名。 tag签名需要加入-s,如: $ git tag -s mytag 验证 $ git tag -v mytag 6. 使用gpg-agent帮助减少密码输入,可将下面命令写入~/.xsession、 ~/.profile或者.bash_profile等启动文件 eval $(gpg-agent --daemon) 7. 导出导入key 导出 gpg --export-secret-key F461FA2D923C3798 > ~/private.key gpg --export F461FA2D923C3798 > ~/public.key 导入 gpg --import ~/private.key 8. 删除key gpg --delete-secret-key F461FA2D923C3798 gpg --delete-key F461FA2D923C3798 别名配置: # vim ~/.bash_profile alias open=\"$WINDIR/explorer.exe\" alias -- -='cd -' alias ~='cd ~' alias ..='cd ..' alias ...='cd ../..' alias ....='cd ../../..' alias .....='cd ../../../..' alias ......='cd ../../../../..' alias d='dirs -v | head -10' alias g=git alias ga='git add' alias gaa='git add --all' alias gap='git apply' alias gapa='git add --patch' alias gau='git add --update' alias gb='git branch' alias gba='git branch -a' alias gbd='git branch -d' alias gbda='git branch --no-color --merged | command grep -vE "^(\*|\s*(master|develop|dev)\s*$)" | command xargs -n 1 git branch -d' alias gbl='git blame -b -w' alias gbnm='git branch --no-merged' alias gbr='git branch --remote' alias gbs='git bisect' alias gbsb='git bisect bad' alias gbsg='git bisect good' alias gbsr='git bisect reset' alias gbss='git bisect start' alias gc='git commit -v' alias 'gc!'='git commit -v --amend' alias gca='git commit -v -a' alias 'gca!'='git commit -v -a --amend' alias gcam='git commit -a -m' alias 'gcan!'='git commit -v -a --no-edit --amend' alias 'gcans!'='git commit -v -a -s --no-edit --amend' alias gcb='git checkout -b' alias gcd='git checkout develop' alias gcf='git config --list' alias gcl='git clone --recursive' alias gclean='git clean -fd' alias gcm='git checkout master' alias gcmsg='git commit -m' alias 'gcn!'='git commit -v --no-edit --amend' alias gco='git checkout' alias gcount='git shortlog -sn' alias gcp='git cherry-pick' alias gcpa='git cherry-pick --abort' alias gcpc='git cherry-pick --continue' alias gcs='git commit -S' alias gcsm='git commit -s -m' alias gd='git diff' alias gdca='git diff --cached' alias gdct='git describe --tags `git rev-list --tags --max-count=1`' alias gdcw='git diff --cached --word-diff' alias gdt='git diff-tree --no-commit-id --name-only -r' alias gdw='git diff --word-diff' alias gf='git fetch' alias gfa='git fetch --all --prune' alias gfo='git fetch origin' alias gg='git gui citool' alias gga='git gui citool --amend' alias ggpur=ggu alias ghh='git help' alias gignore='git update-index --assume-unchanged' alias gignored='git ls-files -v | grep "^[[:lower:]]"' alias git-svn-dcommit-push='git svn dcommit && git push github master:svntrunk' alias gk='\gitk --all --branches' alias gke='\gitk --all $(git log -g --pretty=%h)' alias gl='git pull' alias glg='git log --stat' alias glgg='git log --graph' alias glgga='git log --graph --decorate --all' alias glgm='git log --graph --max-count=10' alias glgp='git log --stat -p' alias glo='git log --oneline --decorate' alias glog='git log --oneline --decorate --graph' alias gloga='git log --oneline --decorate --graph --all' alias glol='git log --graph --pretty='\''%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'\'' --abbrev-commit' alias glola='git log --graph --pretty='\''%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'\'' --abbrev-commit --all' alias glp=_git_log_prettily alias glum='git pull upstream master' alias gm='git merge' alias gma='git merge --abort' alias gmom='git merge origin/master' alias gmt='git mergetool --no-prompt' alias gmtvim='git mergetool --no-prompt --tool=vimdiff' alias gmum='git merge upstream/master' alias gp='git push' alias gpd='git push --dry-run' alias gpoat='git push origin --all && git push origin --tags' alias gpristine='git reset --hard && git clean -dfx' alias gpu='git push upstream' alias gpv='git push -v' alias gr='git remote' alias gra='git remote add' alias grb='git rebase' alias grba='git rebase --abort' alias grbc='git rebase --continue' alias grbi='git rebase -i' alias grbm='git rebase master' alias grbs='git rebase --skip' alias grep='grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn}' alias grh='git reset HEAD' alias grhh='git reset HEAD --hard' alias grmv='git remote rename' alias grrm='git remote remove' alias grset='git remote set-url' alias grt='cd $(git rev-parse --show-toplevel || echo ".")' alias gru='git reset --' alias grup='git remote update' alias grv='git remote -v' alias gsb='git status -sb' alias gsd='git svn dcommit' alias gsi='git submodule init' alias gsps='git show --pretty=short --show-signature' alias gsr='git svn rebase' alias gss='git status -s' alias gst='git status' alias gsta='git stash save' alias gstaa='git stash apply' alias gstc='git stash clear' alias gstd='git stash drop' alias gstl='git stash list' alias gstp='git stash pop' alias gsts='git stash show --text' alias gsu='git submodule update' alias gts='git tag -s' alias gtv='git tag | sort -V' alias gunignore='git update-index --no-assume-unchanged' alias gunwip='git log -n 1 | grep -q -c "\-\-wip\-\-" && git reset HEAD~1' alias gup='git pull --rebase' alias gupv='git pull --rebase -v' alias gwch='git whatchanged -p --abbrev-commit --pretty=medium' alias gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit --no-verify -m "--wip-- [skip ci]"' alias h=history alias history='fc -l' alias l='ls -lah' alias la='ls -lAh' alias ll='ls -lh' alias ls='ls --color=tty' alias lsa='ls -lah' alias md='mkdir -p' alias po=popd alias pu=pushd alias rd=rmdir $ source ~/.bash_profile tarball编译 https://www.kernel.org/pub/software/scm/git/ **********centos命令如下: $ sudo su # yum install gcc gcc-c++ perl-ExtUtils-MakeMaker -y # yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel -y 下载git源码(curl和wget选一个即可) # wget -P /usr/src/git-2.19.1 https://www.kernel.org/pub/software/scm/git/git-2.19.1.tar.xz // # wget -P /usr/src/git-2.19.1 https://www.kernel.org/pub/software/scm/git/git-2.19.1.tar.gz // # curl --create-dirs -o /usr/src/git-2.19.1/git-2.19.1.tar.gz https://www.kernel.org/pub/software/scm/git/git-2.19.1.tar.gz # chmod -R 755 /usr/src/git-2.19.1/ 解压安装 # cd /usr/src/git-2.19.1/ # tar Jxf git-2.19.1.tar.xz // # tar zxf git-2.19.1.tar.gz # cd git-2.19.1 # ./configure --prefix=/usr/local/ # make install 查看结果,删除yum的git和git-2.19.1.tar.gz # whereis git git: /usr/bin/git /usr/local/bin/git /usr/share/man/man1/git.1.gz # /usr/local/bin/git --version git version 2.19.1 # rm -f /usr/src/git-2.19.1/git-2.19.1.tar.gz # yum remove git # yum autoremove -y # exit $ git version git version 2.19.1 **********debian命令如下: $ sudo apt update $ sudo apt install -y build-essential $ sudo apt install -y libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev $ sudo curl --create-dirs -o /usr/src/git-2.19.1/git-2.19.1.tar.gz https://www.kernel.org/pub/software/scm/git/git-2.19.1.tar.gz (或) $ sudo wget -P /usr/src/git-2.19.1 https://www.kernel.org/pub/software/scm/git/git-2.19.1.tar.gz $ cd /usr/src/git-2.19.1/ $ sudo tar Jxf git-2.19.1.tar.xz // $ sudo tar zxf git-2.19.1.tar.gz $ cd git-2.19.1 $ sudo ./configure --prefix=/usr/local/ $ sudo make install $ whereis git git: /usr/bin/git /usr/local/bin/git /usr/share/man/man1/git.1.gz $ /usr/local/bin/git version git version 2.15.1 $ sudo rm -f /usr/src/git-2.19.1/git-2.19.1.tar.gz $ sudo apt remove git $ sudo apt autoremove -------------------------- git lfs -------------------------- MacOS $ brew install git-lfs Linux $ mkdir git-lfs $ cd git-lfs $ wget https://github.com/git-lfs/git-lfs/releases/download/v2.5.1/git-lfs-linux-amd64-v2.5.1.tar.gz $ tar xvzf git-lfs-linux-amd64-v2.5.1.tar.gz $ sudo ./install.sh $ git lfs version $ cd .. $ rm -rf git-lfs -------------------------- git flow -------------------------- https://github.com/nvie/gitflow https://github.com/nvie/gitflow/wiki/Linux 安装方式一: MacOS $ brew install git-flow 或者 $ wget --no-check-certificate -q -O - https://github.com/nvie/gitflow/raw/develop/contrib/gitflow-installer.sh | sudo bash Debian $ apt-get install git-flow Fedora $ yum install gitflow 安装方式二:手动安装 手动安装方法1(推荐) $ git clone --recursive git://github.com/nvie/gitflow.git $ cd gitflow $ sudo make install 如果安装到其他目录,可以 $ sudo make prefix=/opt/local install $ git flow version $ cd .. $ rm -rf gitflow 手动安装方法2 $ wget -q -O - https://github.com/nvie/gitflow/raw/develop/contrib/gitflow-installer.sh | sh 删除 $ sudo ./gitflow-installer.sh uninstall 版本:git flow version zsh插件:git-flow、bobthecow/git-flow-completion 如果提示zsh compinit: insecure directories, run compaudit for list. 可以用下面命令更改权限 compaudit | xargs chmod -R 755 使用:分支feature/demo1,修改后合并到develop分支 $ git clone ... $ cd ... $ git flow init -d $ git flow feature start demo1 // 修改代码 $ git flow feature finish demo1 分支模式学习文档: http://nvie.com/posts/a-successful-git-branching-model/ git-extras https://github.com/tj/git-extras $ brew install git-extras $ sudo yum install git-extras $ sudo apt-get install git-extras windows git clone https://github.com/tj/git-extras.git git checkout $(git describe --tags $(git rev-list --tags --max-count=1)) install.cmd 使用方法 https://www.oschina.net/p/git-extras 查看某个贡献者 git contrib m2nlight 查看repo的概况 git summary 显示每个文件的提交数量和活跃时间 git effort --above 15 {src,lib}/* 从某个日期开始的提交 git commits-since yesterday git commits-since last week 显示提交总数,加上参数--all显示每位作者的提交数量 git count git count --all 移除最近提交 git undo git undo 3 等等 **Windows** 丰富git-bash工具(需要在PowerShell 3+.NET Framework 4.5+下安装scoop) 1. 打开PowerShell,安装scoop(https://scoop.sh/) iex (new-object net.webclient).downloadstring('https://get.scoop.sh') set-executionpolicy unrestricted -s cu 2. 安装curl, wget, bat... scoop install curl wget bat 3. 其他scoop命令 scoop help 显示帮助 scoop list 显示已安装的命令 scoop search 查找命令 scoop info 信息 scoop uninstall 卸载 scoop update */ 更新全部,去掉*只更新scoop 4. 配置git-bash别名 回到git-bash 用cat代替bat echo "alias cat='bat'" >> ~/.bash_profile 添加conflicts显示冲突文件 git config --global alias.conflicts "diff --name-only --diff-filter=U" git conflicts 5. 安装diff-so-fancy mkdir -p ~/bin curl https://raw.githubusercontent.com/so-fancy/diff-so-fancy/master/third_party/build_fatpack/diff-so-fancy -L > ~/bin/diff-so-fancy chmod +x ~/bin/diff-so-fancy diff-so-fancy 配置git git config --global core.pager "diff-so-fancy | less --tabs=1,5 -RFX" ---- Install GnuPG ---- https://www.gnupg.org/download/ mac 方式一:推荐 mac $ brew install gpg pinentry pinentry-mac $ echo "pinentry-program /usr/local/bin/pinentry-mac" >> ~/.gnupg/gpg-agent.conf $ killall gpg-agent 如果使用期间发生错误,参考后面的 Inappropriate ioctl for device 配置环境变量。 SourceTree支持: $ ls -la /usr/local/bin/gpg lrwxr-xr-x /usr/local/bin/gpg -> ../Cellar/gnupg/2.2.10/bin/gpg $ ln -s /usr/local/bin/gpg /usr/local/bin/gpg2 在SourceTree的配置界面,点“高级”,更改“GPG程序”路径为 /usr/local/bin。 方式二: https://gpgtools.org/ 下载dmg安装 linux (tarball) $ mkdir ~/gnupg $ cd ~/gnupg $ wget https://www.gnupg.org/ftp/gcrypt/gnupg/gnupg-2.2.10.tar.bz2 wget https://www.gnupg.org/ftp/gcrypt/libgpg-error/libgpg-error-1.32.tar.bz2 wget https://www.gnupg.org/ftp/gcrypt/libgcrypt/libgcrypt-1.8.3.tar.bz2 wget https://www.gnupg.org/ftp/gcrypt/libksba/libksba-1.3.5.tar.bz2 wget https://www.gnupg.org/ftp/gcrypt/libassuan/libassuan-2.5.1.tar.bz2 wget https://www.gnupg.org/ftp/gcrypt/ntbtls/ntbtls-0.1.2.tar.bz2 wget https://www.gnupg.org/ftp/gcrypt/npth/npth-1.6.tar.bz2 wget https://www.gnupg.org/ftp/gcrypt/pinentry/pinentry-1.1.0.tar.bz2 // wget https://www.gnupg.org/ftp/gcrypt/gpgme/gpgme-1.11.1.tar.bz2 // wget https://www.gnupg.org/ftp/gcrypt/gpa/gpa-0.9.10.tar.bz2 $ tar xvjf gnupg-2.2.10.tar.bz2 tar xvjf libgpg-error-1.32.tar.bz2 tar xvjf libgcrypt-1.8.3.tar.bz2 tar xvjf libksba-1.3.5.tar.bz2 tar xvjf libassuan-2.5.1.tar.bz2 tar xvjf ntbtls-0.1.2.tar.bz2 tar xvjf npth-1.6.tar.bz2 tar xvjf pinentry-1.1.0.tar.bz2 // tar xvjf gpgme-1.11.1.tar.bz2 // tar xvjf gpa-0.9.10.tar.bz2 // 编译安装GnuPG组件 $ cd libgpg-error-1.32 $ ./configure $ sudo make && sudo make install $ cd ../libgcrypt-1.8.3 $ ./configure $ sudo make && sudo make install $ cd ../libksba-1.3.5 $ ./configure $ sudo make && sudo make install $ cd ../libassuan-2.5.1 $ ./configure $ sudo make && sudo make install $ cd ../ntbtls-0.1.2 $ ./configure $ sudo make && sudo make install $ cd ../npth-1.6 $ ./configure $ sudo make && sudo make install // 编译安装GnuPG $ cd ../gnupg-2.2.10 $ ./configure $ sudo make -j8 && sudo make install $ whereis gpg gpg: /usr/bin/gpg /usr/local/bin/gpg /usr/share/man/man1/gpg.1.gz $ /usr/local/bin/gpg --version /usr/local/bin/gpg: error while loading shared libraries: libgcrypt.so.20: cannot open shared object file: No such file or directory 正常会显示版本信息,如果出现这个错误,先获得libgcrypt.so.20文件的位置,然后加入变量LD_LIBRARY_PATH中: $ whereis libgcrypt.so.20 libgcrypt.so: /usr/lib/libgcrypt.so.11 /usr/lib64/libgcrypt.so.11 /usr/local/lib/libgcrypt.so.20 /usr/local/lib/libgcrypt.so $ export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH $ /usr/local/bin/gpg --version gpg (GnuPG) 2.2.10 libgcrypt 1.8.3 Copyright (C) 2018 Free Software Foundation, Inc. ... 将环境变量添加到启动配置,如.bashrc中 $ echo export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH >> ~/.bashrc 同理检查PATH环境变量,使/usr/local/bin在/usr/bin前,然gpg命令直接运行我们编译的版本。 重新进入会话,输入 gpg --version 应该是刚才安装的版本。 必要组件安装: --- // 用于输入密码保护(必须) $ cd ../pinentry-1.1.0 $ ./configure $ sudo make && sudo make install --- 下面两个可选,可能需要修改库的路径,configure才能正确通过。 // GPGME is the standard library to access GnuPG functions from programming languages. $ cd ../gpgme-1.11.1 $ ./configure $ sudo make && sudo make install // GPA is a graphical frontend to GnuPG. $ cd ../gpa-0.9.10 $ ./configure $ sudo make && sudo make install --- 最后可删除无用的源文件 $ cd ~ $ sudo rm -rf gnupg