Shortcut to git commit and push in Visual Studio Code

3 minute read

Image source: git-scm.com and GitHub

Introduction

In a previous post, we covered how to use a custom plugin in Jekyll and deploy our site on GitHub using the source and master branches trick:

  • switch to source -> commit the changes and push -> build the site -> switch to master -> commit the changes in _site and push -> switch back to source.

Here I will share how to use VSCode to simplify the workflow, by using VSCode task to execute a customized shell script, which does commit/push and a few other things.

  • Caveat: this post assumes that our readers are as lazy as myself and do not want to do repetitive things by hand.

Streamline the workflow in VS Code

After the first time done working building the site and syncing the changes to the master, we can streamline this workflow in VSCode by pressing a single key combo…using a task hack.

If we make sure everything is good locally by doing bundle exec jekyll serve, we can deploy the site by the following.

Create a shell script with all the git commands

First we can create a shell script update.sh in the workspace root, and add executable rights to it chmod +x update.sh. This script does three things:

  1. (Assume we are done writing in source branch) Commit and push all changes in source branch.
  2. Build site with the production JEKYLL_ENV.
  3. Sync the site to master branch (and come back to source).
#!/bin/bash
git add . && \
git commit -m "Update pages in source" --allow-empty && \
git push origin source && \
JEKYLL_ENV=production bundle exec jekyll build && \
git checkout master && \
cp -r _site/* . && rm -rf _site/ && touch .nojekyll && \
git add . && \
git commit -m "Update build site" && \
git push --all origin && \
git checkout source

Set up a VSCode task to execute the shell script

Next, we set up a VSCode task in the current workspace Ctrl+ Shift+ B, add the following task in tasks.json in the current workspace:

"tasks": [
  {
      "label": "update site build and sync to master",
      "type": "shell",
      "command": "./update.sh",
      "group": "build"
  }
]

Next time upon invoking the task again by pressing Ctrl+ Shift+ B (or + Shift+ B on Mac), just toggle the option to the “update site build and sync to master” and press Enter, the site will be good to go!

Fetch and pull from all branches

Another example is that we just wanna fetch from all remotes and pull the changes to local branches. Following this StackOverflow answer, we can do something similar by adding a fetch_pull_all.sh and update the task.json as follows.

#!/bin/bash
`git branch -r`; do git branch --track ${remote#origin/} $remote; done
git fetch --all
git pull --all
{
    // https://stackoverflow.com/questions/10312521/how-to-fetch-all-git-branches
    // pull for all local branch from tracked remotes
    "version": "2.0.0",
    "tasks": [
        {
            "label": "update site build and sync to master",
            "type": "shell",
            "command": "./update.sh",
            "group": "build"
        },
        {
            "label": "fetch and pull all that track remote",
            "type": "shell",
            "command": "./fetch_pull_all.sh",
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

Last remark

Of course, this is vastly an under-usage of the powerful VSCode tasks. In my daily work, VSCode tasks speed up the grid search in deep learning model structures (not just hyperparams) prototyping greatly.

Comments