Using Git to Open Modified or Changed Files Since Previous Commit

Nafeu Nasir
The Startup
Published in
4 min readSep 21, 2020
Icon by https://thenounproject.com/paisurangkana/

If you want to skip to the goods, this is the command that we will be working up to:

Open changed or modified files in editor

Simplifying The Burden of Retracing Steps

Frequent context switching between tasks, returning to yesterday’s work or thinking about how to continue an old project that hasn’t been touched in a while can make any energetic coder feel drained in less time than expected. When you’re in this spot, your tasks may include:

  1. Remembering what problem(s) you were working on
  2. Prioritizing and deciding what the most responsible steps would be to take next in progressing towards your project’s goal
  3. Re-orienting yourself with the code you were in the middle of writing last time (whether you ended your coding session off with a clean repository or not)

For 1 and 2 you might have notes, checklists or project management tools to refer to, however, when it comes to number 3, you might end up just switching through tabs, looking for placeholder comments left in your code prior, looking for hints inside an editor that was left open, or running a sequence of git commands in your project directory to recall recent changes. Once you’ve done all of that, you may have already put yourself through a fair bit of unnecessary decision making.

How can we leverage git and some shell commands to make 3 less cumbersome? Let’s look at the following:

1. See Changed Files Since Last Commit

git diff --name-only HEAD~ HEAD

This command allows us to list what files have changed since the last commit. Which is a wonderful thing to integrate into your workflow when returning to a project. It takes advantage of git diff. We can test it out with an example git project:

mkdir example-repo
cd example-repo
git init
echo "foo" > exampleA.txt
echo "bar" > exampleB.txt
git add --all
git commit -m "First commit."

Here we have created a new directory called example-repo, instantiated a git repository, created two new files called exampleA.txt and exampleB.txt, staged and commited those files with our first commit. To verify that we’ve made our commit, run git —-no-pager log --online --decorate and you should see the following (with a different unique hash at the front of course):

$ git --no-pager log --oneline --decorate
b3a4096 (HEAD -> master) First commit.

Now let’s modify a file and create a second commit. Let’s change exampleA.txt to contain the text “baz”:

echo "baz" > exampleA.txt
git add --all
git commit -m "Second commit."

We should see both our commits in our logs:

$ git --no-pager log --oneline --decorate
9d17441 (HEAD -> master) Second commit.
f5c2b44 First commit.

Now lets see if our diff command works, run:

git diff --name-only HEAD~ HEAD

Since only exampleA.txt changed from our first commit, you should see:

git diff — name-only HEAD~ HEAD

2. See Modified Files In Current Directory

git status --porcelain | awk '{print $2}'

This command allows us to list files that have been modified (whether they are staged or not staged). Why don’t we modify the file exampleB.txt to say “hello world”:

echo "hello world" > exampleB.txt

Now let’s run git status --porcelain | awk '{print $2}' and it should just print exampleB.txt, to show that staged files will also be listed, lets stage exampleB.txt with:

git add exampleB.txt

And when we run git status --porcelain | awk '{print $2}' we should still see exampleB.txt

3. Setting A Default Editor With An Environment Variable

*Note: If you have one set already, feel free to skip to step 4 below.

If you don’t know what editor command you have set in your environment under the EDITOR variable, you can check by running:

echo $EDITOR

For instance, I have mine set to subl which is linked to Sublime Text 3. You can also set an editor for your current session with export EDITOR=editor_command_name where editor_command_name would be your editor of choice. Alternatively (and the preferred way to do it) would be to export the EDITOR environment variable in your .bashrc file OR .zshrc file (if you are using zsh).

If you are using your .bashrc then this can be done with:

echo 'export EDITOR=editor_command_name' >> ~/.bashrc

and if you are using .zshrc then:

echo 'export EDITOR=editor_command_name' >> ~/.zshrc

Then reset your terminal session so that the variable is instantiated.

4. Combining Both Commands And Opening Files In Your Editor

Using our EDITOR environment variable, echo, and our two commands discussed in steps 1 and 2, we can do the following:

Open changed or modified files in editor

To test this out, use our existing example project, or open up an old project that uses git and run the command. All your changed and modified files should pop up in your editor.

To save even more time, create an alias for this command called ocm and append it to your .bashrc or .zshrc :

alias ocm="$EDITOR `echo $(git diff --name-only HEAD~ HEAD) $(git status --porcelain | awk '{print $2}')`"

Now you can just run ocm and you are right where you left off.

Happy Coding!

--

--

The Startup
The Startup

Published in The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

Nafeu Nasir
Nafeu Nasir

Written by Nafeu Nasir

is a musician and full-stack web developer from Toronto, Canada who studied Computer Science and Linguistics at the University of Toronto.

No responses yet