13 ms·
99% of the Git commands you'll need at work, demonstrated in a single script
- carodgers 7y agoI got a little tired of running to Google every time I needed a non-trivial git command, so I set out to identify all (and only) the git commands I require for my day-to-day and capture them in one place. The result is this script. It can be run fully on your local machine, no remote accounts required. (But it still shows how to use remote repos!) I'd also be interested to hear what git commands others use frequently (that don't involve a GUI). I'm curious to know which workloads might require some of git's more advanced features on a regular basis.
- katet 7y agoA couple of random addendums (although these aren't necessarily what you'd use day to day) git diff --color-moved (seriously, read through the man page for git diff sometime) git log -p --all -- <path/to/file> (I use this with multiple remotes, so I can track changes in my fork and in upstream when looking for context around a file) git cherry-pick -x (for automatically adding a reference to the original commit) A couple more, but I think they're really quite specific to my personal workflow and responsibilities
- carodgers 7y agogit log -p --all -- <path/to/file> I like this one. I'll remember it. Thanks for sharing!
- Myrmornis 7y ago(shameless plug): https://github.com/dandavison/delta https://github.com/dandavison/delta is a syntax-highlighting pager for git that adds syntax highlighting and within-line edit detection to all diffs displayed by git. I agree about `git log -p`. A good trick with that is to use it as a quick way to search for a commit that includes a certain string (since the pager is less, one can search back in time with `/`). Same trick for `git reflog -p`. `git stash show -p` is another less-known diff command (look at a stash without applying it).
- adrianN 7y agoI use git log -S regularly https://www.git-scm.com/docs/git-log#Documentation/git-log.txt--Sltstringgt https://www.git-scm.com/docs/git-log#Documentation/git-log.t...
- coldpie 7y ago> I got a little tired of running to Google every time I needed a non-trivial git command Have you read through the gittutorial, gittutorial-2, and especially the gitcore-tutorial manpages[1]? Reading those guides and actually following the steps and examining the results on disk was extremely helpful for me in understanding how Git actually works. Once you've got that understanding, the Git UI seems less arbitrary. You can start to see how it was put together over the years, which should help you use the tool more intuitively and effectively. > I'm curious to know which workloads might require some of git's more advanced features on a regular basis. I work on a very large and very old (mid-90s) project, so history-diving and diffing branches is very useful for me. I use all of git-blame, git-show, and git-diff to examine history to learn how the code came to be as it is. Even git-cat-file comes up occasionally when I just want to look at some old object I happen to have a hash for and don't want to look up properly through the porcelain. We also maintain a bunch of branches and backport modern commits onto legacy branches. git-rebase can help for this, but more often I find myself doing it manually (well, with a bash script) with git-cherry-pick, as it's easier to work with commits without being in the middle of a rebase operation. Knowing git-log's many options to massage history into a useful form for my work comes in very handy here, too. More details on the rebasing procedure I use: https://github.com/ValveSoftware/Proton/blob/proton_4.11/docs/REBASING_TIPS.md https://github.com/ValveSoftware/Proton/blob/proton_4.11/doc... Here's some of the tooling I use often. For Git stuff, see pick_commits and gitconfig https://gitlab.com/mywinetools/mywinetools/ https://gitlab.com/mywinetools/mywinetools/ [1] I know, I know, no one likes studying and learning how to use a tool. But sometimes you gotta if you want to use the tool effectively.
- taneq 7y agoHow much required reading should a version control tool really need before you can use it competently?
- coldpie 7y agoDepends on how many features you want it to have, and what level of control over it you want to have, I suppose. It's hardly unique in being a tool one needs training to master. How many programming language books are there? Database theory books? Adobe Photoshop guides? Safety instruction guides for power tools? Best practices for building construction... You have to learn a thing to use a thing. Git is no different.
- panpanna 7y agoI feel the list of useful for commands should be much shorter: git add/rm/commit/status/push git branch/checkout/merge/rebase git tag/push --tag Anything else should be handled by policies. If you get bad PR, reject it instead of trying to fix it using some complex sequence of git commands.
- edflsafoiewq 7y agoI'd add stash and log.
- SamBam 7y agoI personally find `cherrypick` to be occasionally helpful, as a way to jump out of trying to be too git-clever, and simply starting a new branch and pulling in the useful parts of another branch. That's probably anathema to some users, though.
- wwright 7y agoHow is the PR author supposed to fix it without complex Git commands? Error prone copy and paste? I _really_ love working with Git, but in my experience engineers unfortunately do need to tap into the long tail of “advanced” commands fairly often. At least often enough that they will quickly be frustrated by it. (I’m currently helping move a bunch of our engineers from SVN to Git, and while I believe the better ecosystem will be worth it, it does break my heart every time someone has pointless trouble simply because of Git’s CLI.)
- dspillett 7y ago> How is the PR author supposed to fix it without complex Git commands? Error prone copy and paste? Reclone from master (or the active branch if something else) to another directory, merge in their final changes, manually if needed, properly test the result, and sent a new PR with the cleaned up version? (caveat: I've not used git in anger, there may be more to it than that, but I can't imagine much more) I suppose who is responsible for dealing with cleaning up updates before merging them into the main project depends upon who wants it merged most: the PR submitter because they want a fix/change/other in upstream so they don't have to maintain their own fork, or the project maintainer because the update is more generally useful to other users or more specifically useful to the maintainer. If you submit a badly arranged PR or other patch you are creating extra work for the project maintainer(s). Depending on the project and maintainer(s) this may be acceptable or, equally rightly, it may not.
- HocusLocus 7y agoRan this script, it uploaded my whole network to Github as an open source project and sent a broadcast notification to Krebs On Security.
- hnhg 7y agoIs that what you wanted?
- aequitas 7y agoTry Gitless[0]. It's an alternative 'UI' to Git which is kind of build top down instead of bottom up. The commands are much higher in the abstraction tree and clear single operations and dropping some concepts like the staging area. It operates on the Git database the same way 'normal' Git does so you can always fall back. [0] https://gitless.com/ https://gitless.com/
- sidpatil 7y ago> It's an alternative 'UI' to Git Also known as a porcelain, as opposed to the low-level commands known as the plumbing.
- etbebl 7y agoMy understanding is that in git-land, "porcelain" and "plumbing" usually refer to a specific dichotomy of high-level vs. low-level git commands, rather than further abstractions over the commands such as a GUI.
- Munksgaard 7y agoI know this has been mentioned a million times on here, but for the off chance that someone hasn't heard about it before I'm going to completely disregard that and say that Magit for Emacs is an incredibly helpful tool for interfacing with git repositories that I couldn't imagine working without it. It's by far the best interface to a version control system that I've ever used. It has helped me understand how git works behind the scenes, and allows me to effortlessly work with branching, stashes, merging, rebasing, remotes and much more, without remembering a bunch of arcane git commands.
- hwj 7y agoMagit sounds great. If I only could use it without remembering a bunch of arcane emacs commands.
- shpx 7y agoIf you've memorized the arcane vim commands, try Spacemacs or Doom Emacs. http://spacemacs.org/ http://spacemacs.org/ https://github.com/hlissner/doom-emacs https://github.com/hlissner/doom-emacs In magit in those Emacs distributions, the question mark brings up a list of available commands and if you type the first letter of a command with multiple letters, it'll show you the names of the actions that start with that key.
- phalangion 7y agoSpacemacs makes emacs commands both less arcane and more discoverable. Give it a shot.
- giancarlostoro 7y agoSpacemacs is my favorite way to use Emacs, I leave the Emacs keybindings for it, but you can still use some of the ergonomic shortcuts from it. I have never used Magit however, I just prefer using a GUI: Sourcetree on Windows / Mac is A+, Gitkraken is fine, but idk if I can justify $60 a year, and SublimeMerge doesn't "feel" ready for me just yet, but if it ever does I may consider it. The main way I do things overall is IntelliJ and Co's built-in git utilities. If something gets complex I switch out and into GitKraken or Sourcetree.
- psv1 7y agoCan anyone point to a good guide and/or tool for resolving merge conflicts?
- Asooka 7y agoThe ones listed as options for git mergetool are all good. Personally I use kdiff3, it has a simple interface for choosing conflict resolutions and can often resolve some of them on its own.
- adrianN 7y agoI liked this http://vimcasts.org/episodes/fugitive-vim-resolving-merge-conflicts-with-vimdiff/ http://vimcasts.org/episodes/fugitive-vim-resolving-merge-co...
- coldpie 7y agoSet merge.conflictstyle=diff3!!!! The default merge style is utterly useless, I have no idea why it is the default. diff3 gives you three pieces of information: the status before at the common ancestor; the status on the current branch; and the status on the to-be-merged branch. You can then understand what changes were made on each branch, and decide how to merge them intelligently. Without that common ancestor, it's way way harder to understand what you're looking at.
- dahart 7y agoI’m a fan of using Perforce merge (p4merge) with git. It’s free to download and use, and easy to integrate with git. I remember thinking the UI was weird the first time I used it, but since then I’ve tried a bunch of merge tools and haven’t found something I like more. It shows common ancestor by default, and lets you hand-edit the merge section, which not all merge tools do.
- darepublic 7y agomany modern IDEs will help with this, ie. visual studio code. It will highlight and name the conflicting code by branch. When the conflict occurs you can do git status and the highlighted files are those with a conflict -- at that point you can just navigate there in the code editor of your choice and look for the <<<< blocks. After you get those files into the state you would like then you run 'git commit' to resolve.
- vfc1 7y agoThanks for sharing, but I feel like it's more like 99.999%. The essential commands for development are really a lot less than this. Maybe it's a list for release managers / sys admins? Committing, pushing, handling local branches, updating, merging, rebasing, cherry-picking, squashing and stashing is almost all that everyone uses, and several of those not even on a daily basis.
- stunt 7y agoDevelopers having hard time to master GIT after so many years is just a good argument to "Why we need GO more than Scala" and a very disappointing reality at the same time.
- JamesQuigley 7y agoI'm personally a huge fan of https://www.gitkraken.com/ https://www.gitkraken.com/ (I also used to work there.) Don't have to memorize git cli commands and get beautiful visuals on top of that.
- rinchik 7y agough i hate GUIs, for me those are a middle man. With cli, i can "feel" git. with GUI i'm just telling a 3rd party to talk with git for me. That 3rd party can also screw up some communication, just like in telephone game.
- wruza 7y agoThey also fail to at least show you the conversation before it starts, so you could align your ideas on what can go wrong. I had to ditch tortoise<vcsname> for this exact reason in our entire division, except for simple initial checkouts. That damn thing always tried to append some “--qwerty --asdf” to every innocent command it could be asked to perform.
- jackcodes 7y agoI do appreciate that this is probably a cracking tool for beginners and juniors. If it was skilling someone up to git using this tool or not at all, damn right I’m choosing this. But I can’t bring myself to run a web browser to run git commands either. The git binary on my system comes out at 2.5Mb and the MacOS .app bundle for this is 346.5Mb.
- JamesQuigley 7y agoYeah pretty much every app I run nowadays is Electron. Don't love that, but they bring me enough value that it's 100% worth it for me. I'm with the other commenters that I don't like having to google a git command every time I have to do something more complex than add/commit. GK is perfect in that sense
- mroche 7y agoIt’s been a long span of time since I’ve used GitKraken or Atlassian Sourcetree, but GK had some serious performance issues with larger repos in my experience. Very clean app, though, and I did enjoy it. Right now, Im partial to Sublime Merge[0] as it’s fast, has a clean UI for diffing, shares many similarities with Sublime, and also shows the use what git commands they’re running by hovering. It can be a very effective tool for learning git. It has the same evaluation structure as Sublime, so that’s a bonus as well. [0] https://www.sublimemerge.com/ https://www.sublimemerge.com/
- pastrami_panda 7y ago'git add -i' is my new favorite. When you want to stage certain files but don't want to type out the paths over and over again to deeply nested files
- niark 7y agoAfter years of wanderings, I've settled with Fork which is a a delightful GUI. Polished, feature-rich, and still improving on a regular basis. Kudos to the authors! [0] https://git-fork.com/ https://git-fork.com/
- therealdrag0 7y agoI switched from SourceTree a few months ago after seeing Fork on HN. I couldn't be happier with it! Love seeing high-quality software developed by just 2 people.
- sixstringtheory 7y agoSame, I spend about 50% of my time in git on the command line, the other 50% in Fork. I've used SourceTree -> Tower -> Fork and I'm very happy with where I'm at now.
- liquidify 7y agohmm why no linux?
- johncoltrane 7y agoHe is a MacOS developer (Swift, Cocoa), she is a Windows developer (.NET, WPF).
- jeena 7y agoThe example commit messages are triggering me.
- thrower123 7y agoThis kind of thing is why I'm very glad that the Team Explorer git plugin in Visual Studio has become decent enough that I don't have to drop into the cmd line, pretty much ever.
- anreekoh 7y agoIf you want git on the command-line, but don’t want to remember commands, try lazygit[0], it’s changed what I’m comfortable doing with git because it can tell me at any given moment what I can do. [0]: https://github.com/jesseduffield/lazygit https://github.com/jesseduffield/lazygit
- trevor-e 7y agoMaybe my workflow is a lot easier than others, but I work on a team of 10+ engineers and my Git usage is dead simple. I do everything from a GUI (Fork for macOS) and very rarely have to deal with any complicated issues that require a terminal. - Always pull w/ rebase for the current branch. - Always merge other branches into your current branch, eg master -> feature. - Always stage individual chunks of code one at a time to make sure I'm committing the right stuff. - Always squash feature branches into a single commit when merging back. - Stash changes if needed when switching branches. - Cherry-pick one-off commits if needed. - Append a previous commit that I haven't pushed yet if I happened to forget something. - For complicated merge conflicts I switch to Visual Studio Code which also has a great GUI. I think this covers 99% of the stuff I encounter in my day-to-day.
- ssully 7y agoI am in a similar boat, except I just Githubs desktop client. In the past 4 years, I think I have had to use the command line under 10 times.
- doubleunplussed 7y agoAm I missing something, or is it impossible to, y'know, check out a previous revision using github desktop? Create a branch at a previous revision? Reset the branch to some previous commit? Any of these things? It seems to have next to zero functionality. What is the point of it? What you you use it for, other than commit, push?
- iamsb 7y agoYou can do most of the things you have mentioned using atlassians desktop client https://www.sourcetreeapp.com/ https://www.sourcetreeapp.com/
- doubleunplussed 7y agoI know that, I'm just wondering what the point of GitHub Desktop is. I use Sublime Merge, but no Git GUI I've used comes close to tortoisehg for mercurial. Tortoisehg is actually superior to and a comprehensive replacement for the command line. I wish something like it existed for git, if not for me then for the newbie-ish people I need to instruct on project workflows.
- lbenes 7y agoWhen I want to get back to a pristine state, I prefer $ git reset --hard origin/master over the suggested $ git reset --hard HEAD If you use git and don't know the difference, read this: https://stackoverflow.com/questions/8196544/what-are-the-git-concepts-of-head-master-origin https://stackoverflow.com/questions/8196544/what-are-the-git...
- karmakaze 7y agogit reflog # for those times you get lost and want to go back
- shaneos 7y agoI took all my most common Git commands and made them far easier to type - here's the script in case it's helpful to you https://github.com/shaneosullivan/git-shortcuts https://github.com/shaneosullivan/git-shortcuts
- RealityVoid 7y agoI just found out today about git worktree[1], and I think it's a very powerful feature more people should know about, but it seems used less than it could. Basically, you can do a working copy of the git environment without pulling the repo again, so you have only one .git folder. A godsend if you, like me, work on gigantic repos that build unbearably slowly and want to be able to use multiple instances of the same repo at the same time. [1] https://git-scm.com/docs/git-worktree https://git-scm.com/docs/git-worktree
- jomar 7y agoCompared to the previous technique of creating a new separate local clone for unrelated simultaneous work on a project, it's also glorious when it comes time to clean up and delete no-longer-needed extra working directories. After `make clean` and `git status --ignored` shows nothing worth preserving, you can delete the worktree (via `git worktree remove`) with impunity. No more paranoid checking that there is no valuable work stashed or hiding in other branches before typing `rm -rf`, as those are shared with the main worktree so won't be lost.
- jnurmine 7y agoSilly little alias, "git top". [alias] # top 10 top = !sh -c 'git log --oneline|head -10' It shows the top 10 commits. I use it several times every day, to orient myself while flipping between multiple repositories and branches.
- ShteiLoups 7y agoI used to live and die by the command line. Then I met sublime-merge. And fell in love. It is the only Gui I find more efficient and beautiful than my terminal. I highly reccomend it.
- B1FF_PSUVM 7y ago#CodeSmell (i.e. that this is necessary speaks volumes)
- darnir 7y agoAwesome! Now show me how this is done in Mercurial
- wyclif 7y agoOne tip I give people who feel overwhelmed by Git is that unless you are an edge case, you can probably do most of what you need to get done with these six commands: branch checkout add commit pull push Reading and understanding the documentation for those six core commands isn't a big investment, and it will pay off if you're doing software or documentation development.
- KuhlMensch 7y agoI'd say I literally use pickaxe (-S) and the -G one a few times a week. Often to answer questions I hear around the office. If you are not familiar with it, I think its an amazing. Err... I guess...check it: http://www.philandstuff.com/2014/02/09/git-pickaxe.html http://www.philandstuff.com/2014/02/09/git-pickaxe.html Also I do allot of git reset --soft HEAD; (pick-hunks-with-GUI); git stash -u -k; run-test.sh; git commit -m "blah"`
- hansdieter1337 7y ago"UNEXP_CONT=$(ls sandbox/..." this will ignore hidden files. So does the "rm -rf"