Useful Git Commands and Tips
Git command | Why is it useful? |
---|---|
git merge-base --fork-point master | While on your development branch, when you type this command, it will return the hash of the commit which was the 'fork point' for your development branch, i.e - the commit from which you first created your development branch. |
git config commit.gpgsign true | (Git v2.0+ only) Want to get rid of the hassle of having to remember to sign your commit? This will automatically sign your commits, all the time once you have set up GPG signature for your git repository. |
git config --global credential.helper store | Tired of entering your Github username and password every single time you make a push ? You can use this command after you enter login details once and git will store your credentials for you so that the next time onwards you wouldn't have to enter your login credentials again. |
git mergetool | This will launch a merge tool which you can use to resolve a merge conflict. You can configure your own merge tool (see the next pair of commands) or let git choose a default. |
git config --global merge.tool <program> git config --global mergetool.<program>.path <path/to/program> | These two commands can be used to configure a custom merge tool (like p4merge) which will then be used by default by commands like "git mergetool" and "git difftool" |
git difftool <hash1> <hash2> | This will launch the program configured via merge.tool to perform the git diff operation. |
git add -u | This will stage only the files which were modified or deleted. So, if there are un-tracked files, they won't get added (unlike "git add ." which stages everything) |
Git Tip | Why is it useful? |
---|---|
To ignore whitespace in a Git diff: add ?w=1 to the URL | Find this and other GitHub secrets at https://github.com/blog/967-github-secrets |
Push changes to your fork... often. | If you accidentally trash your local repo, all is not lost. |
Always update your branch via the rebase route. Never use the "Update Branch" button on GitHub. | Whenever your branch is out of date, there's a button that appears on your PR on Github saying "Update Branch" which tempts you to bring your branch up to date with master with the click of a button. Well, please don't give in to the temptation, that button will create a merge commit, which is not signed, both of which are forbidden. So, always update your branch via the painful, rebase route. |
When reviewing new files larger than 1500 lines, put each comment with its line number into one large comment in the pull request. | One developer has been bitten twice by large GitHub reviews. In both cases he was reviewing new files that were larger than 1500 lines. Any comment that he gave after line 1500 showed up at line 1500. |
Working with Github's 2 factor authentication | If doing any remote git operations (like git push) ask you for github authentication and you have 2 factor authentication set up on Github, then you'll have to create a personal access token from Github (https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/) and use that as your password. You can use git's credential helper to store the token so that you don't have to authenticate every time you push something (see the table above for more details on credential helper) |
To recover info that's not accessible via the website | GitHub has a Python API called PyGithub. You can use it to access comments from a PR that is not responsive. The script is attached here. You can pretty much recover everything from a GitHub PR using this API; this script just recovers the comments (along with the code diff hunk): ''' |