Git Quick Reference Guide

Git is a distributed version control software used for tracking change to any group of files. Git was originally developed in 2005 for use with Linux kernel development. The Git project is a member project of the Software Freedom Conservancy

Starting a Project

git clone

Copy and download a repository to local machine

Example 1. HTTPS clone
git clone https://{serverAdress.xyz[port]/path/to/project/repoName.git}
Example 2. SSH Clone
git clone ssh://{user@serverAddress.xyz[port]:/path/to/project/repoName.git}

git clone private GitHub repo with MFA enabled

First you will need to obtain a Personal Access Token (PTA) from your GitHub account. Settings > Developer Settings > Personal access token > Fine-grained tokens. Add the token to the repo url as shown below.

Example 3. HTTPS clone with PAT
git clone https://{PAT}@github.com/{userName or accountName}/{repoName.git}

git init

Create an empty git repository or reinitialize an existing one

Example 4. git init
git init

Git Configuration

git config

Set User Email, Name and Password

Example 5. Set global user’s email
git config --global user.email "{emailAddress}"
Example 6. Set local repository user’s email
git config --local user.email "{emailAddress}"
Example 7. Set global user’s name
git config --global user.name "{usersName}"
Example 8. Set local repository user’s name
git config --local user.name "{usersName}"
Example 9. Set global user’s password
git config --global user.password"{password}"
Example 10. Set local repository user’s password
git config --local user.passowrd "{password}"
Example 11. Enable global credential storage
git config --global credential.helper store
Security Notice

credential.helper does store the credentials in plain text in the ~/.git-credentials file

Send Updates To or From a Repository

git pull

Example 12. Pulls down latest version of remote repository and merges changes into local repository
git pull

git fetch

Example 13. Downloads latest version of the remote repository without merging the changes.
git fetch https://{serverAdress.xyz[port]/path/to/project/repoName.git}

git push

Add changes from Committed to destination repository

Example 14. Push to original source of cloned repository
git push origin
Example 15. Push a tag
git push origin {TagName}
Example 16. Creates and pushes branch to remote repository
git push --set-upstream origin {branchName}

Add, and Commit Changes

git add

Add changes from Unstaged to Staged

Example 17. Add changes from a directory
git add {relative/patch/to/directory}
Example 18. Add all files in current directory
git add .
Example 19. Add a specific file
git add {fileName}

git commit

Example 20. Add changes from Staged to Committed with a note
git commit -m "{message}"

git reset

Example 21. Reset unstaged changes if they have not been Committed
git reset

Branches, Commits, Tags, and Stashes Management

git branch

Example 22. List branches
git branch
Example 23. Create new branch. Does not move you to the new branch.
git branch {branchName}
Example 24. Delete a branch
git branch -d {branchName}

git stash

Example 25. Creates a new stash and reverts to the most resent commits
git stash
Example 26. Save a stash under a name
git stash save "{StashName}"
Example 27. List stashes
git stash list
Example 28. Restore the changes from the most recent stash
git stash pop

git diff

Example 29. Check differences between two branches
git diff {Branch1} {Branch2}

git checkout

Move Head to a specific commit, branch, or tag.

Example 30. Show UUIDs for commits
git log
Example 31. Move to a specific commit
git checkout {UUID}
Example 32. Check out a tags
git checkout {TagName}
Example 33. Move to a branch
git checkout {branchName}
Example 34. Create new branch and move to it.
git checkout -b {branchName}

git restore

git restore {file}

git merge

Example 35. Merges specified branch in to currently located branch
git merge {SourceBranch}
Example 36. Cancel a conflicting merge
git merge --abort

git rebase

Example 37. Rebase(merge) current branch with SourceBranch
git rebase {sourceBranch}

git tag

Creates name for specific commits. Use instead of UUIDs.

Example 38. List all tags
git tag
Example 39. Add a tag with a name and message
git tag -a {tagName} -m "{message}"

Status and History

git status

Example 40. Show current status of local repository
git status

git log

Example 41. Show history of repository name
git log
Example 42. Compact version
git log --oneline
Example 43. Show commits as a graph
git log --graph
Example 44. Show commits as a compact graph
git log --graph --oneline

Stages

  • Unstaged - Made changes that you are not sure you want to keep

  • Staged - Made changes that you are sure you want to keep

  • Committed - Defiantly want to keep changes

  • Pushed - Uploaded changes to remote repository