TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Common Git Problems and How to Fix Them

228 点作者 mzehrer将近 7 年前

19 条评论

alangpierce将近 7 年前
I think the most powerful tool for fixing mistakes is &quot;git reflog&quot;. It doesn&#x27;t fix everything, but it works very well as long as you view git with the right mental model: a git repo is an ever-growing tree of immutable commits, with branches and tags pointing to the most interesting ones. As long as code has ever made it into that tree (by being in any commit at any time), it&#x27;s recoverable, and reflog lets you trace your steps back to any point in the past on that tree. Supposedly-destructive operations like amend and rebase actually just build the tree at a different point and move branches to other places in the tree, but (pretty much) nothing in the tree is ever destroyed.<p>For the actually-destructive git commands like checkout and reset, another tool that I&#x27;d highly recommend is a &quot;local history&quot; feature in your editor. JetBrains IDEs do this by default, and other editors have plugins for it. It automatically records changes as you make them so you can go back to previous versions of any file. Usually git is enough to save me, but I&#x27;ve also had plenty of times where I make some mistake outside of git commit history and am saved by digging through local history.
评论 #17633470 未加载
评论 #17633679 未加载
评论 #17633811 未加载
评论 #17634384 未加载
评论 #17633342 未加载
michaelmrose将近 7 年前
Can the main article link to <a href="https:&#x2F;&#x2F;www.codementor.io&#x2F;citizen428&#x2F;git-tutorial-10-common-git-problems-and-how-to-fix-them-aajv0katd" rel="nofollow">https:&#x2F;&#x2F;www.codementor.io&#x2F;citizen428&#x2F;git-tutorial-10-common-...</a> which is vastly more readable.
评论 #17634365 未加载
评论 #17638162 未加载
评论 #17635923 未加载
ecthiender将近 7 年前
Why aren&#x27;t code snippets part of the article? Why are they hosted somewhere else? Especially when they are 2-3 line snippets.
评论 #17633161 未加载
评论 #17633106 未加载
评论 #17633028 未加载
nerdponx将近 7 年前
I&#x27;m sorry, I hate &quot;recipe style&quot; Git articles.<p>Understanding reset and checkout is not hard if you understand the underlying data model. If you don&#x27;t understand te data model, it&#x27;s all black magic.<p>Interestingly, I feel the same way about actual recipe books for cooking. It&#x27;s one thing to keep around as a reference. But if you don&#x27;t know what a bay leaf tastes like and what it does to a dish, you won&#x27;t learn anything from someone telling you to use it in a particular recipe.
评论 #17633932 未加载
评论 #17632871 未加载
levi_n将近 7 年前
I found the list helpful, with just about everything I would expect in a list such as this.<p>If I could make one suggestion: I found it to be quite annoying to have to click through to a two-line gist for every single one command. Having those commands be in the article itself would be considerably easier to read.
评论 #17634996 未加载
0x0将近 7 年前
Don&#x27;t use push --force, use push --force-with-lease. Then you avoid accidentally undoing unexpected new commits on the remote.
评论 #17632977 未加载
评论 #17632883 未加载
评论 #17634223 未加载
评论 #17634112 未加载
cdubzzz将近 7 年前
Another reference site that has come in handy for me for quick reminders, and has a very memorable name -- <a href="http:&#x2F;&#x2F;ohshitgit.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;ohshitgit.com&#x2F;</a>
评论 #17632974 未加载
magoon将近 7 年前
I advocate:<p>git add -p<p>for interactive staging. It’ll present each hunk of code with a y&#x2F;n prompt. This is a good habit to prevent committing any debug code or stray marks.
评论 #17633871 未加载
评论 #17633934 未加载
评论 #17633603 未加载
Hello71将近 7 年前
In addition to what nerdponx said about recipe style articles being poor, there are some moderate issues with this article:<p>1. it doesn&#x27;t clearly say that git checkout with a path is irreversible.<p>2. it says that git reset --hard is irreversible, which is not correct. (see 3.)<p>3. it doesn&#x27;t mention one of the most powerful git features for fixing mistakes, the reflog.<p>4. git remove is not a git command.<p>5. gitingore is not a git file.<p>6. git-amend is not a git command.<p>7. It doesn&#x27;t adequately explain why force-pushing causes problems.<p>It looks like clicking through to the gists was such a pain that even the author didn&#x27;t proofread them.
评论 #17632962 未加载
davidgerard将近 7 年前
Mods - please link the original, which has the code fragments inline:<p><a href="https:&#x2F;&#x2F;www.codementor.io&#x2F;citizen428&#x2F;git-tutorial-10-common-git-problems-and-how-to-fix-them-aajv0katd" rel="nofollow">https:&#x2F;&#x2F;www.codementor.io&#x2F;citizen428&#x2F;git-tutorial-10-common-...</a>
Tomis02将近 7 年前
Can someone explain to me the fascination for git, despite the constant stream of &quot;how to fix [<i>insert various git issues</i>]&quot; articles showing it&#x27;s very difficult to use, at least compared to the alternatives? Most people&#x27;s work streams only need something as simple as hg, which would also save them a lot of time and hassle.<p>My personal impression is that it&#x27;s a fashion thing (people think it&#x27;s cool to waste a lot of time on git, because git is cool and fixing git problems feels like real work, because you&#x27;re using your keyboard and all that), but maybe someone has a different perspective.
majewsky将近 7 年前
I&#x27;m seeing only github gist URLs instead of code snippets. What became of progressive enhancement?
评论 #17632981 未加载
arenaninja将近 7 年前
Out of all these, I wish I could use git bisect the most. Working on large codebases with large teams, the command itself is a godsend.<p>Unfortunately a PC with Symantec Endpoint Protection makes the filesystem dog slow to the point that the one time I needed it and it should&#x27;ve taken about 13 steps to find where the bug was introduced, it was faster to redo the feature without accessing the previous code
评论 #17633324 未加载
评论 #17633880 未加载
评论 #17632926 未加载
nottorp将近 7 年前
So why do i have to open 10 more tabs to see the actual commands? Or am I supposed to blindly run them without knowing what they are?
Groxx将近 7 年前
As much as I like rerere, I have had it result in silently[1] producing the wrong end result. Especially since I merge &#x2F; rebase frequently, and end up recording lots of small conflict resolutions. Always always always check non-clean merges by hand or you&#x27;ll eventually be surprised.<p>And sometimes I just resolved it incorrectly once, and now it always does it wrong. Is there a way to make rerere forget a resolution?<p>[1]: relatively speaking. it doesn&#x27;t fail &#x2F; pause the operation, so it&#x27;s &quot;silent&quot;, even though it prints out that it applied a conflict-fix.
microtherion将近 7 年前
Was I the only one who was expecting something along THOSE lines? <a href="https:&#x2F;&#x2F;i.pinimg.com&#x2F;originals&#x2F;4d&#x2F;ca&#x2F;1c&#x2F;4dca1ce93db819647f2f87fb92769561.jpg" rel="nofollow">https:&#x2F;&#x2F;i.pinimg.com&#x2F;originals&#x2F;4d&#x2F;ca&#x2F;1c&#x2F;4dca1ce93db819647f2f...</a>
StavrosK将近 7 年前
<p><pre><code> function fuckgit rm -rf &#x2F;tmp&#x2F;fuckgit&#x2F; git clone --no-checkout (git config --get remote.origin.url) &#x2F;tmp&#x2F;fuckgit&#x2F; rm -rf .git&#x2F; mv &#x2F;tmp&#x2F;fuckgit&#x2F;.git . rm -rf &#x2F;tmp&#x2F;fuckgit&#x2F; end</code></pre>
评论 #17633300 未加载
paulddraper将近 7 年前
Need &quot;missing blob&quot; (corrupted git repo).<p>That problem is a depressing one and not uncommon [1]<p>[1] <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;q&#x2F;18678853&#x2F;1212596" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;q&#x2F;18678853&#x2F;1212596</a>
throw7将近 7 年前
incredibly annoying how the examples are links to another site. had to stop at the 3rd example.