on November 27, 2019
I created a cheat sheet for the Git Command Line Interface to go along with my Git tutorial for SQL Change Automation video.
I find the Git CLI to be very friendly and easier to learn than a GUI interface.
Setup
Download git CLI: https://git-scm.com/downloads
git clone
Clone a remote repo (maybe an empty one) into your local directory. Ref.
git init
If not cloning, you might initialize a fresh repo locally. Ref.
.gitignore file
- I often show people how to use Git with Redgate’s SQL Change Automation. For that tool, you can download and unzip the .gitignore file to the folder where your .sqlproj file lives: https://documentation.red-gate.com/display/sca4/Version+control.
- Another great resource for figuring out what to ignore is http://gitignore.io/
Essentials
git status
Lists modified or added files, with status as to whether they are staged. Ref.
git add .
Stages all modified or added files. You must stage before you commit. Ref.
If you want to unstage everything: git reset Ref.
git commit -m “this is my meaningful message”
Commits staged files. Ref.
git push
Pushes staged files to remote repo. If upstream branch doesn’t exist, git CLI prints syntax to push to upstream branch with same name as your local branch: simply copy and paste! Ref.
git pull
Fetches from remote and updates current branch. If using feature branches, remember to checkout master and pull after you merge a Pull Request on the upstream repo. Ref.
Branching
git checkout -b features/mynewfeaturename
Switches to a newly created branch (the -b tells it to create a new branch. In Azure DevOps (and some other systems), the “/” means that this branch will be logically grouped into a features “folder-like-thing” and the branch will be named mynewfeaturename. Ref.
git merge master
Merges the contents of another branch (in this case, it’s master)into your current branch. Ref.
git checkout master
Switches to existing branch named master (trunk). Ref.
git branch
Lists all local branches. Ref.
git branch -d features/mynewfeaturename
Deletes local branch features/mynewfeaturename. Only works if you are not in that branch. Ref.
Stashing
git stash -u
Put away / temporarily removes all modified files. The -u means it applies also to even recently added untracked files (not yet staged). Ref.
git stash pop
Unpack your stash. Doesn’t matter if you’re in a different branch from where you stashed. Ref.