TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Git Tips

487 pointsby akras14about 9 years ago

31 comments

majewskyabout 9 years ago
Here&#x27;s my favorite git oneliner:<p><pre><code> git push origin --delete $(git branch --merged origin&#x2F;master -r | grep -v master | grep origin | cut -d&#x2F; -f2-) </code></pre> It deletes all remote branches that are merged into the remote master. Because people always forget to click the &quot;Delete branch&quot; button after merging their pull requests.<p>EDIT: And another thing. Turn all your Git aliases into shell aliases (e.g. &quot;git status&quot; is aliased to &quot;git st&quot; is aliased to just &quot;st&quot; on my system) with this one weird trick! <a href="https:&#x2F;&#x2F;github.com&#x2F;majewsky&#x2F;devenv&#x2F;blob&#x2F;2c4252d37597617a493f1addadacfbf8d88b4942&#x2F;toplevel&#x2F;zshrc#L55-L59" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;majewsky&#x2F;devenv&#x2F;blob&#x2F;2c4252d37597617a493f...</a>
评论 #11536893 未加载
评论 #11536973 未加载
评论 #11534396 未加载
评论 #11537961 未加载
评论 #11550906 未加载
epmatswabout 9 years ago
Love #16 and the associated article. I cringe when I see something like &quot;js fix&quot; as a commit message.<p>However, not on board with the hate for rebase. If it&#x27;s your feature branch and you aren&#x27;t sharing it with others, I&#x27;d much rather get a cleaned up PR than one filled with junk commits because the author was afraid to rebase.
评论 #11532665 未加载
评论 #11532566 未加载
评论 #11532670 未加载
评论 #11534158 未加载
评论 #11537390 未加载
yes_or_gnomeabout 9 years ago
I&#x27;m an avid supporter of rebasing over merging. I&#x27;ll refrain from getting into my workflow, but here&#x27;s what i want to say: For the love of all that is holy, never squash a merge commit nor squash onto a merge commit.<p>Squashing a merge commit is a perfect way to reintroduce old code back into master.<p>Squashing onto a merge commit is great way to lose changes. It&#x27;s been a while since I have tried this, but creating a didactic repo if fairly easy. Create a repo with two feature branches, a file on each of master and the feature branches, merge featureA to featureB, make some changes or delete a file, `commit --amend` on the merge, and merge featureB to master. Then use `git cat-file` to look at those commits and commit trees. I&#x27;ve seen mysterious things such as simple as unreported changes to files mysteriously being deleted from the repo.
azag0about 9 years ago
I use git often on the command line, especially for non-trivial tasks, but do not quite understand why someone would pick it for routine committing&#x2F;tree view over a GUI. I find the ability to graphically see the state of the working tree and the index, and being able to stage&#x2F;unstage individual lines by visual selection in a non-linear way absolutely natural. Likewise, the ability to get a context menu for a commit in a tree view and fire a command (rebase&#x2F;branch most often) seems like something git was &quot;built&quot; for.
评论 #11533239 未加载
评论 #11533220 未加载
评论 #11533273 未加载
评论 #11533217 未加载
评论 #11533161 未加载
评论 #11533263 未加载
评论 #11533287 未加载
rollulusabout 9 years ago
My tip #20: tig [1]<p>[1]: <a href="http:&#x2F;&#x2F;jonas.nitro.dk&#x2F;tig&#x2F;" rel="nofollow">http:&#x2F;&#x2F;jonas.nitro.dk&#x2F;tig&#x2F;</a>
评论 #11532847 未加载
Pirate-of-SVabout 9 years ago
Since interactive rebase was mentioned I&#x27;d recommend taking a look at its --autosquash flag and the --fixup flag of commit. With autosquash the interactive rebase will move fixup commits to where they belong:<p><pre><code> git commit --fixup=&lt;commitref&gt; git rebase -i --autosquash HEAD~5</code></pre>
评论 #11532791 未加载
jdudekabout 9 years ago
My recommendation: configure the shell prompt (add __git_ps1 to $PS1), this saves a ton of time and makes it much easier to understand what is going on. Especially recommended for beginners (but obviously not limited to).
评论 #11533738 未加载
i_have_to_speakabout 9 years ago
Mandatory xkcd reference: <a href="https:&#x2F;&#x2F;xkcd.com&#x2F;1597&#x2F;" rel="nofollow">https:&#x2F;&#x2F;xkcd.com&#x2F;1597&#x2F;</a>
评论 #11535105 未加载
egjerlowabout 9 years ago
For vimmers: <a href="https:&#x2F;&#x2F;github.com&#x2F;tpope&#x2F;vim-fugitive" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tpope&#x2F;vim-fugitive</a> is awesome, especially for only adding only parts of a file to a commit.
评论 #11534998 未加载
notzorbo3about 9 years ago
Site is non-responsive for me. Cached link:<p><a href="http:&#x2F;&#x2F;webcache.googleusercontent.com&#x2F;search?q=cache:04x449vvRhkJ:www.alexkras.com&#x2F;19-git-tips-for-everyday-use&#x2F;&amp;num=1&amp;client=ubuntu&amp;hl=en&amp;gl=nl&amp;strip=1&amp;vwsrc=0" rel="nofollow">http:&#x2F;&#x2F;webcache.googleusercontent.com&#x2F;search?q=cache:04x449v...</a>
rndstrabout 9 years ago
<p><pre><code> git checkout - </code></pre> Switches to the previously checked out branch.
评论 #11533270 未加载
cornstalksabout 9 years ago
I hope I&#x27;m not the only one who thinks that &quot;Update README file&quot; is a poor commit message. It&#x27;s overly nondescript. A better commit message would be &quot;Update README install instructions&quot; or &quot;Fix README links to dependencies&quot; or anything else that better describes what about the README file was updated.
endgameabout 9 years ago
My #1 git tip to anyone who will listen: use magit. Having the options for each command within easy reach makes getting git to do the right thing so much easier, and the UI feels very emacs.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;magit&#x2F;magit" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;magit&#x2F;magit</a>
评论 #11533881 未加载
kevindeasisabout 9 years ago
For git commit messages. I&#x27;ve began liking to initiate or end a statement with stuff like these:<p>¯\_(ツ)_&#x2F;¯<p>( ͡° ͜ʖ ͡°)<p>(ง&#x27;̀-&#x27;́)ง<p>(ノಠ益ಠ)ノ彡┻━┻<p>(゚∀゚)<p>ಠ_ಠ<p>How do you guys feel about these in git commit messages?
评论 #11534452 未加载
评论 #11535319 未加载
ivan_ahabout 9 years ago
At my old job I had to do a major cleanup of a git repo with dozens of old branches. I got really good with the `git log` command. Here my alias form my ~&#x2F;.gitconfig that shows author, branches, and remotes in a colorful manner:<p><pre><code> [alias] ll = log --graph --oneline --decorate --date=short --all --pretty=format:&#x27;%ad %h %Cgreen%an %Cred%d %Creset%s&#x27; </code></pre> With this you have a pretty good picture of what&#x27;s going on accross all branches.
评论 #11533964 未加载
K0nservabout 9 years ago
Great set of tips. Unfortunately I find that so many post that suggest rebase recommend against force pushing. This takes a lot of the power away from rebase&#x2F;amend. So much does this happen that is wrote a blog post titled &quot;You should force push more&quot;[0].<p>0: <a href="https:&#x2F;&#x2F;hugotunius.se&#x2F;2014&#x2F;09&#x2F;08&#x2F;you-should-force-push-more.html" rel="nofollow">https:&#x2F;&#x2F;hugotunius.se&#x2F;2014&#x2F;09&#x2F;08&#x2F;you-should-force-push-more....</a>
评论 #11533213 未加载
BerislavLopacabout 9 years ago
My favourite tip for commit messages: write them as if you would explain someone how to do what was done in the commit. So, instead of &quot;fixed a bug&quot; use &quot;replace datetime object with date&quot;. It can be more abstract for larger commits (e.g. &quot;refactor payments&quot;), but it should be roughly analogue to what you would write to a ticket title to the same effect.
daurnimatorabout 9 years ago
My must have: pre-commit unit tests. See <a href="http:&#x2F;&#x2F;daurnimator.com&#x2F;post&#x2F;134519891749&#x2F;testing-pre-commit-with-git" rel="nofollow">http:&#x2F;&#x2F;daurnimator.com&#x2F;post&#x2F;134519891749&#x2F;testing-pre-commit-...</a>
评论 #11533026 未加载
LaFolleabout 9 years ago
Talking of commit message styling, Antirez (of Redis fame) also has a say. <a href="http:&#x2F;&#x2F;antirez.com&#x2F;news&#x2F;90" rel="nofollow">http:&#x2F;&#x2F;antirez.com&#x2F;news&#x2F;90</a>
xavhanabout 9 years ago
love this one :<p><pre><code> git diff --name-only | uniq | xargs $EDITOR </code></pre> (opens all modified files)<p>and this one to open files with conflicts<p><pre><code> git diff --name-only --diff-filter=U | uniq | xargs $EDITOR</code></pre>
评论 #11533752 未加载
aaossaabout 9 years ago
Am I the only one who&#x27;s strugling to see the website? My Phone is having a bad time trying to show it (Windows Phone, Internet Explorer)
评论 #11534047 未加载
chris_wotabout 9 years ago
I seriously must be doing something wrong, but when I&#x27;m doing an interactive rebase, if it stops due to a merge conflict I sometimes find I need to tweak other files, and to add the files to git I find I do the following:<p><pre><code> git add $(git diff --name-only) </code></pre> Am I wrong, or is this the right way of doing this?
评论 #11536379 未加载
ozimabout 9 years ago
I work with git daily for couple years and I find it quite tiring to follow git branches&#x2F;commits in command line.<p>I rather use Git Extensions on windows and on mac Git Kraken. For beginners I think it would be better to use GUI tools.
评论 #11533085 未加载
pbreitabout 9 years ago
I&#x27;m still not even quite sure how to a) discard edits that I&#x27;ve made but not added&#x2F;committed yet and b) delete something from a repo that I added accidentally.
评论 #11533084 未加载
评论 #11533226 未加载
评论 #11533090 未加载
akavelabout 9 years ago
I&#x27;m collecting my personal notes&#x2F;tips at: <a href="https:&#x2F;&#x2F;gingkoapp.com&#x2F;git-notes" rel="nofollow">https:&#x2F;&#x2F;gingkoapp.com&#x2F;git-notes</a>
michaelmcmillanabout 9 years ago
I never recall the command, but it lives in my bash history somewhere. Running your test suite over a series of commits can be very handy.
评论 #11536444 未加载
评论 #11533648 未加载
educarabout 9 years ago
git log --format=%ae | sort | uniq -c | sort -nr
knorkerabout 9 years ago
&quot;Table of contents&quot;
the_common_manabout 9 years ago
git reflog is my favorite
评论 #11533532 未加载
thihtabout 9 years ago
Wow, I had no idea git bisect was so easy to use
knownabout 9 years ago
Brilliant tips