Table of Contents

Git

A general guide for the distributed version control system Git.

This guide shows the Git commands, but for ease of use these can be wrapped in shell functions. See Powershell Git.

Initial Git configuration

git config --global user.name "Your Name Here"
git config --global user.email your@email.com
git config --global core.excludesFile ~/.gitignore

Check the configuration

git config --list

Initializing or Cloning a Repo

Start by initializing a new repo or cloning an existing one.

Initialize new repository

Since a project that is just starting tends to be rudimentary it makes sense to initially create the repo with an empty master branch and then immediately create a working branch where the initial work will begin. Once the project gets further along and hits certain milestones the changes can be merged into the master branch.

Below is an example using Powershell to only run commands if there is not an existing Git repo configured.

git-init.ps1

param(
    [Parameter(Mandatory=$true)] [String]$email
)
 
if (-not(Test-Path -Path ".\.git")) {
    git init --initial-branch=main
    git config --global user.name "John Doe"
    git config --global user.email $email
    git config --global core.excludesFile ~/.gitignore
    git commit --allow-empty -m "Initial commit for bootstrapping the repo"
    git checkout -b working
}
git-init.ps1 -email "jdoe@gmail.com"
Sample .gitignore
.gitignore
.vscode
*.pyc
__pycache__/
**/.working/*
Setup remote repositories for Github and the like
git remote add <remote name> <remote url>

Clone Existing Repo

git clone <url>

Typical Workflow

git diff HEAD
git add -u
git commit -m "wip"
git fetch
git merge <remote name>/<branch>
git push -u <remote name> <branch>
# Alternatively, push all branches
git push --all <remote name>

Seeing Changes

See what's new or modified in the working directory.

git status

List files that were part of the specified commit. Alias gls

git ls-tree --full-tree --name-only -r $commit

Show the details, including changes made, for the specified commit.

 git show $commit

See the changes between the current working directory and the last commit (HEAD) or some other commit (e.g. HEAD~1).

git diff HEAD

History

git log

Branches

git checkout -b <new branch name>

Deleting Branches

git push -d <remote_name> <branchname>
git branch -d <branchname>

Remotes

Push branch to remote repo

git push -u <origin name> <branch name>

Squashing Commits

To tidy up a messy trail of commits

Squash last n commits to one

git reset --soft HEAD~n && git commit -m 'new commit message'
git push <remote> <branch> --force

Archive/Dump/Extract to zip

git archive --format=zip -o $zip_file $commit [file(s) to archive]

Example wrapper function in Powershell

function git-extract([Parameter(Mandatory=$true)][string]$commit, [Parameter(Mandatory=$true)][string]$path) {
    $temp_file="$env:temp\git-dump.zip"; git archive -o $temp_file $commit; Expand-Archive -path $temp_file -destinationpath $path
}

Extract specific file(s) from specific commits and put in a specified path

Example in Powershell

$archive='c:\path\to\archive.zip';
git archive --format=zip <commit> <file(s)> -o $archive;
Expand-Archive $archive -DestinationPath 'c:\path\to\destination'

git-extract

Using Powershell, extract the files from a specified commit to a destination folder.

param(
    [Parameter(Mandatory=$true)] [String]$commit,
    [Parameter(Mandatory=$true)] [String]$DestPath
)
 
$archive="$env:temp\$([System.IO.Path]::GetRandomFileName()).zip"
git archive --format=zip $commit -o $archive
Expand-Archive -Path $archive -DestinationPath $DestPath -Force

Example Usage

git-extract head $env:temp\foobar

Secondary Commands

Get URL for remote repo.

git config --get remote.origin.url

Related