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

推荐订阅源

S
Security Affairs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Jina AI
Jina AI
P
Palo Alto Networks Blog
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Blog — PlanetScale
Blog — PlanetScale
S
Schneier on Security
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
雷峰网
雷峰网
T
Tenable Blog
人人都是产品经理
人人都是产品经理
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
AWS News Blog
AWS News Blog
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
Scott Helme
Scott Helme
SecWiki News
SecWiki News
C
CERT Recently Published Vulnerability Notes
Recorded Future
Recorded Future
I
InfoQ
Security Archives - TechRepublic
Security Archives - TechRepublic
Help Net Security
Help Net Security
Cloudbric
Cloudbric
C
Check Point Blog
Engineering at Meta
Engineering at Meta
TaoSecurity Blog
TaoSecurity Blog
B
Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
N
News and Events Feed by Topic
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
腾讯CDC
量子位
Application and Cybersecurity Blog
Application and Cybersecurity Blog
K
Kaspersky official blog
Vercel News
Vercel News
F
Full Disclosure
T
Troy Hunt's Blog
Forbes - Security
Forbes - Security
S
Security @ Cisco Blogs

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