http://www.git-scm.com/download/http://www.git-scm.com/download/winhttp://www.git-scm.com/download/machttps://www.kernel.org/pub/software/scm/git/git-2.19.1.tar.gzhttps://mirrors.edge.kernel.org/pub/software/scm/git/git-2.19.1.tar.xzGit via GitIf 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拉取远程分支2git fetch origin develop:developgit checkout developgit branch -u origin/develop创建分支git checkout -b newbranchgit push -u origin newbranchgit branch -a查看分支关联git branch -vv合并newbranch到master分支git checkout mastergit branchgit merge newbranchgit push删除分支git branch -agit branch -d delbranchgit branch -r -d origin/delbranchgit push origin :delbarnch创建taggit tag newtaggit push origin newtaggit tag -l删除taggit tag -d deltaggit push origin :refs/tags/deltag修改远程仓库地址$ git remote -vorigin git@172.24.10.155:Bob/WorkflowDesigner2.git (fetch)origin git@172.24.10.155:Bob/WorkflowDesigner2.git (push)$ git remote set-url origin git@192.168.0.101:Bob/WorkflowDesigner2.git$ git remote -vorigin git@192.168.0.101:Bob/WorkflowDesigner2.git (fetch)origin git@192.168.0.101: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 showgit show HEAD~显示分支历史节点git show-branch按贡献值显示提交git shortloggit shortlog -s // 按名字统计git shortlog -s -e // 按邮箱统计显示冲突文件git diff --name-only --diff-filter=Ugit config --global alias.conflicts "diff --name-only --diff-filter=U"git conflicts获取提交ID信息$ git rev-parse HEAD356b6cf8ad7f2a3235b0bbeae5f4991816ac8087$ git rev-parse --short HEAD356b6cf$ git rev-parse --abbrev-ref HEADdevelop配置全局信息(详细:https://git-scm.com/docs/git-config)$ git config --global user.name "Bob"$ git config --global user.email "bob@email.com"$ git config --global push.default simple// git config --global push.default matching// 区别是,simple是仅推送当前分支,matching会推送全部分支$ cat ~/.gitconfig[user] name = Bob email = bob@email.com[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:proxyPassword@proxy.server.com:port比如:git config --global http.https://gitlab.com.proxy http://10.124.124.124:80SSH代理配置$ 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 "bob@email.com"$ 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 applicableif [ -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"]下的urlGit push/pull fatal: protocol error: bad line length character: This原因是服务器端没为git用户配置bash,解决方法是:# sudo usermod -s /bin/bash gitGPG配置备注:Windows安装:https://gpg4win.org/download.html其他系统安装在此文件后面,可以搜索GnuPG1. 查看已有的GPG keys$ gpg --list-secret-keys --keyid-format LONG2. 创建一个GPG key$ gpg --full-generate-key3. 导出GPG public key为文本格式$ gpg --list-secret-keys --keyid-format LONGsec rsa2048/F461FA2D923C3798 2018-09-18 [SC] BA4CF1650621335540E42DB5F461FA2D923C3798uid [ultimate] yourname (demo) ssb rsa2048/96E3D9B0C023B825 2018-09-18 [E]记住sec中算法后面的值,输入在下面(说明:也可以用邮箱地址替换这个KEY值)$ gpg --armor --export F461FA2D923C37984. 添加一个user id到GPG key$ gpg --edit-key F461FA2D923C3798> adduid> quitSave 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 mytag6. 使用gpg-agent帮助减少密码输入,可将下面命令写入~/.xsession、 ~/.profile或者.bash_profile等启动文件eval $(gpg-agent --daemon)7. 导出导入key导出gpg --export-secret-key F461FA2D923C3798 > ~/private.keygpg --export F461FA2D923C3798 > ~/public.key导入gpg --import ~/private.key8. 删除keygpg --delete-secret-key F461FA2D923C3798gpg --delete-key F461FA2D923C3798别名配置:# vim ~/.bash_profilealias 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=gitalias 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=ggualias 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_prettilyalias 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=historyalias 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=popdalias pu=pushdalias rd=rmdir$ source ~/.bash_profiletarball编译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 gitgit: /usr/bin/git /usr/local/bin/git /usr/share/man/man1/git.1.gz# /usr/local/bin/git --versiongit 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 versiongit 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 gitgit: /usr/bin/git /usr/local/bin/git /usr/share/man/man1/git.1.gz$ /usr/local/bin/git versiongit 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-lfsLinux$ 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/gitflowhttps://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 bashDebian$ apt-get install git-flowFedora$ 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 versionzsh插件: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-extrashttps://github.com/tj/git-extras$ brew install git-extras$ sudo yum install git-extras$ sudo apt-get install git-extraswindowsgit clone https://github.com/tj/git-extras.gitgit 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 yesterdaygit commits-since last week显示提交总数,加上参数--all显示每位作者的提交数量git countgit count --all移除最近提交git undogit 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 cu2. 安装curl, wget, bat...scoop install curl wget bat3. 其他scoop命令scoop help 显示帮助scoop list 显示已安装的命令scoop search 查找命令scoop info 信息scoop uninstall 卸载scoop update */ 更新全部,去掉*只更新scoop4. 配置git-bash别名回到git-bash用cat代替batecho "alias cat='bat'" >> ~/.bash_profile添加conflicts显示冲突文件git config --global alias.conflicts "diff --name-only --diff-filter=U"git conflicts5. 安装diff-so-fancymkdir -p ~/bincurl https://raw.githubusercontent.com/so-fancy/diff-so-fancy/master/third_party/build_fatpack/diff-so-fancy -L > ~/bin/diff-so-fancychmod +x ~/bin/diff-so-fancydiff-so-fancy配置gitgit 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/gpglrwxr-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.bz2wget https://www.gnupg.org/ftp/gcrypt/libgpg-error/libgpg-error-1.32.tar.bz2wget https://www.gnupg.org/ftp/gcrypt/libgcrypt/libgcrypt-1.8.3.tar.bz2wget https://www.gnupg.org/ftp/gcrypt/libksba/libksba-1.3.5.tar.bz2wget https://www.gnupg.org/ftp/gcrypt/libassuan/libassuan-2.5.1.tar.bz2wget https://www.gnupg.org/ftp/gcrypt/ntbtls/ntbtls-0.1.2.tar.bz2wget https://www.gnupg.org/ftp/gcrypt/npth/npth-1.6.tar.bz2wget 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.bz2tar xvjf libgpg-error-1.32.tar.bz2tar xvjf libgcrypt-1.8.3.tar.bz2tar xvjf libksba-1.3.5.tar.bz2tar xvjf libassuan-2.5.1.tar.bz2tar xvjf ntbtls-0.1.2.tar.bz2tar xvjf npth-1.6.tar.bz2tar 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 gpggpg: /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.20libgcrypt.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 --versiongpg (GnuPG) 2.2.10libgcrypt 1.8.3Copyright (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