Useful Git Commands and Tips

Git commandWhy is it useful?
git merge-base --fork-point masterWhile 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 storeTired 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 mergetoolThis 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 -uThis 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 TipWhy 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 authenticationIf 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):


'''
Created on Jan 9, 2018

@author: ragrawal
'''

from github import Github

# Add your pull request number here
PR = -1

# Add your Github Access token here
TOKEN = ""

# Login to Github
g = Github(TOKEN)
if g is None:
raise Exception("Github token invalid, couldn't login")

# Fetch your repositories & find pbspro
pbspro_repo = None
for repo in g.get_user().get_repos():
if repo.name == "pbspro":
pbspro_repo = repo

if pbspro_repo is None:
raise Exception("PBSPro repo not found")

# Get your PR & and associated comments
pr_object = pbspro_repo.get_pull(PR)
if pr_object is None:
raise Exception("Pull Request " + str(PR) + " not found")
comments = pr_object.get_comments()
if comments is None:
print "No comments found on PR " + str(PR) + ", exiting"
exit(0)

# Write the comments & related details in a file called 'pr_<pr number>_review_comments'
filename = "pr_" + str(PR) + "_review_comments"
with open(filename, "w") as fd:
for comment in comments:
fd.write(comment.commit_id + "\n" + str(comment.created_at))
fd.write("\n" + comment.user.name + "\n")
fd.write("FILENAME: " + comment.path + ": " + str(comment.position) + "\n")
fd.write("COMMENT:\n" + comment.body + "\n")
fd.write("DIFF: \n" + comment.diff_hunk + "\n\n")
fd.write("==================\n\n")

print "PR comments written in " + filename





OSS Site Map

Developer Guide Pages