Git_命令行

几个概念



(图片来自http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html)

  • Workspace:工作区
  • Index / Stage:暂存区
  • Repository:本地仓库
  • Remote:远程仓库

配置用户信息


用户的信息保存在.gitconfig文件中。全局的.gitconfig文件位于用户目录下。

// 查看配置信息(global + local)
git config --list
// 查看配置信息(global)
git config --global --list
// 查看配置信息(local)
git config --local --list

// 修改配置信息
git config --global --edit

// 配置用户名
git config --global user.name “yourname”

// 配置用户邮箱
git config --global user.email “youremail@gmail.com”

// 配置默认文本编辑器
git config --global core.editor sublime

// 配置默认merge工具
git config --global merge.tool p4merge

// 设置推送默认方式(simple或者matching)
// ・matching:Git 1.x 的默认行为,如果执行 git push 但没有指定分支,将 push 所有本地的分支到远程仓库中。
// ・simple:Git 2.x 的默认行为,如果执行 git push 但没有指定分支,将 push 当前本地的分支到远程仓库中。
git config --global push.default simple/matching

如果用了 --global 选项,那么更改的配置文件就是位于你用户主目录下的那个,以后你 所有的项目都会默认使用这里配置的用户信息。如果要在某个特定的项目中使用其他名字或 者电邮,只要去掉 --global 选项重新配置即可,新的设定保存在当前项目的 .git/config 文件里。

常用命令


// 创建本地Git仓库(将已有项目加入 git 管理)
git init
// 创建本地Git仓库(新建项目并加入 git 管理)
git init <project_name>
// 将本地Git仓库上传到远程仓库(首先远程仓库要存在)
git remote add origin git@github.com:username/reponame.git
git push origin master
// 删掉本地 Git 仓库关联的远程仓库
git remote rm origin

// 克隆仓库
git clone https://github.com/test/test.git
// 仓库太大,克隆不下来,先浅克隆(浅克隆相关内容参考:浅克隆
git clone --depth=1 https://github.com/test/test.git

// 查看当前状态
git status

// 把文件添加到暂存区域
git stage <文件名>
git add <文件名>
// 把所有文件添加到暂存区域
git stage .
git add .

// 提交文件
git commit <文件名>
// 提交所有文件
git commit . / git commit
// 提交所有文件并附带注释
git commit -m “comment”
// 跳过 git add 直接 commit
git commit -a -m “comment”

// 推送到远程仓库
git push
// 把当前分支push到远程仓库,并制定分支在远程仓库的名称
git push origin <分支名>

// 抓取远程仓库数据到本地,抓取仅仅是看远程仓库有哪些更新
// 如果想要这些更新反应到本地工作目录,必须要执行pull操作
git fetch origin
git fetch --all
git fetch
// 在抓取的同时,把已经不存在的branch给删除掉
git fetch --prune
git fetch -p

// 把远程仓库最新内容更新到本地工作目录
git pull

// 删除文件
rm <文件名>
// 删除文件并添加到暂存区域
git rm <文件名>
// 删除文件(仅仅是从Git仓库删除,也就是从暂存区域删除,本地工作目录不删除)
git rm --cached <文件名>

// 重命名文件
git mv <原文件名> <新文件名>

// 查看工作目录中当前文件和暂存区域快照之间的差异
git diff
// 查看已经暂存起来的文件和上次提交时的快照之间的差异
git diff --staged/–cached
// 工具diff
git difftool
// 工具diff(所有文件一起显示)(kdiff3可以,p4Merge不可以)
git difftool --dir-diff
git difftool -d

// 修正提交内容(有些文件忘了提交或者想要修改提交注释)
// 下面两个提交会合并成一个提交
git stage file1
git commit -m “commit 1”
git stage file2
git commit --amend -m “commit 1 and 2”
git push -f

// 取消已暂存的文件
git reset HEAD <文件名>
// 取消对文件的修改
git checkout – <文件名>
// 取消对所有文件的修改
git checkout .
// 撤销某次提交内容及之后的所有提交内容,恢复到所选提交id的前一次提交id的状态
// 撤销完成后如果没有冲突的话会自动新建一个commit
git revert <提交id>
// 撤销远程仓库的提交
git reset --hard <提交id> // 提交id是想保留的最近的一次提交id
git push -f origin <分支名>
或者
git checkout <提交id>
git push -f origin HEAD:<分支名>

// 查看所有本地branch
git branch
// 查看所有远程branch
git branch -r
// 查看所有本地和远程branch
git branch -a
// 查看所有本地和远程branch(带最新一次commit id 和 commit log)
git branch -av
// 查看所有branch,并显示最后一次commit信息
git branch -v
// 切换到指定的branch
git checkout <分支名>
// 从远程仓库checkout分支
git checkout -b <本地分知名> origin/<远程仓库分支名>
git checkout --track origin/<远程仓库分支名>
// 创建branch(不会自动切换到创建的branch)
git branch test-branch
// 创建branch(并切换到创建的branch)
git checkout -b test-branch
// 基于某个branch创建branch(并切换到创建的branch)
git checkout -b test-branch base-branch
// 基于某个hash值创建branch(并切换到创建的branch)
git checkout -b test-branch f9ea07b74b4b00
// push本地分支到远程仓库
git push --set-upstream origin test-branch
// 将其他分支的内容merge到master
git checkout master
git merge test-branch
// 查看那些分支已经被合并到当前分支
git branch --merged
// 查看那些分支还没有被合并到当前分支
git branch --no-merged
// 删除分支
git branch -d test-branch
// 把某次的提交内容合并到当前分支
git cherry-pick <提交id>

// 删除远程分支
git push origin :<远程分支名>

// 显示所有 tag
git tag
// 显示所有1.x的tag
git tag -l “v1.*”
// 显示tag的详细信息
git show v1.0
// 创建 tag
git tag -a v1.0 -m ‘tag v1.0’
// 以某个提交 id 创建 tag  
git tag -a v1.0 <提交 id> -m ‘tag v1.0’
// 将创建的 tag 推送到远程仓库
git push origin v1.0 // 单个 tag
git push --tags 或者 git push origin --tags // 所有 tag

// 查看log
git log
// 查看log(每条log一行显示)
git log --oneline
// 查看log并显示简要的增高行数
git log --stat
// 查看提交时的补丁信息
git log -p
// 查看最近2次log
git log -2
// 以关键字搜索log
git log -S <关键字>
// 显示所有提交过的用户和提交message
git shortlog
// 显示所有提交过的用户,按提交次数排序
git shortlog -sn
// 查看所有分支log
git log --all
// 查看所有分支log,以图形的方式显示
git log --all --graph
// 显示指定文件是什么人在什么时间修改过
git blame [file]
// 查看所有分支的所有操作记录(注意是操作记录,不仅仅局限于提交纪录)
git reflog
// 使用可视化工具查看log
gitk

// 添加一个远程版本库,并指定名称为 github
git remote add github
// 查看远程版本库信息
git remote -v
// 查看指定的远程仓库信息
git remote show origin
// 重命名远程仓库(默认的远程仓库名为 origin)
git remote rename <原仓库名> <新仓库名>
// 删除远程仓库
git remote rm <仓库名>

// 缓存当前增在进行的工作
git stash
git stash save
git stash save “stash name”
// 缓存当前增在进行的工作(包括没有被追踪的文件)
git stash --include-untracked
git stash -u
// 查看所有stash
git stash list
// 恢复缓存(不加缓存名称则默认最近一次缓存)
git stash apply
git stash apply stash@{0}
// 恢复缓存并删除该缓存
git stash pop
git stash pop stash@{0}
// 删除缓存
git stash drop
git stash drop stash@{0}
// 显示缓存内容(文件一览)
git stash show stash@{0}
// 显示缓存内容(差分一览)
git stash show -p stash@{0}

// 通过 hash 值查看对象的类型
git cat-file -t f9ea07b74b4b00
// 通过 hash 值查看对象的大小
git cat-file -s f9ea07b74b4b00
// 通过 hash 值查看对象的内容
git cat-file -p f9ea07b74b4b00

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值