git 命令

初始化版本库

# 在当前目录新建一个Git代码库
git init

# 新建一个目录,将其初始化为Git代码库
git init [project-name]

# 下载一个项目和它的整个代码历史
git clone [url]

配置全局变量

查看配置

# Git的设置文件为 .git/config 

# 可以在用户主目录下(全局配置)
# 也可以在项目目录下(项目配置)

# 显示当前的Git配置
git config --list

# 编辑Git配置文件
git config -e [--global]

全局变量

# 设置用户名
git config --global user.name 'asaawan'

# 设置邮箱
git config --global user.email 'asaawan@126.com'

# 设置当前分支为默认 push 分支
git config --global push.default "current"

设置忽略文件

设置每个人都要忽略的文件

设置每个人都要忽略的文件

	在根目录新建一个文件名为 .gitignore 的文件
	
		在命令行执行 echo *.jpg>.gitignore
		
		注意>左右不要有空格

设置只有自己要忽略的文件

设置只有自己要忽略的文件

	修改 .git/info/exclude 文件
	
		可使用正则表达式
			
		例如 *.[oa] 等价于*.o 和*.a

添加文件到暂存区

添加单个文件

# 添加单个文件
git add filename.txt

添加所有txt文件

# 添加所有txt文件
git add *.txt

添加指定文件

# 添加指定文件到暂存区
git add [file1] [file2] ...

# 添加指定目录到暂存区,包括子目录
git add [dir]

# 添加每个变化前,都会要求确认
# 对于同一个文件的多处变化,可以实现分次提交
git add -p

添加所有文件

#添加所有文件
git add .
	他会监控工作区的状态树,使用它会把工作时的所有变化提交到暂存区
	包括子目录、文件内容修改 (modified) 以及新文件 (new)
	但不包括被删除的文件和空目录
	
git add -u
	他仅监控已经被 add 的文件 (即tracked file) 
	他会将被修改的文件提交到暂存区
	add -u 不会提交新文件 (untracked file)
	(git add --update的缩写)

git add -A
	添加所有状态的文件
	(git add --all的缩写)

删除文件

删除工作区文件,并且将这次删除放入暂存区
git rm [file1] [file2] ...

# 停止追踪指定文件,但该文件会保留在工作区
git rm --cached [file]

# 改名文件,并且将这个改名放入暂存区
git mv [file-original] [file-renamed]

提交

# 提交所有文件的修改
git commit -m "对此次提交的描述"

# 提交单个文件的修改
git commit -m "对此次提交的描述" filename.txt

#暂存并提交
git commit -am "对此次提交的描述"

# 提交暂存区的指定文件到仓库区
git commit [file1] [file2] ... -m [message]

# 提交工作区自上次commit之后的变化,直接到仓库区
git commit -a

# 提交时显示所有diff信息
git commit -v

# 使用一次新的commit,替代上一次提交
# 如果代码没有任何新变化,则用来改写上一次commit的提交信息
git commit --amend -m [message]

# 重做上一次commit,并包括指定文件的新变化
git commit --amend [file1] [file2] ...

撤销修改

撤销尚未提交的修改

git checkout HEAD
# git checkout HEAD  撤销文件到已提交的任意版本 
# HEAD 参数也可以是版本号,默认回退到最近一次提交的版本

# 撤销所有文件
git checkout HEAD .
	
# 撤销少量文件
git checkout HEAD readme.txt todo.txt
	
# 撤销所有txt文件
git checkout HEAD *.txt
git checkout
# git checkout	无法撤销已暂存的文件

# 恢复暂存区的指定文件到工作区
git checkout [file]

# 撤销所有文件
git checkout
git checkout .

# 撤销少量文件
git checkout readme.txt todo.txt

# 撤销所有txt文件
git checkout *.txt
git reset
# 取消暂存
git reset head
git reset head <filename>

# 重置暂存区与工作区,与上一次 commit 保持一致
git reset --hard
git stash
# 暂时将未提交的变化移除,稍后再移入
$ git stash
$ git stash pop

撤销已经提交的修改

git reflog
# 每次版本移动的历史记录
git reflog
git checkout
# 恢复某个commit的指定文件到暂存区和工作区
git checkout [commit] [file]
git revert
# 反转提交
# 生成一个新的提交来撤销某次提交
git revert HEAD

# 新建一个commit,用来撤销指定commit
# 后者的所有变化都将被前者抵消,并且应用到当前分支
git revert [commit]
git reset
# 恢复到最近一次提交
git reset --hard HEAD^

# 重置当前分支的 HEAD 为指定 commit,同时重置暂存区和工作区,与指定 commit 一致
git reset --hard [commit]

# 恢复 head 之前的某个老版本(按照顺序)
git reset --hard 56a4d64a6sd

# 重置当前 HEAD 为指定 commit,但保持暂存区和工作区不变
git reset --keep [commit]

# 重置当前分支的指针为指定 commit,同时重置暂存区,但工作区不变
git reset [commit]

# 重置暂存区的指定文件,与上一次 commit 保持一致,但工作区不变
git reset [file]

分支管理

列出本地分支

# 列出本地分支
git branch

列出所有分支

# 列出所有分支
git branch -a

创建分支

# 基于当前分支的末梢创建新的分支,新建一个分支,但依然停留在当前分支
git branch <branchname>

创建分支指向指定commit

# 新建一个分支,指向指定commit
git branch [branch] [commit]

创建分支并建立追踪关系

# 新建一个分支,与指定的远程分支建立追踪关系
$ git branch --track [branch] [remote-branch]

创建并切换分支

# 创建并切换分支
git checkout -b <branchname>

切换分支

# 切换分支
git checkout <branchname>

切换到上一个分支

# 切换到上一个分支
git checkout -

每个分支最后的提交

# 每个分支最后的提交
git branch -v

本地与远程建立追踪关系

# 建立追踪关系,在现有分支与指定的远程分支之间
git branch --set-upstream [branch] [remote-branch]

合并分支

# 普通合并
# 合并并提交,合并指定分支到当前分支
git merge <branchname>

# 合并但不提交
git merge --no-commit <branchname>

# 选择一个commit,合并进当前分支
git cherry-pick [commit]
	
# 压合合并
# 压合合并后直接提交
git merge --squash <branchname>
		
# 压合合并后不提交
git merge --squash --no-commit <branchname>

重命名分支

# 不会覆盖已存在的同名分支
git branch -m <branchname> <newname>

# 会覆盖已存在的同名分支
git branch -M <branchname> <newname>	

删除分支

# 如果分支没有被合并会删除失败
git branch -d <branchname>

# 强制删除分支
git branch -D <branchname>		

查看信息

git status

# 当前状态,显示有变更的文件
git status

git log

# 查看提交历史记录,显示当前分支的版本历史
git log

# 显示 commit 历史,以及每次 commit 发生变更的文件
git log --stat

# 搜索提交历史,根据关键词
git log -S [keyword]

# 显示某个 commit 之后的所有变动,每个 commit 占据一行
git log [tag] HEAD --pretty=format:%s

# 显示某个commit之后的所有变动,其"提交说明"必须符合搜索条件
git log [tag] HEAD --grep feature

# 显示某个文件的版本历史,包括文件改名
git log --follow [file]
git whatchanged [file]
# 显示指定文件相关的每一次diff
git log -p [file]

# 以图形(树状图)模式显示
git log --graph

# 只输出版本号和版本提交描述
git log --oneline
git log --pretty=oneline
		
# 简化版本号
git log --abbrev-commit

# 显示过去5次提交
git log -5 --pretty --oneline
git log -5 --graph --pretty=oneline --abbrev-commit

git reflog

# 显示当前分支的最近几次提交
$ git reflog

gitk

# 查看当前分支的历史记录
gitk
			
# 查看某个分支的历史记录
gitk <branchname>

# 查看所有分支
gitk --all		

git diff

# 工作区与暂存区比较
git diff [filepath]

# 工作区与 HEAD (当前工作分支) 比较
git diff HEAD [filepath]

# 暂存区与 HEAD 比较
git diff --staged [filepath]
git diff --cached [filepath]

# 当前分支的文件与 branchName 分支的文件进行比较
git diff <branchName> [filepath]

# 与某一次提交进行比较
git diff <commitId> [filepath]

# 显示两次提交之间的差异
git diff [first-branch]...[second-branch]

# 显示今天你写了多少行代码
git diff --shortstat "@{0 day ago}"

git show

# 显示某次提交的元数据和内容变化
git show [commit]

# 显示某次提交发生变化的文件
git show --name-only [commit]

# 显示某次提交时,某个文件的内容
git show [commit]:[filename]

导出版本库

# 生成一个可供发布的压缩包
git archive --format=zip head >filename.zip
git archive --format=zip --prefix=1.1/head >txt.zip

业务统计

开发人员总数

# 开发人员总数
git log --pretty='%aN' | sort -u | wc -l

总提交数

# 总提交数
git log --oneline | wc -l

查看个人代码量

# 查看个人代码量
git log --author="asaawan" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -

查看指定时间个人代码量

git log --author=asaawan --since=2019-01-01 --until=2022-02-01 --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | grep "\(.html\|.java\|.xml\|.properties\)$" | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done

统计每个人的增删行数

# 统计每个人的增删行数
git log --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done

显示所有提交过的用户

# 显示所有提交过的用户,按提交次数排序
git shortlog -sn

显示指定文件修改记录

# 显示指定文件是什么人在什么时间修改过
git blame [file]

查看仓库提交者排名前五

# 查看仓库提交者排名前五
git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5

标签

# 列出所有tag
git tag

# 新建一个tag在当前commit
git tag [tag]

# 新建一个tag在指定commit
git tag [tag] [commit]

# 删除本地tag
git tag -d [tag]

# 删除远程tag
git push origin :refs/tags/[tagName]

# 查看tag信息
git show [tag]

# 提交指定tag
git push [remote] [tag]

# 提交所有tag
git push [remote] --tags

# 新建一个分支,指向某个tag
git checkout -b [branch] [tag]

远程初始化

# 克隆版本库
# 克隆后会自动添加 4 个 config
git clone <url>

# 添加远程版本库的别名,添加别名后会自动添加 2 个 config
git remote add <别名> <url>

# 删除远程库的别名和相关分支
git remote rm <别名>
	
# 通过本地远程库的别名修改远程库的 url
git remote set-url <别名> <newurl>

远程分支

# 列出远程分支
git branch -r

# 列出所有本地分支和远程分支
git branch -a

# 删除远程库中已经不存在的分支
git remote prune origin

# 删除远程分支
git push origin --delete [branch-name]
git branch -dr [remote/branch]

远程仓库

# 显示所有远程仓库
git remote -v

# 显示某个远程仓库的信息
git remote show [remote]

# 增加一个新的远程仓库,并命名
git remote add [shortname] [url]

远程拉取

# 获取远程仓库的所有变动到本地但不 merge
git fetch [remote]

# 获取远程仓库的变化到本地并 merge
git pull [remote] [branch]

# 获取远程到本地并 merge 不会产生一个无用的 commit
git pull --rebase

远程推送

# 推入远程库
git push [remote] [branch]
git push origin <分支>

# 强行推送当前分支到远程仓库,即使有冲突
git push [remote] --force

# 推送所有分支到远程仓库
git push [remote] --all
posted @ 2021-06-17 19:57  山奈  阅读(96)  评论(0)    收藏  举报