How do I clone all remote branches?

First, clone a remote Git repository and cd into it: $ git clone git://example.com/myproject $ cd myproject Next, look at the local branches in your repository: $ git branch * master But there are other branches hiding in your repository! See these using the -a flag: $ git branch -a * master remotes/origin/HEAD remotes/origin/master remotes/origin/v1.0-stable … Read more

Undo a Git merge that hasn’t been pushed yet

With git reflog check which commit is one prior the merge (git reflog will be a better option than git log). Then you can reset it using: git reset –hard commit_sha There’s also another way: git reset –hard HEAD~1 It will get you back 1 commit. Be aware that any modified and uncommitted/unstashed files will … Read more

What’s the difference between tilde(~) and caret(^) in package.json?

See the NPM docs and semver docs: ~version “Approximately equivalent to version”, will update you to all future patch versions, without incrementing the minor version. ~1.2.3 will use releases from 1.2.3 to <1.3.0. ^version “Compatible with version”, will update you to all future minor/patch versions, without incrementing the major version. ^2.3.4 will use releases from … Read more

How do I check if an array includes a value in JavaScript?

Modern browsers have Array#includes, which does exactly that and is widely supported by everyone except IE: console.log([‘joe’, ‘jane’, ‘mary’].includes(‘jane’)); //true You can also use Array#indexOf, which is less direct, but doesn’t require polyfills for outdated browsers. console.log([‘joe’, ‘jane’, ‘mary’].indexOf(‘jane’) >= 0); //true Many frameworks also offer similar methods: jQuery: $.inArray(value, array, [fromIndex]) Underscore.js: _.contains(array, value) … Read more

How do I squash my last N commits together?

You can do this fairly easily without git rebase or git merge –squash. In this example, we’ll squash the last 3 commits. If you want to write the new commit message from scratch, this suffices: git reset –soft HEAD~3 && git commit If you want to start editing the new commit message with a concatenation … Read more

How do I make the first letter of a string uppercase in JavaScript?

The basic solution is: function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } console.log(capitalizeFirstLetter(‘foo’)); // Foo Some other answers modify String.prototype (this answer used to as well), but I would advise against this now due to maintainability (hard to find out where the function is being added to the prototype and could cause conflicts if other … Read more

How do I make a flat list out of a list of lists?

Given a list of lists l, flat_list = [item for sublist in l for item in sublist] which means: flat_list = [] for sublist in l: for item in sublist: flat_list.append(item) is faster than the shortcuts posted so far. (l is the list to flatten.) Here is the corresponding function: def flatten(l): return [item for … Read more

Accessing the index in ‘for’ loops

Use the built-in function enumerate(): for idx, x in enumerate(xs): print(idx, x) It is non-pythonic to manually index via for i in range(len(xs)): x = xs[i] or manually manage an additional state variable. Check out PEP 279 for more.

How do I exit Vim?

Hit the Esc key to enter “Normal mode”. Then you can type : to enter “Command-line mode”. A colon (:) will appear at the bottom of the screen and you can type in one of the following commands. To execute a command, press the Enter key. :q to quit (short for :quit) :q! to quit … Read more