基本慨念
- 工作区 (写代码)
- 暂存区 (临时存储)
- 本地库 (历史版本)
git add ... #从工作区添加到暂存区
git commit ... #从暂存区添加到本地库
- git和代码托管中心,
- 代码托管中心:维护远程库
- gitlab
- github
- 等等
git命令行操作
- 本地库操作
- 本地库初始化
git init;
ls -la;
ll .git;
-
基本操作
- 设置签名
- 形式:用户名: liangze, email地址:350442340@qq.com
- 作用:区分不同开发人员的身份
- 辨析:这里设置的签名和登录远程库(代码拖管中心)的账号,密码没有任何关系。
- 命令:
- 项目级别/仓库级别
仅在当前本地库范围内有效果 - 系统用户级别: 登陆当前操作系统的用户范围内有效果。比项目级别范围广
- 优先级:就近原则。
# 项目级别/仓库级别 ,保存到当前项目目录下 .git/config 文件 git config user.name laingze git config user.email 350442340@qq.com # 系统用户级别 全局 , 信息保存到 $HOME/.gitconfig git config --global user.name laingze git config --global user.email 350442340@qq.com # 查看 git config --list
- 项目级别/仓库级别
-
分支管理
-
远程库操作
基本操作
# 状态查看操作
git status; # 查看工作区,暂存区状态
# 添加操作 将工作区的 “新建/修改” 添加到暂存区
git add [filename] ;
git add . ; # 提交所有
git add -a;
git add --all;
# 提交操作 将暂存区的内容提交到本地库
git commit -m "info" [filename];
git commit -m "info"; # 提交所有
# 把暂存区的文件 untrack到工作区
git rm --cached filename;
查看历史记录
git log; #最完整的, 多屏显示控制方式 空格向上翻页 b向上翻页 q退出
git log --pertty=oneline;
git log --oneline;
git reflog; # 可以看到 HEAD指针位置 HEAD@{1}
# zsh git插件 缩写 路径: ~/.oh-my-zsh/plugins/git
glod; # git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset'
版本控制
git在管理版本时,他有一个HEAD指针,指针指向哪里就是用哪个版本
- 前进后退
# 基于索引值操作[推荐]
# git reflog; 后前面有个hash值 copy下来
git reset --hard [局部索引];
# 使用^符号 一次退一步,只能后退
git reset --hard HEAD^
# 使用~符号 n表示第几步
git reset --hard HEAD~3
# 紧接着强制推送到远程分支:
git push -f
HEAD是本地库的指针
soft(软,仅仅在本地库移动下HEAD指针)
mixed(混合, 在本地库移动下HEAD指针, 重置暂存区)
hard(硬,在本地库移动下HEAD指针, 重置暂存区和工作区)
删除文件找回? git reset --hard HEAD 和 git checkout .
git reset --hard HEAD
回退版本,放弃所有更改 包括暂存区
git checkout .
检出版本,也可用于放弃更改, 只能包括在工作区的
文件比较
# 不带文件名 比较多个文件
git diff [filename]; # 将工作区中的文件和暂存区进行比较
git diff [本地库中的历史版本的记录] [filename]; # 将工作区中的文件和本地仓库历史记录比较
分支操作
# 创建分支
git branch [branchname];
# 查看分支
git branch;
git branch -v;
# 切换分支
git checkout [branchname];
# 合并分支 先切换 再合并
git merge [有新内容的分支]
冲突
<<<<<< HEAD
冲突--子分支
冲突---master
master
git 基本原理
- 哈希,git底层采用的是SHA-1算法
- 哈希算法可以被用来验证文件。
- 拿原始文件进行SHA-1摘要 和 目标文件 的哈希值进行比对,从根本上保证数据的完整性
- git是基于快照的 每一个快照有一个 parent指向父节点
- 分支管理的本质是创建和移动指针
本地和远程库相关命令
https://github.com/LZRight123/gitHowUser.git
# 查看远程分支
git remote -v;
# 删别名
git remote rm origin;
# 给远程库起别名 下面的 origin = [远程分支别名]
git remote add origin https://github.com/LZRight123/gitHowUser.git
# 推送
# 第一次使用
git pull origin master --allow-unrelated-histories
git push -u origin master;
git push --set-upstream origin master;
# 以后可以用git push
git push origin master;
# 从 remote clone 拉分支 ;不需要进行 git init
git clone https://github.com/LZRight123/gitHowUser.git
# 抓取远程库代码
1. git fetch origin master; # 只把远程内容下载到本地,不会改本地工作区内容
2. git merge origin/master;
pull = fetch + merge;
git pull
使用SSH登陆 git仓库
$HOME/.ssh
文件夹下存放ssh相关资料
ssh-keygen -t rsa -C 350442340@qq.com;
cat id_rsa.pub;
# 复制到git -> setting -> SSH -> new ssh_key
# 为ssh方式添别名 就不用每次输密码了
git remote add origin_ssh git@github.com:LZRight123/gitHowUser.git
过大
在使用git更新或提交项目时候出现 "fatal: The remote end hung up unexpectedly " 原因是推送的文件太大。
那就简单了,要么是缓存不够,要么是网络不行,要么墙的原因
特别是资源库在国外的情况下。此问题可能由网络原因引起。
方法一:
修改提交缓存大小为500M,或者更大的数字
git config --global http.postBuffer 524288000
some comments below report having to double the value:
git config --global http.postBuffer 1048576000
或者在克隆/创建版本库生成的 .git目录下面修改生成的config文件增加如下:
[http]
postBuffer = 524288000
git plugin
The git plugin provides many aliases and a few useful functions.
To use it, add git
to the plugins array in your zshrc file:
plugins=(... git)
Aliases
Alias | Command |
---|---|
g | git |
gcl | git clone --recurse-submodules clone时 递归把子模块也clone下来 |
gcf | git config --list 查看git config的信息 |
ga | git add |
gaa | git add --all |
gapa | git add --patch |
gau | git add --update |
gav | git add --verbose |
gap | git apply |
gb | git branch |
gba | git branch -a |
gbd | git branch -d |
gbda | git branch --no-color --merged | command grep -vE "^(+ |
gbD | git branch -D |
gbl | git blame -b -w |
gbnm | git branch --no-merged |
gbr | git branch --remote |
gbs | git bisect |
gbsb | git bisect bad |
gbsg | git bisect good |
gbsr | git bisect reset |
gbss | git bisect start |
gc | git commit -v |
gc! | git commit -v --amend |
gcn! | git commit -v --no-edit --amend |
gca | git commit -v -a |
gca! | git commit -v -a --amend |
gcan! | git commit -v -a --no-edit --amend |
gcans! | git commit -v -a -s --no-edit --amend |
gcam | git commit -a -m |
gcsm | git commit -s -m |
gcb | git checkout -b |
gclean | git clean -id |
gpristine | git reset --hard && git clean -dfx |
gcm | git checkout master |
gcd | git checkout develop |
gcmsg | git commit -m |
gco | git checkout |
gcount | git shortlog -sn |
gcp | git cherry-pick |
gcpa | git cherry-pick --abort |
gcpc | git cherry-pick --continue |
gcs | git commit -S |
gd | git diff |
gdca | git diff --cached |
gdcw | git diff --cached --word-diff |
gdct | git describe --tags $(git rev-list --tags --max-count=1) |
gds | git diff --staged |
gdt | git diff-tree --no-commit-id --name-only -r |
gdv | git diff -w $@ | view - |
gdw | git diff --word-diff |
gf | git fetch |
gfa | git fetch --all --prune |
gfg | git ls-files | grep |
gfo | git fetch origin |
gg | git gui citool |
gga | git gui citool --amend |
ggf | git push --force origin $(current_branch) |
ggfl | git push --force-with-lease origin $(current_branch) |
ggl | git pull origin $(current_branch) |
ggp | git push origin $(current_branch) |
ggpnp | ggl && ggp |
ggpull | git pull origin "$(git_current_branch)" |
ggpur | ggu |
ggpush | git push origin "$(git_current_branch)" |
ggsup | git branch --set-upstream-to=origin/$(git_current_branch) |
ggu | git pull --rebase origin $(current_branch) |
gpsup | git push --set-upstream origin $(git_current_branch) |
ghh | git help |
gignore | git update-index --assume-unchanged |
gignored | git ls-files -v | grep "^[[:lower:]]" |
git-svn-dcommit-push | git svn dcommit && git push github master:svntrunk |
gk | gitk --all --branches |
gke | gitk --all $(git log -g --pretty=%h) |
gl | git pull |
glg | git log --stat |
glgp | git log --stat -p |
glgg | git log --graph |
glgga | git log --graph --decorate --all |
glgm | git log --graph --max-count=10 |
glo | git log --oneline --decorate |
glol | git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' |
glols | git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --stat |
glod | git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset' |
glods | git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset' --date=short |
glola | git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --all |
glog | git log --oneline --decorate --graph |
gloga | git log --oneline --decorate --graph --all |
glp | _git_log_prettily |
gm | git merge |
gmom | git merge origin/master |
gmt | git mergetool --no-prompt |
gmtvim | git mergetool --no-prompt --tool=vimdiff |
gmum | git merge upstream/master |
gma | git merge --abort |
gp | git push |
gpd | git push --dry-run |
gpf | git push --force-with-lease |
gpf! | git push --force |
gpoat | git push origin --all && git push origin --tags |
gpu | git push upstream |
gpv | git push -v |
gr | git remote |
gra | git remote add |
grb | git rebase |
grba | git rebase --abort |
grbc | git rebase --continue |
grbd | git rebase develop |
grbi | git rebase -i |
grbm | git rebase master |
grbs | git rebase --skip |
grev | git revert |
grh | git reset |
grhh | git reset --hard |
groh | git reset origin/$(git_current_branch) --hard |
grm | git rm |
grmc | git rm --cached |
grmv | git remote rename |
grrm | git remote remove |
grs | git restore |
grset | git remote set-url |
grss | git restore --source |
grt | cd "$(git rev-parse --show-toplevel || echo .)" |
gru | git reset -- |
grup | git remote update |
grv | git remote -v |
gsb | git status -sb |
gsd | git svn dcommit |
gsh | git show |
gsi | git submodule init |
gsps | git show --pretty=short --show-signature |
gsr | git svn rebase |
gss | git status -s |
gst | git status |
gsta | git stash push |
gsta | git stash save |
gstaa | git stash apply |
gstc | git stash clear |
gstd | git stash drop |
gstl | git stash list |
gstp | git stash pop |
gsts | git stash show --text |
gstall | git stash --all |
gsu | git submodule update |
gsw | git switch |
gswc | git switch -c |
gts | git tag -s |
gtv | git tag | sort -V |
gtl | gtl(){ git tag --sort=-v:refname -n -l ${1}* }; noglob gtl |
gunignore | git update-index --no-assume-unchanged |
gunwip | git log -n 1 | grep -q -c "--wip--" && git reset HEAD~1 |
gup | git pull --rebase |
gupv | git pull --rebase -v |
gupa | git pull --rebase --autostash |
gupav | git pull --rebase --autostash -v |
glum | git pull upstream master |
gwch | git whatchanged -p --abbrev-commit --pretty=medium |
gwip | git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit --no-verify --no-gpg-sign -m "--wip-- [skip ci]" |
Deprecated
Functions
Current
Command | Description |
---|---|
current_branch | Return the name of the current branch |
git_current_user_name | Returns the user.name config value |
git_current_user_email | Returns the user.email config value |
Work in Progress (WIP)
These features allow to pause a branch development and switch to another one ("Work in Progress", or wip). When you want to go back to work, just unwip it.
Command | Description |
---|---|
work_in_progress | Echoes a warning if the current branch is a wip |
gwip | Commit wip branch |
gunwip | Uncommit wip branch |
Deprecated
Command | Description | Reason |
---|---|---|
current_repository | Return the names of the current remotes | Didn't work properly. Use git remote -v instead (grv alias) |