Skip to content

Miscellaneous Git and GitHub Features

This article contains a brief overview of Git and GitHub features that aren't as essential as previous topics, but are still important to know about.
As we find and use similar features, this article will be updated.

Stashing

Consider the following scenario: you've cloned a repo and made some changes, but never made a new branch. Now you want to to pull down the latest changes from an upstream branch, but haven't committed your changed files. What can you do to keep your changes and pull down updated files?

Since you have modified files, Git will give you slightly different warnings depending on what exactly you are trying to do (though they both say the same thing):
1. Swap to different branch:

$ git checkout <other branch> 
error: Your local changes to the following files would be overwritten by checkout:
        <list of all files that would lose changes>
Please commit your changes or stash them before you switch branches.
Aborting

2. Pull in new changes that touch the same files:
$ git pull
< lines for login, and tracking information about the remote >
From https://github.com/<project>
   86e7a5b..efabd76  main       -> origin/main
Updating 86e7a5b..efabd76
error: Your local changes to the following files would be overwritten by merge:
        <file list of all files that would lose changes>
Please commit your changes or stash them before you merge.
Aborting

When you get into this situation, we've already covered two of the three Git-based solutions: Commit + merge and Commit + rebase. Now we'll cover the third option, conveniently pointed out by the errors above: stashing. Stashing allows you to hide changes to all tracked files (including new ones if they have been added with git add) into a "commit" hidden away from this branch. Once stashed, you can safely pull and re-apply the stash (and resolve any potential merge conflicts that cannot be automatically merged by git). Put all together, your commands will look like this:

$ git add <any files not added/tracked>
$ git stash push -m "This is a stash message"
$ git pull
$ git stash pop

Stashes can also be ported to a new branch this way, assuming you haven't committed anything yet:
$ git add <any files not added/tracked>
$ git stash push -m "This is a stash message"
$ git checkout -b <branch_name>
$ git stash pop

One final note, git stash apply can be used to take the most recent stash, apply its changes, and unlike git stash pop, and keep the stash for later.

Using Git Diff

It is sometimes useful to see the exact set of changes that have happened in a file. GitHub provides multiple methods for this, but Git only provides the git diff command for this purpose. The example below shows both addition and deletion of lines in the equations.txt file.

$ git diff equations.txt
diff --git a/equations.txt b/equations.txt
index 8bbe3ad..9097e60 100644
--- a/equations.txt
+++ b/equations.txt
@@ -3,4 +3,6 @@
 4^4=64
 0+0=0
 2-1=1
-a+b+c+d+e=f
+a+b+c+d=f+e
+
+100-2 = 98

By default, git diff shows you changes between the last commit and what the file looks like now (the "a/equations.txt" and "b/equations.txt", respectively). Any line that starts with - has been removed, and anything that starts with a + has been added. This command is flexible and can compare files across commits and branches:
$ git diff main -- equations.txt
diff --git a/equations.txt b/equations.txt
index 93c9c91..c7b6fd2 100644
--- a/equations.txt
+++ b/equations.txt
@@ -1,5 +1,6 @@
-1+1=2
 2*2=4
 4^4=64
 0+0=0
 2-1=1
+a+b+c+d+e

Here the "equations.txt" file on the main branch is compared to its counterpart on the current branch. If you want to see all differences between your current branch and another branch, you can run the command without -- file:
$ git diff main
diff --git a/README.md b/README.md
index 36d1d92..9f5ea06 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
 # example-repo

 We also have issue and pull request templates now too!
-Hello world!
+Hello world this is Derek!

 This is our README.
diff --git a/equations.txt b/equations.txt
index 93c9c91..c7b6fd2 100644
--- a/equations.txt
+++ b/equations.txt
@@ -1,5 +1,6 @@
-1+1=2
 2*2=4
 4^4=64
 0+0=0
 2-1=1
+a+b+c+d+e=f
+mx+b=y

The git diff command is not limited to just branches; you can also compare commits.
For example, to compare the current status of your branch to a specific commit on the same branch, run: git diff <commit hash> where you put the hash of the commit to compare against. For more examples, check out the git diff man pages.