Using Git: Command Line and VS Code
Setting up Git, the everyday commands, reading history and undoing changes, and Git in VS Code's Source Control view.
Ways to use Git
You can use Git by typing commands in a terminal, through the Source Control view built into editors such as VS Code, or with a graphical app. They all do the same thing to the same repository, so you can mix them freely: many DBAs commit from VS Code and reach for the command line when they need something more advanced.
One-time setup
After installing Git, tell it who you are, since every commit records it:
$ git config --global user.name "Kofi Mensah"
$ git config --global user.email "kofi@acme.com"
# call the first branch of new repositories main, like GitHub and GitLab do
$ git config --global init.defaultBranch main
To push to GitHub or GitLab, you also need to prove who you are. Two common ways: an SSH key added to your account, or signing in over HTTPS through a credential helper, which VS Code and Git for Windows set up for you. Passwords alone no longer work on the major platforms.
The command line
The Git CLI (command-line interface) is the full toolset, and the same on Linux, macOS, and Windows. These commands cover almost everything a DBA does:
| Task | Command |
|---|---|
| Get a repository | git clone <url>, or git init for a new one |
| See what's changed | git status, git diff |
| Stage and commit | git add <file>, git commit -m "message" |
| Work on a branch | git switch -c <branch>, git switch main |
| Share and update | git push, git pull, git fetch |
| Combine work | git merge <branch> |
| Mark a release | git tag v1.2.0, then git push --tags |
Reading history
History is where Git really pays off for a DBA. When a script misbehaves, these commands show exactly what changed, when, and who to ask:
# the history as a compact graph of branches and merges
$ git log --oneline --graph
# every commit that touched one file
$ git log --oneline -- sql/roles.sql
a81d4e0 Grant finance_ro read access to orders
3f2a1c9 Add read-only role for sales reporting
# who last changed each line of a file, and in which commit
$ git blame sql/roles.sql
# the full details of one commit
$ git show a81d4e0
Undoing things
| Situation | Command |
|---|---|
| Throw away uncommitted edits to a file | git restore <file> |
| Unstage a file, keeping the edits | git restore --staged <file> |
| Fix the message of your last commit, before pushing | git commit --amend |
| Undo a commit that's already been pushed | git revert <commit>, which adds a new commit that reverses it |
Git in VS Code
Visual Studio Code, Microsoft's free editor, has Git built in. Its Source Control view, opened from the side bar or with Ctrl+Shift+G, does the everyday work with clicks instead of commands:
- Changes list: every changed file, marked M (modified), U (untracked, new), or D (deleted). Click a file to see a side-by-side diff.
- Stage: the + button next to a file stages it, the same as
git add. You can even stage single lines from the diff. - Commit: type a message and click Commit.
- Branches: the current branch is shown in the status bar at the bottom; click it to switch or create one.
- Sync: one button to pull and push.
- Merge conflicts: shown with Accept Current, Accept Incoming, and Accept Both buttons, or in a three-way merge editor.
- Extensions: GitLens shows who changed each line as you read it, and GitHub Pull Requests (or the GitLab Workflow extension) lets you review and comment on pull requests without leaving the editor.
VS Code also has a built-in terminal (Ctrl+`), so you can switch to Git commands at any moment. It's a good home for DBA work generally, with extensions for SQL, YAML, Terraform, and Ansible, and Oracle's own SQL Developer extension for VS Code.
Other Git tools
- The platform's web page, on GitHub or GitLab, for browsing files and history, small edits, and reviewing pull requests.
- Desktop apps, such as GitHub Desktop, Sourcetree, and GitKraken, for a visual view of branches and history.
- IDEs, such as JetBrains DataGrip and IntelliJ, which have Git built in much like VS Code.
Key takeaways
- Use Git from the command line, from an editor such as VS Code, or from a desktop app; they all work on the same repository.
- Set your name and email once, and use an SSH key or a credential helper to push.
- A small set of commands covers daily work;
git log,git blame, andgit showanswer "what changed, and who did it?". - Undo safely:
git restorefor uncommitted edits,git revertfor pushed commits. Avoid rewriting shared history. - VS Code's Source Control view stages, commits, branches, syncs, and resolves conflicts with clicks, and extensions add pull request reviews.