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.
git config --global user.name "Your Name Here" git config --global user.email your@email.com git config --global core.excludesFile ~/.gitignore git config --global init.defaultBranch main
Check the configuration
git config --list
Start by initializing a new repo or cloning an existing one.
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.
--allow-empty
allows a commit to the repo without any files added.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"
.gitignore .vscode *.pyc __pycache__/ **/.working/*
git remote add <remote name> <remote url>
git clone <url>
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>
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
git diff
: Show differences between your working directory and the index.git diff –cached
: Show differences between the index and the most recent commit.git diff HEAD
: Show the differences between your working directory and the most recent commit.git diff –name-only <commit> <commit>
: Show the files that changed between two commits.git log
git branch <new branch name>
git checkout -b <new branch name>
git push -d <remote_name> <branch name> git branch -d <branch name>
To force delete an unmerged branch
git branch -D <branch name>
Push branch to remote repo
git push -u <origin name> <branch name>
To tidy up a messy trail of commits
git reset --soft HEAD~n && git commit -m 'new commit message' git push <remote> <branch> --force
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 }
$archive='c:\path\to\archive.zip'; git archive --format=zip <commit> <file(s)> -o $archive; Expand-Archive $archive -DestinationPath 'c:\path\to\destination'
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
git-extract head $env:temp\foobar
Get URL for remote repo.
git config --get remote.origin.url