惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Threatpost
NISL@THU
NISL@THU
L
LINUX DO - 最新话题
T
The Exploit Database - CXSecurity.com
Schneier on Security
Schneier on Security
I
Intezer
T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
S
Secure Thoughts
U
Unit 42
aimingoo的专栏
aimingoo的专栏
L
LangChain Blog
B
Blog RSS Feed
Forbes - Security
Forbes - Security
D
DataBreaches.Net
D
Docker
M
MIT News - Artificial intelligence
F
Full Disclosure
N
News and Events Feed by Topic
T
Tor Project blog
MyScale Blog
MyScale Blog
博客园 - 叶小钗
SecWiki News
SecWiki News
IT之家
IT之家
云风的 BLOG
云风的 BLOG
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Security Blog
Microsoft Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
The Register - Security
The Register - Security
T
Threat Research - Cisco Blogs
博客园 - 聂微东
T
Troy Hunt's Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Security Latest
Security Latest
N
News and Events Feed by Topic
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
P
Proofpoint News Feed
S
Securelist
PCI Perspectives
PCI Perspectives
美团技术团队
V
V2EX
P
Privacy & Cybersecurity Law Blog

Xudong's Blog

AI杀死了个人博客 让MDC在各种线程间穿梭自如 新手选购微单的策略 一些让Python代码更快的技巧 HTTP报文结构 Python日志库logging总结 写定向爬虫时遇到的问题 Java中的协变与逆变 给Hexo主题添加LaTeX公式支持 Python3中的Iterator与Iterable Rss订阅源分享 Redis的数据库与持久化 Redis中的数据结构 控制反转与依赖注入 Vim命令笔记 Java中的equals和hashCode方法 Java中对象域的初始化 Java中的基本类型和自动装拆箱 Hello, world. 使用Hexo搭建静态博客
Git Tips
Xudong Sun · 2019-04-17 · via Xudong's Blog

作为强大的版本管理工具,基本的Git命令几乎是每个程序员都应该掌握的。在实际使用中,在紧急的关口,我们需要祭出这个神奇的时间机器,却突然根本不记得Git的不常见命令,这种尴尬时刻可能很多人都遇到过。查官方文档固然是一个好办法,不过下面的一些tips可能会帮你节省一些时间。

使用Git回溯到之前的commit

当你修改了一个配置文件,发现项目启动失败。当你重构了一些代码,发现程序出现bug。当你…。总是不管出现什么问题,你过去几个小时的努力可能要白费了,你想让一切恢复原状,那么就需要回溯到之前某一个正常的commit。

1
2
3
4
5
6
git reflog
f8fd780 (HEAD -> master, origin/master, origin/HEAD) HEAD@{0}: commit: 6/10
176bc3a HEAD@{1}: commit: xxxx
48b3b87 HEAD@{2}: commit: xxxx

git reset HEAD@{index}

第一个命令列出了过去提交的更改,可以看到第二列的格式分支名称@{下标}。第二个命令就可以帮我们的repo回到当时的状态。不管你中间执行了什么样的更改,git都可以把我们的项目带回当时的状态,神奇的时间机器。

刚commit 又有新的小改动

提交commit总是让人开心,以至于飞速的敲下熟悉的命令,却忘了刚才的代码还漏了一个大括号,或者你觉得需要加个注释。你又得把刚才的一串命令再来一遍。而下面的命令可以帮你节省时间。

1
2
git add . # 或者 git add xxx.xx
git commit --amend

还有一种情况,你只是觉得刚才的commit message写得很差劲(清楚的提交信息是很重要的),而项目本身并没有改动。你需要下面的命令。

1
git commit --amend -m "new-message"

改动提交到了错误的分支

1
2
3
4
5
6
7
git reset HEAD~ --soft
git stash

git checkout CORRECT-branch
git stash pop
git add .
git commit -m "xxx"

经过上面的一串命令,改动就被移动到了正确的分支上。

还有另一种实现方式是使用cherry-pick。

1
2
3
4
git checkout CORRECT-branch
git cherry-pick master
git checkout master
git reset HEAD~ --hard

只clone 最新一次提交

有时拉取大项目确实很耗时,只clone最近一次提交,会节省 clone时间。

1
git clone --depth=1 https://some.github.url/user/repo.git

在diff比较两个文件时没有反应

Git不会比较已经被add到暂存区的文件。所以你需要

1
git diff --staged

查看某段代码的作者

1
git blame rubbish.c

blame 👿

我选择死亡

1
2
3
4
cd ..
sudo rm -rf repo-dir
git clone https://some.github.url/repo-dir.git
cd repo-dir

从Git Repo中彻底删除

  1. 从资料库中删除文件

    1
    git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch [Path To Your Remove File]' --prune-empty --tag-name-filter cat -- --all
  2. 强制推送到远程Repo

    1
    git push origin master --force --all
  3. 回收空间

    1
    git gc --prune=now