Git:常用功能 - 命令行

  1. 正在某个分支进行开发,突然有个紧急BUG需要切换到其他分支进行修复?

git add README.md # 将文件添加到暂存区

git stash save "v1.0.0 in development" # 贮藏暂存区的修改

# do something in other branch & checkout previous branch

git stash list # 显示贮藏列表
# output:"stash@{0}: On master: v1.0.0 in development"

git stash pop stash@{0} # 应用贮藏的修改

 

2. 提交了N个版本后发现代码有问题,想重置代码到某个版本?

git log --pretty=format:"%h %an %ar: %s" # 输出日志,格式:提交的简短哈希值 作者 提交时间 提交
# output:"
ac4b4fc YaungOu 4 minutes ago: fix(bug-1004): fix something
868e045 YaungOu 5 minutes ago: fix(bug-1003): fix something
aa13e9b YaungOu 5 minutes ago: fix(bug-1002): fix something
4169d73 YaungOu 6 minutes ago: fix(bug-1001): fix something
a142e95 YaungOu 7 minutes ago: fix(bug-1000): fix something
c221ede YaungOu 75 minutes ago: init
"

git reset --hard 868e045 # 重置提交历史到该版本,并丢弃所有修改

  

  3. 重置代码到某个版本,不小把有用的提交也重置?

git reflog # 查看所有操作记录(包括reset操作)
# output:"
868e045 HEAD@{14}: reset: moving to 868e045
ac4b4fc HEAD@{15}: commit: fix(bug-1004): fix something
868e045 HEAD@{16}: commit: fix(bug-1003): fix something
aa13e9b HEAD@{17}: commit: fix(bug-1002): fix something
4169d73 HEAD@{18}: commit: fix(bug-1001): fix something
a142e95 HEAD@{19}: commit: fix(bug-1000): fix something
c221ede HEAD@{23}: commit (initial): init
"

git branch resume-master ac4b4fc # 基于某个版本创建新的分支 # 合并分支或者创建、应用补丁

  

  4. 不小心清空了贮藏的修改(贮藏也要好好写备注喔(●'◡'●),不然想搜索也难)

git fsck --lost-found | awk '{print $3}' | xargs git show | grep -C 5 v1.0.5 # 找回丢失的对象 | 获取对象哈希值 | 显示对象内容 | 根据关键搜索
# output:"
Checking object directories: 100% (256/256), done.
commit 52b018c63226ec7e1f4ddfc349111840dc61c6cc
Merge: a142e95 e9c39a1
Author: YaungOu
Date: Wed May 17 01:22:56 2017 +0800

On master: v1.0.5 in development


commit 635b85f8113df5672c91274d925b4d66d6eae6b7
Author: YaungOu
Date: Tue May 16 22:56:44 2017 +0800
"

git stash apply 52b018c63226ec7e1f4ddfc349111840dc61c6cc # 应用上一个命令得到的HashId对应的修改

  未完待续。。。

posted @ 2017-05-16 23:57  無限の剣製  阅读(250)  评论(0)    收藏  举报