Skip to content

Instantly share code, notes, and snippets.

@akashnimare
Last active March 16, 2020 10:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akashnimare/e5b308a0df03c1ad3eb9752390fc49f4 to your computer and use it in GitHub Desktop.
Save akashnimare/e5b308a0df03c1ad3eb9752390fc49f4 to your computer and use it in GitHub Desktop.
Git tips for first time contributors

Reword a old commit message

https://help.github.com/articles/changing-a-commit-message/

* git checkout your-branch
* git rebase -i HEAD~N (N is the number of commits)
* use **reword**
* Follow the steps
* git push --force

Update a PR with the upstream

git checkout master
git fetch upstream
git rebase upstream master
git checkout <your-branch>
git rebase -i master

Update your fork repo with upstream

git checkout your-branch
git rebase -i HEAD~N
git fetch upstream
git rebase upstream/master
git push --force

Cherry-pick a commit from another branch

Git cherry-pick allows you to merge a single commit from one branch into another. To use the cherry-pick command follow these steps:

  • Check out the branch into which you want to merge the commit. (E.g.: git checkout master)
  • Identify the commit hash through your favorite method. You can use git log, a GUI tool such as sourcetree or tower, or if you use GitHub or BitBucket you can use their interface. In SWS we use GitHub, so I tend to use that method often. With GitHub you can find the commit hash on the commit list page or on the individual commit page itself. See the screenshots below. Pick'em! - git cherry-pick 9638d99c89a92350ad2f205f47af24b81ac39a64
  • If there is a merge conflict you will have to resolve that with your favorite merge tool, but otherwise you have now successfully pulled in the work from the other branch.

Squash All Commits Related to a Single Issue into a Single Commit

If there are 5 commits in a PR and you want to squash them into one, follow this steps -

  • git rebase -i HEAD~5
  • In the text editor that comes up, replace the words "pick" with "squash" next to the commits you want to squash into the commit before it.
  • Save and close the editor
  • git will combine the "squash"'ed commits with the one before it. Git will then give you the opportunity to change your commit message.
  • Use git show to check if everything is fine
  • If you've already pushed commits to GitHub, and then squash them locally, you will have to force the push to your branch.
  • git push -f or git push origin branch-name --force

Edit your last commit message -

* git commit --amend
* Change the commit message
* git push -f

Checking a PR locally -

There are couple of options to achieve this -

  • Save following function in your bashrc/zshrc and run pullpr 123
function pullpr() {
	git fetch origin pull/$1/head:$1;
	git checkout $1;
}
  • Run following bash script -
#!/bin/bash
set -e
set -x
if ! git diff-index --quiet HEAD; then
    set +x
    echo "There are uncommitted changes:"
    git status --short
    echo "Doing nothing to avoid losing your work."
    exit 1
fi
request_id="$1"
remote=${2:-"origin"}
git fetch "$remote" "pull/$request_id/head"
git checkout -B "review-original-${request_id}"
git reset --hard FETCH_HEAD
  • Easiest way is to use hub -
hub checkout https://github.com/zulip/zulip-electron/pull/353

Delete a Git branch both locally and remotely?

  • remove a local branch from your machine:
git branch -d {the_local_branch} (use -D instead to force deleting the branch without checking merged status)
  • To remove a remote branch from the server:
git push origin --delete {the_remote_branch}
  • You can also make a function in your bashrc/zshrc:
function rmbranch() {
	git push origin --delete $1;
	git branch -D $1;
}

then use it like rmbranch dev.

Push commit from one branch to another branch

  • So for an example if you want to push branch1 to branch2 then
From branch1
git push origin branch1:branch2 -f
or
git push origin push_from_branch:push_to_this_branch -f

Undoing a last commit

git reset --hard HEAD~1 (changes to last commit will be removed )
or
git reset --soft HEAD~1 (changes to last commit will be available as uncommited local modifications)

Delete all the git stash

git stash clear

Checking someone's fork

git remote add username https://github.com/username/repo.git
git fetch username branch
git checkout username/branch

Exclude files from git diff

This is pretty useful if you just want to exclude some files from git diff.

git diff -- . ':(exclude)*-lock.json'

Show commits that have touched specific text in a file

git log -S "no-select" --stat -p -- path-of-the-file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment