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.

Lesser known Git commands

262 pointsby guomanminover 8 years ago

31 comments

dahartover 8 years ago
I&#x27;ve seen more stash accidents than any other kind with git. Stashing is more dangerous than committing or branching, and to me it doesn&#x27;t seem to provide any advantages... do people actually find stashing easier than branching? Is it just because when you branch you have to name it, and that causes friction? I stay away from stash.<p>Right from the man page: &quot;If you mistakenly drop or clear stashes, they cannot be recovered through the normal safety mechanisms.&quot;<p><a href="https:&#x2F;&#x2F;git-scm.com&#x2F;docs&#x2F;git-stash" rel="nofollow">https:&#x2F;&#x2F;git-scm.com&#x2F;docs&#x2F;git-stash</a><p>--<p>I do like shorty. My version:<p><pre><code> alias gits=&#x27;git status -sb&#x27; </code></pre> --<p>My next fave is rebase the current branch against it&#x27;s upstream branch point:<p><pre><code> [alias] rearrange = &quot;!git rebase -i $(git merge-base HEAD @{u})&quot; </code></pre> git rearrange ftw.
评论 #12613518 未加载
评论 #12614501 未加载
评论 #12614811 未加载
评论 #12613205 未加载
评论 #12613699 未加载
评论 #12613073 未加载
评论 #12614318 未加载
评论 #12614346 未加载
评论 #12613611 未加载
评论 #12613596 未加载
评论 #12616240 未加载
评论 #12616360 未加载
评论 #12613917 未加载
评论 #12613439 未加载
评论 #12613989 未加载
评论 #12614717 未加载
masklinnover 8 years ago
Warning: not actually git commands, rather TFAA&#x27;s aliases for possibly useful combinations of switches and&#x2F;or commands.
评论 #12612873 未加载
评论 #12613107 未加载
评论 #12612914 未加载
评论 #12613499 未加载
opkover 8 years ago
Some of my aliases, first some basic shortcuts:<p><pre><code> freshen = commit --amend --no-edit --date=now cont = rebase --continue br = branch --column recent = branch --sort=committerdate idiff = diff --cached ff = merge --ff-only co = checkout </code></pre> Show what&#x27;s left to do in an interactive rebase:<p><pre><code> todo = !cat `git rev-parse --git-dir`&#x2F;rebase-merge&#x2F;git-rebase-todo </code></pre> Set tracking to to origin&#x2F;&lt;the same branch name&gt;<p><pre><code> upstream = !zsh -c &#x27;git branch --set-upstream-to=origin&#x2F;$(git symbolic-ref --short HEAD) $(git symbolic-ref --short HEAD)&#x27;</code></pre> I use this to swap my work to home e-mail or vice-versa if I&#x27;ve got it wrong. Need to specify an initial commit.<p><pre><code> allmine = filter-branch --env-filter &#x27;GIT_COMMITTER_EMAIL=my@email.com GIT_AUTHOR_EMAIL=my@email.com&#x27; vim = &quot;!gvim `git ls-files -m`&quot;</code></pre>
CGamesPlayover 8 years ago
A few that weren&#x27;t mentioned below:<p><pre><code> git alias start &#x27;checkout @{u} -B&#x27; </code></pre> All my branches track origin&#x2F;master (rather than local master), which makes it easy to git push &#x2F; git pull without extra arguments. This will checkout a new branch from whatever the current branch is tracking. Usage: `git start my-new-feature`<p><pre><code> git alias track &#x27;track = branch --set-upstream-to&#x27; </code></pre> Sets up your current branch to track some other branch. Usage: `git track origin&#x2F;master`.<p><pre><code> git alias gcbr &#x27;gcbr = !git branch --no-track --no-color --merged | sed &#x27;s&#x2F;[ *]*&#x2F;&#x2F;&#x27; | grep -v master | xargs -n1 git branch -d &amp;&gt; &#x2F;dev&#x2F;null || exit 0&#x27; </code></pre> &quot;Garbage Collect BRanches&quot; will delete any branches which are already merged into your current branch (excluding master). Basically, any branch which is &quot;safe&quot; to delete. (This one has been handed down through the ages; I initially found it on the skeleton dotfiles when I started working at Facebook. Thanks, whoever!)
评论 #12615283 未加载
k__over 8 years ago
I&#x27;m no Git wizard, but switching from &quot;merge&quot; to &quot;rebase&quot; and from &quot;add &lt;file&gt;&quot; to &quot;add -p&quot; cleaned up my repos quite a bit.
评论 #12613291 未加载
michaelmiorover 8 years ago
&gt; so it’s good practice to create an empty commit as your repository root<p>While I&#x27;m aware of issues with rebasing the root commit, I&#x27;ve never heard this advice before and it seems unnecessary.
评论 #12613132 未加载
评论 #12613200 未加载
评论 #12614276 未加载
评论 #12613145 未加载
leni536over 8 years ago
&gt; git it and empty root commit<p>Why doesn&#x27;t git init does this by default? It would be nice if every repo had an empty root commit (with no author, date,... so it has the same commit hash), then every two repos would have a common commit. Semantically it would mean that repos would be just specific branches of a hypotetical large repo.
评论 #12613409 未加载
评论 #12613362 未加载
creebleover 8 years ago
This doesn&#x27;t even look like I wrote it, but I thought I did and use it all the time: &quot;What was that (private) branch I worked on a few weeks ago?&quot;<p>Just put this shell script in &#x2F;usr&#x2F;local&#x2F;bin or wherever as &quot;git-branch-dates&quot;<p><pre><code> git branch-dates #!&#x2F;bin&#x2F;bash for k in `git branch|perl -pe s&#x2F;^..&#x2F;&#x2F;`;do echo -e `git show --pretty=format:&quot;%Cgreen%ci %Cblue%cr%Creset&quot; $k|head -n 1`\\t$k;done|sort -r</code></pre>
aequitasover 8 years ago
<p><pre><code> alias such=git alias very=git alias wow=&#x27;git status&#x27; $ wow $ such commit $ very push </code></pre> <a href="https:&#x2F;&#x2F;twitter.com&#x2F;chris__martin&#x2F;status&#x2F;420992421673988096?lang=en" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;chris__martin&#x2F;status&#x2F;420992421673988096?...</a>
SEJeffover 8 years ago
My personal favorite two:<p><pre><code> jschroeder@omniscience:~$ git config alias.up pull --rebase jschroeder@omniscience:~$ git config alias.down push </code></pre> They can be used thusly:<p><pre><code> git up &amp;&amp; git down</code></pre>
评论 #12613035 未加载
评论 #12613154 未加载
评论 #12613084 未加载
评论 #12613056 未加载
评论 #12614024 未加载
评论 #12614324 未加载
评论 #12613420 未加载
radarsat1over 8 years ago
I like the `git grog` command, I might actually use that.<p>The idea of `git please` is not bad but it made me wonder if it could be possible to go even further.. a variant on --force that only force-pushes IF NOT ONE HAS PULLED YET.<p>I can see why this wouldn&#x27;t be built into git core however, as it&#x27;s quite stateful and depends on tracking other people&#x27;s fetches, which probably isn&#x27;t easy.
anarcatover 8 years ago
one thing i wish git would do would be to allow me to specify default options for certain aliases. for example, I quite like the `--short --branch` format of git status. but I can&#x27;t do this:<p><pre><code> [alias] status = &quot;status -sb&quot; </code></pre> because git just ignores those. i am then forced to setup non-standard commands just to bend git to my will.<p>in some cases, there are distinct `git-config` options I can set to get the right behavior, but that&#x27;s way less flexible since stuff like `--force-with-lease` don&#x27;t have their own config options.<p>the rationale behind this is, according to the git-config manpage, that &quot;To avoid confusion and troubles with script usage, aliases that hide existing git commands are ignored&quot;. but then there&#x27;s --porcelain for that, so i don&#x27;t understand that limitation.<p>sigh.
评论 #12615412 未加载
relics443over 8 years ago
Some of the aliases are funny but could be renamed for brevity.<p>Also grog is making me feel extremely groggy.
评论 #12612885 未加载
评论 #12614130 未加载
michaelmiorover 8 years ago
&gt; If in doubt, the long one (git staaash) will always restore your worktree to what looks like a fresh clone of your repository.<p>Not necessarily. This depends on what branch you&#x27;re on as well as whether or not it&#x27;s up to date with the remote.
poormanover 8 years ago
My most useful bash alias (when you go back to master and want to start at the most up to date everything before creating a new branch):<p><pre><code> alias grm=&quot;git fetch origin &amp;&amp; git reset --hard origin&#x2F;master&quot;</code></pre>
评论 #12613087 未加载
kuahyeowover 8 years ago
My two favourite aliases :<p><pre><code> mo = ls-files -m lol = log --oneline --decorate --graph </code></pre> `git mo tests&#x2F;` gets you modified files for the tests folder.<p>`git lol` I think as the command line version of gitk
SonOfLilitover 8 years ago
Nothing esoteric, but I advise every person I teach git about to put this in their .bashrc:<p><pre><code> alias s=&#x27;git status&#x27; alias l=&#x27;git log --graph --oneline --decorate --all --show-signature&#x27; alias d=&#x27;git diff&#x27; alias dc=&#x27;git diff --cached&#x27; alias c=&#x27;git commit -m&#x27; alias ca=&#x27;git commit -am&#x27; alias a=&#x27;git add -p&#x27; alias u=&#x27;git checkout -p&#x27; </code></pre> These probably saved me hours of typing by now. Especially `l` I think should be canon.
评论 #12613523 未加载
评论 #12613538 未加载
ZenoArrowover 8 years ago
I&#x27;m still getting to know Git, and I appreciate articles like this as they touch on approaches to Git that have benefits in the real world, but I can&#x27;t help but feel that version control tools should be simpler.<p>Does anyone have any experience with version control tools that have worked out simpler to use than Git? I&#x27;ve heard good things about Darcs before (aside from its performance issues, which projects like Pijul are designed to address), are there any other tools that are worth investigating?
评论 #12615887 未加载
评论 #12616386 未加载
neuroidover 8 years ago
<i>The first commit of a repository can not be rebased like regular commits</i><p>That&#x27;s not necessarily true. This can be done with:<p><pre><code> git rebase -i --root</code></pre>
RandomBKover 8 years ago
Nice list! I&#x27;ll adapt some for my git workflow.<p>Here&#x27;s some that I&#x27;ve created or collected over the years, in case anyone finds this useful:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;randombk&#x2F;randombk-dotfiles&#x2F;blob&#x2F;master&#x2F;master-main&#x2F;.gitconfig" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;randombk&#x2F;randombk-dotfiles&#x2F;blob&#x2F;master&#x2F;ma...</a>
daenneyover 8 years ago
For every repo I fork I usually add a remote named upstream pointing to the original.<p>So then, whenever I need to get my copy up to date with upstream it&#x27;s:<p><pre><code> update = !git fetch upstream &amp;&amp; git merge upstream&#x2F;master&#x27; </code></pre> It&#x27;s probably easily changed to make the branch configurable, but I have only ever needed this with master.
shawkinawover 8 years ago
A couple of aliases I use all the time:<p><pre><code> [alias] graph = log --oneline --decorate --graph --all copy-logs-since = !git log --reverse --format=%B &quot;$1&quot;.. | pbcopy </code></pre> `graph` is similar to `grog` in the article. `git copy-logs-since &lt;tag&gt;` copies log messages since the tag, which I use for release notes. Very handy.
CUViperover 8 years ago
&gt; git commend quietly tacks any staged files onto the last commit you created, re-using your existing commit message. So as long as you haven’t pushed yet, no-one will be the wiser.<p>Well, they might notice that the author and committer dates don&#x27;t match, but you can add &quot;--reset-author&quot; to <i>really</i> make this undetectable.
joelthelionover 8 years ago
I like &quot;git ready&quot;:<p><pre><code> ready = !git checkout master &amp;&amp; git fetch -p &amp;&amp; git merge --ff-only </code></pre> The use case is for when you&#x27;re done with a particular feature branch and ready to start something new. It puts you back on master and updates it in a safe way.
swangover 8 years ago
Author&#x27;s &quot;commend&quot; function I just call &quot;cane&quot;<p>git (c)ommit --(a)mend --(n)o-(e)dit
pattu777over 8 years ago
<p><pre><code> git diff origin&#x2F;master -- test.file </code></pre> It will show you a diff of a file between the master branch of remote origin and your local branch. I use it way more than I can think of.
avelover 8 years ago
Didn&#x27;t know about those stash options. I&#x27;ve always used &#x27;git stash save -u&#x27; to bring along the untracked files in the stash, that&#x27;s the more common scenario.
评论 #12612976 未加载
wnevetsover 8 years ago
Heres one I like to use sdiff = diff --ignore-space-change
etqwzutewzuover 8 years ago
One of my personal favorite:<p>stp = !git stash &amp;&amp; git pull &amp;&amp; git stash pop
评论 #12613253 未加载
jackmaneyover 8 years ago
git config --global alias.stache stash
sigmonsaysover 8 years ago
You baitin... how is a lesser known git command a git alias?