Skip to content

Ferdinand Agyei-Yeboah

Handy Git Commands

October 27, 2020

Note: <> represents variable info in the post and should be replaced with your own values.

Creating Commits

Create normal commit with message

git commit -m "updated files"

Create empty commit. You might use this to trigger a CI/CD build

git commit --allow-empty -m "Empty commit to trigger build"

Amend last commit. Assuming has not been pushed to remote, else will have to force push

git commit --amend

Stashing Commits

Stashing is good for when you want to store away changes that are not ready to be committed but you don’t want to lose. For example, you could be trying out multiple ways to solve a solution and not sure which one you want to pursue, you could stash each solution while working through them and commit the one you finally choose to go with.

Stash some draft changes

git stash save "stash message"

List your stashes

git stash list

Check file changes in a stash

git stash show@{stash-number}

Apply the changes in the stash

git stash apply stash@{stash-number}

Checking Out

Branches

git checkout <branch-name>

Tags

To checkout a tag into a branch, make sure to fetch all tags and then checkout the tag into the branch.

git fetch --tags
git checkout tags/<tag-name> -b <new-branch-name>

Forks

To checkout branches of a fork, add the fork as a remote, then checkout the desired branch.

git remote add <fork-name> <fork-url.git>
git fetch <fork-name>
git checkout -b <local-branch-name> @<fork-name>/<branch-name> # git checkout -b sams-branch @sam/branch

PRs

To create a branch from a remote PR to work on locally use

git fetch <remote> pull/<id>/head:<local-branch-name> # git fetch origin pull/1/head:fix-the-first-pr

Now can switch to that local branch

git checkout <local-branch-name> # git checkout fix-the-first-pr

Squashing Commits

Using soft reset

To squash the last X consecutive commits, you can reset the HEAD back and push a new commit.

git reset --soft HEAD~<number> # git reset --soft HEAD~2 -> unstage last 2 commits
git commit -m "New commit"

Note that you will have to do a force push (git push <remote> <branch> --force) to rewrite history if some of the commits have already been pushed upstream.

Using rebase

Although longer, the interactive rebase command can also be used to squash commits with more flexibility.

git rebase -i HEAD~<number> # git rebase -i HEAD~4 -> interactively operate on last 4 commits (not including current, use --root for that)

This will open up the rebase tool in your default text editor.

Rebase Prompt

This shows the commits to be operated on. Now you can “pick” one commit and “squash” the others into it.

Rebase Prompt 2

Save and quit the file to execute the squash. The next prompt will allow you to edit the commit message if you choose.

Git Merging

While on a local branch to recieve a merge from another local branch

git merge <local-branch-to-pull-in>

To pull/merge a remote branch into your local branch

git pull <remote>/<remote-branch-to-pull-in> # git pull origin/master

Check if Merge Conflicts Resolved

git diff --check

Software Engineering Tutorials & Best Practices