2016.09.10
最近项目版本管理从SVN切换到Git,下面记录下实际当中常用到的一些命令,方便查阅。
当然,GUI操作的工具非常不错,但是命令行操作那 “
键指如飞” 的感觉相当吸引我。
vim的一些常用命令:
按下ESC键,退出编辑模式,切换到命令模式:
| Vim命令 | 命令说明 |
|---|---|
vi filename
|
创建打开文件 |
| :w | 保存 |
| :q | 退出 |
| ZZ、:wq | 保存退出 |
| ZQ、:q! | 退出不保存 |
| :e! | 放弃修改 |
:w filename
|
另存为 |
| x | 删除当前光标下的字符 |
| X | 删除当前光标左边的字符 |
| s | 修改一个字符 |
| i | 在光标之前插入 |
| a | 在光标之后插入 |
| diw | 删除光标所在的单词,不包括空白字符 |
| daw | 删除光标所在的单词,包括空白字符 |
| dw | 从当前位置删除到下一个单词词音 |
| D | 删除到行尾的内容 |
| C | 修改到行尾的内容 |
| S | 修改一整行 |
| dd | 删除一整行 |
| yy | 复制一整行 |
| u | 撤销改动 |
| P | 粘贴到光标之前 |
| p | 粘贴到光标之后 |
| d | 剪切 |
| :set nu! | 显示行号 |
| :set autoindent | 自动缩进 |
| :set warp | 自动换行 |
下面这张示意图是从VIM常用命令示意图找来的:

Vim常用命令示意图
git的一些常用命令操作:
记录跟踪:
| Git命令 | 命令说明 |
|---|---|
| git status | 检查当前文件状态 |
| git status -s | 状态简览 |
git add filename
|
跟踪新文件 (已暂存状态) |
| cat .gitignore | 列出要忽略的文件模式 |
| git diff | 查看已暂存和未暂存的修改 |
git diff --cached (git diff --staged) |
若要查看已暂存的将要添加到下次提交里的内容 |
提交更新、移动、移除:
| Git命令 | 命令说明 |
|---|---|
| git commit | 提交更新 |
git commit -m "commit info" |
提交更新,并添加提交信息 |
git commit -a -m "commit info" |
跳过使用暂存区域 |
rm filename
|
移除文件 |
git rm filename
|
记录此次移除文件的操作 |
git rm --cached filename
|
让文件保留在磁盘,但不想让Git继续跟踪 |
git rm log/*.log
|
删除log/目录下扩展名为.log的所有文件 |
git mv file_from file_to
|
移动文件 |
提交历史:
| Git命令 | 命令说明 |
|---|---|
| git log | 查看提交历史 |
git log -p -2
|
显示最近两次提交的内容差异 |
git log --pretty=oneline 、git log --pretty=format:"%h - %an,%ar : %s" 、git log --pretty=format:"%h %s" --graph
|
指定使用不同于默认格式的方式展示提交历史(oneline、short、full、fuller) |
git log --since=2.weeks
|
日志限制输出长度 |
git log -Sfunction_name
|
某一个特定函数的引用的提交 |
git log --since="2016-09-07" |
指定时间之后提交信息 |
| git commit --amend | 重新提交 |
git reset HEAD filename
|
取消暂存的文件 |
git checkout -- filename
|
撤销对文件的修改 |
远程仓库:
| Git命令 | 命令说明 |
|---|---|
git clone https://example.com/gitproject.git
|
克隆现有的仓库 |
git clone https://example.com/gitproject.git myTest
|
并设置本地仓库名字 |
| git remote | 查看远程仓库 |
| git remote -v | 读写远程仓库使用的Git保存的简写与其对应的URL |
git remote add test https://example.com/gitproject.git
|
添加远程仓库 |
git fetch remote-name
|
从远程仓库中抓取与拉取 |
git push remote-name branch-name
|
推送到远程仓库 |
git remote show origin
|
查看远程仓库 |
git remote rename testProject test
|
远程仓库重命名 |
git remote rm test
|
远程仓库移除 |
标签:
| Git命令 | 命令说明 |
|---|---|
| git tag | 列出标签 |
git tag -l 'v1.6.2' |
以特定的模式查找标签 |
git tag -a v1.6.3 -m 'my version 1.6.3' |
创建并附注标签 |
git show v1.6.3
|
查看标签信息以及对应的提交信息 |
git tag v1.4-lw
|
轻量标签 |
git tag -a v1.2 9fceb02
|
后期打标签(校验和) |
git push origin v1.5
|
共享标签 |
| git push origin --tags | 共享标签tags |
git checkout -b branchname tagname
|
检出标签 |
别名:
| Git命令 | 命令说明 |
|---|---|
git config --global alias.last 'log -1 HEAD' --> git last
|
查看最后一次提交 |
分支:
| Git命令 | 命令说明 |
|---|---|
git branch test
|
分支创建 |
| git log --oneline --decorate | 查看各个分支当前所指的对象 |
| git log --oneline --decorate --graph --all | 查看分支历史 |
git checkout test
|
分支切换 |
git checkout -b test01
|
新建分支并同时切换到那个分支上 |
git merge test01
|
合并分支 |
git branch -d test01
|
删除分支 |
| git branch | 查看所有分支 |
| git branch -v | 查看每一个分支的最后一次提交 |
| git branch --merged | 查看哪些分支已经合并到当前分支 |
| git branch --no-merged | 查看所有包含未合并工作的分支 |
git push --set-upstream origin test01
|
把本地分支推送到远程 |
远程分支:
| Git命令 | 命令说明 |
|---|---|
| git ls-remote origin | 获取远程引用的完整列表 |
| git remote show origin | 获取远程分支更多的信息 |
git push origin testFix:testFix
|
推送本地分支作为远程分支 |
git checkout --track origin/testFix
|
跟踪分支 |
git checkout -b sf origin/testFix
|
将本地分支与远程分支设置为不用名字 |
| git branch -vv | 将本地分支列出并包含更多的信息 |
git push origin --delete test
|
删除远程分支 |
协议:
| Git命令 | 命令说明 |
|---|---|
git remote add local_proj /opt/git/project.git
|
添加一个本地版本到现有的Git项目 |
git clone ssh://user@server/project.git
|
SSH协议 |
git clone https://example.com/gitproject.git
|
HHTP协议 |