DEV Community

3 Git Commands I Use Every Day

Jason McCreary on January 16, 2017

Lately I'm paying more attention to the Git commands I run to help script Getting Git. I have so much material at this point, I was told to turn so...
Collapse
 
erebos-manannan profile image
Erebos Manannán

This post really highlights some of my pet peeves with Git and Git users. Git makes you memorize these long lists of undecipherable flags (--patch instead of --interactive?) to do your DAILY tasks properly, and then on top the users end up feeling they are particularly clever having come up with these commands.

Your tools should allow you to work efficiently and correctly WITHOUT having to google for blog posts or spending eons on figuring out the most optimal set of flags. Your tools should NOT make correct use of them make you feel like you've been particularly clever. Git users also tend to have this weird attitude where they think they are clever for using the CLI for everything. Just use the CLI for the things the CLI is good at, and leave the rest for good and efficient GUIs.

I regularly see Git users use git add . && git commit -m '' && git push -f because they're too lazy to figure out how their tools work, as their tools are garbage. You should use tools made for human beings instead of tools made by Linus.

Bottom line for me is that most people using Git should really be using Mercurial. There are some people like Linus who are beyond help and can never see reason, but the rest still have a chance. Why Mercurial? Well it's mostly like Git, except made to be used by people who are not Linus.

  1. You should use a GUI for commits. Really do. You will get a diff of all changes that you SHOULD go through before committing, and you always interactively choose which changes get added. If you must use Git there are nowadays a few available e.g. SourceTree, GitKraken and various others available git-scm.com/downloads/guis .. For Mercurial there has been a really good GUI available for a very long time, TortoiseHG. Never wanted another one, it does everything I need from a GUI.

  2. You should use tools made for human beings, that don't require mystical flags to do your job right. If you need to figure out mystical flags to be able to do interactive commits and choose and view which of your changes go to each commit, you are using the wrong tools. Mercurial btw has much more human readable flags for CLI usage, all the extra stuff you shouldn't need daily or is potentially dangerous is "hidden away" as extensions (e.g. purge extension, which removes all files from your checkout that are not in your repository) , while still most of the useful ones are still bundled with the base distribution and are simply disabled.

  3. Your should use tools that are easily interoperable, and cross-platform, with minimal jumping through hoops. Git was made by Linus, for Linus, and practically only supports his workflow efficiently. Mercurial was made by people, for people, so extensions, hooks, etc. can be written using many different tools, including Python. Python runs on practically every machine ever made, unlike your BASH scripts and random binaries you stick in your Git hooks.

I've been using DVCS since way before most people knew what they were, and I've got a long history using both Git and Mercurial for various kinds of professional and hobby projects. In my experience Git really is unsuitable for anything but the smallest of hobby projects, and I would never elect to use it for any professional work on a project that is expected to grow beyond 1 developer.

Collapse
 
zeeeds profile image
Yazeed Sabri • Edited

I understand and agree with most of what you point out here. But can you go into more details about what you mean when you say Mercurial scales better Git? I have an understanding of how Git suffers at a big scale. It also explains why companies like Google, Facebook, and till very recently Microsoft do not use Git. I know that Facebook uses Mercurial, but I believe they do hack it in some way to use it at such scale. The only reason why Microsoft is switching to Git is they can also afford to hack it, thus their GVFS project.

Collapse
 
erebos-manannan profile image
Erebos Manannán

I have no idea where you read that Mercurial scales better than Git, since I sure didn't use the word "scale". However, if talking about the project and team growing, Mercurial makes it easier for growing teams to extend it to their needs.

It works better when you have team members with significantly varying environments due to the use of Python vs. relying on BASH scripts/binaries for hooks etc, and the extensibility and wide variety of already available great extensions does make it easier to tune how Mercurial works specifically for you.

Facebook does not "hack" it. They use it in it's intended way, using the extension APIs.

code.facebook.com/posts/2186788149...

Thread Thread
 
zeeeds profile image
Yazeed Sabri

I used Scale as a broad term here. I used it because of your last paragraph about using git for small projects only, basically scale includes the number of developers.

Also, I believe that Git hooks can be written in any language you are comfortable with, especially Python.

Yes Facebook hacked Mercurial same way Microsoft is hacking Git with GVFS. It is literally almost the same story but two different systems. Microsoft has been actively contributing to Git along with Google Android project, same way Facebook contributed over 500 patches to Mercurial. GVFS is going to serve a skeleton of the repo similar to what remotefilelog does. This is done to hack both systems into thinking that their files are there locally.

From what I gathered, there is not much of a difference between both systems except for your opinion on them. In other words, Facebook only used Mercurial because "they" deemed it easier to change rather than do what Microsoft or Atom did with Git.

When you say Linus build Git to be used Linus (i.e a truly distributed use), same idea applies to Mercurial as a DVCS since Facebook cannot use it in a central manner without it breaking.

All opinions aside, both systems are good but Git would be a better option to learn and get used to given its bigger user base, this is including all its commands options inconsistencies not an opinion over how human readable they are.

Thread Thread
 
erebos-manannan profile image
Erebos Manannán

I really don't think you understand the concept of "extensions". Try to google for "Git extensions" or "Git extensibility". Just try it, see what you find.

You also don't understand what I mean with Python hooks as you didn't bother to read anything about it. Mercurial can literally call Python functions with clear arguments, where these Python functions have access to the Python API Mercurial provides for them, which is a whole lot more powerful and cross-platform than Git hooks could ever be.

Also you seem to be using the word "hacking" in a rather uncommon way. Creating extensions using the APIs designed for it is not "hacking", it is creating extensions. Most of the great extensions FB made for Mercurial are also available as open source.

And no, you really do not understand the differences between Git and Mercurial. If you only think about "Git vs SVN" or "Mercurial vs SVN" yes, Git and Mercurial are similar. They ARE both DVCS systems based on similar ideas. However, if you think "Git vs Mercurial" there are many very clear differences.

Please do read the material provided before jumping into conclusions of your own. from FB's post: "Our engineers were comfortable with Git and we preferred to stay with a familiar tool, so we took a long, hard look at improving it to work at scale. After much deliberation, we concluded that Git's internals would be difficult to work with for an ambitious scaling project.".

"When you say Linus build Git to be used Linus (i.e a truly distributed use)", no, you are putting words in my mouth and interpreting them to mean something completely different. I did not mean he built it to be truly distributed, I meant he built it for HIMSELF and for his problems, using flags and designs HE was comfortable with, ignoring every other person on the planet, like he tends to do. You CAN use both Git and Mercurial in a centralized manner, just look at GitHub and BitBucket, that however has NOTHING to do with anything I said. The difference I was highlighting was that Mercurial has been built for other people to use, Git has been built for Linus to use, thus Mercurial ended up being a lot more friendly to other people.

Also your last point is such a clear logical fallacy it's laughable. yourlogicalfallacyis.com/bandwagon

No, something being more popular does not make it a better choice. No, you do not have to choose only one country and city to live in, one language to program in, one OS to use, one tool for any job, and most definitely you should not choose these facts based on perceived popularity.

Collapse
 
tmr232 profile image
Tamir Bahar

While git add -p is nice, I believe the use case of "hacking all day on some changes" without committing any code should be avoided when possible.

Commit early and commit often. This gives me a safety net for when I screw something up. At the end of the day, I can git rebase -i start-of-day to clean up the mess. It is a bit less beginner-friendly, but I like it. Then again, I also like splitting commits during a rebase, as it allows me to "test" them individually during the rebase.

Collapse
 
rlipscombe profile image
Roger Lipscombe

git-add -p isn't just for hacking all day without committing any code. I use it for two reasons:

  1. An easy way to review my changes before I add them. I prefer it to git diff. It also allows me to exclude things that I don't want to check in, such as random extra bits of logging that I no longer need.
  2. So that I can split my work into multiple commits. Maybe these two changes don't actually go together, even though they're in the same file? Maybe I can merge this change with an earlier one? In which case, it should go into a separate commit, so that I can git rebase -i with fixup (or squash).

Note that I also have git ci aliased to git commit --verbose, which shows me the diffs in the editor window at commit time.

Collapse
 
tmr232 profile image
Tamir Bahar

I totally agree. git add -p is a wonderful command. But having spent too much time convincing co-workers and friends to commit early, I had to make that comment.

I didn't know about git commit --verbose. That's neat!

As far as rebasing workflows go, I like aliasing git commit --fixup to git fix, so that I can "fix" any relevant commits. The log ends up looking really weird before the rebase, but since I know where each commit should go, it is not an issue.

Thread Thread
 
rlipscombe profile image
Roger Lipscombe

But having spent too much time convincing co-workers and friends to commit early, I had to make that comment.

Sure. I get that. I commit all the time, to the point that my commit comments are generally just things like safsahghhfdddd, and might only be a line or two.

Once I'm starting to be happy with my branch, I go back with git rebase -i (using edit and git reset HEAD~) and git add -p to split the changes into sensible pieces.

Then I reorder the commits and squash/fixup as relevant so that my PR forms a coherent "story".

I frequently find myself pulling smaller refactorings to the start of the branch, so that they can be cherry-picked into master separately from the other changes. This allows other team members to start using those changes immediately, and reduces the number of conflicts.

Collapse
 
blagh profile image
Hannele

Similar to your notes on add . vs add -p, I prefer checkout -p as an alternative to reset --hard - you can make sure you're not throwing something away that you missed during the add process. It works exactly the same way as add -p, you can choose to ignore or throw away piece by piece.

Collapse
 
nipafx profile image
Nicolai Parlog

Two out of three. :) I have git patch and git amend as aliases.

Collapse
 
kellyjandrews profile image
Kelly Andrews

Good idea to add aliases for these.

Collapse
 
dohse profile image
dohse

I believe git reset --hard should be avoided. Better remove unwanted changes changes with git checkout --patch and then use --hard's safe brother git reset --keep.

Collapse
 
simonbaeumer profile image
Simon Bäumer

git bisect is one of the best git commands if you are in trouble :)

Collapse
 
jillesvangurp profile image
Jilles van Gurp

I've been use gitx, which is a pretty version of gitk for ages just for the purpose of reading through commit diffs and writing commit messages, and staging/unstaging files. It supports amend as well, which I use a lot. It's one of those things where having a nice UI to review a lot of diffs quickly is a bonus.

Other than that, my git workflow is entirely command line based. I have two aliases: gitu, and gitc that for git fetch && git pull --rebase and git fetch && git push origin. So with gitx, gitu, and gitc I pretty much have my day to day workflow. I also have a gitl alias tied to a fancy customised git log commandline. Other than that, there's the occasional merge and branch juggling. Maybe an occasional cherry-pick and a reset git --hard in case I have unstaged changes I no longer care about.

Collapse
 
aghost7 profile image
Jonathan Boudreau

In regards to git add -p, I personally just use git diff --staged.

Collapse
 
gonedark profile image
Jason McCreary

That's another good one. I recommend tacking on the -w option to avoid noisy whitespace changes.

The main point is to review your changes before blindly adding them. In the end, whatever command helps you do this is an implementation detail.

Collapse
 
jemagee profile image
John Magee

In my experience, git reset --hard is nice, but it doesn't get the job fully done because I've added new files or folders, so I often have to use git clean in combination with --hard. As a newbie, I was surprised that you had to use a second step to remove any newly created files after the hard reset and have always been puzzled that it's not a one command kind of thing.

Collapse
 
berkus profile image
Berkus Decker

gitup.co

just try it

Collapse
 
mabraham profile image
Mark Abraham

Git stash is a nice way to handle some aspects of 3!