Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added Git cheatsheet #74

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Go [here](guides/operations) for operational guides.
- [Incidents](guides/operations/incidents.md)
- [Monitoring](guides/operations/monitoring.md)

Go [here](guides/cheatsheets) for engineering cheatsheets.

- [Git](guides/cheatsheets/git.md)

### Levels

Go [here](levels) for definitions of positions at The Data Shed and their
Expand Down
Binary file added assets/images/gitmeme.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
334 changes: 334 additions & 0 deletions guides/cheatsheets/git.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
# Git Cheatsheet

![Git Meme!](assets/images/gitmeme.jpeg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not averse to the meme, but would be better to change the terminology from master to main as per: https://github.com/github/renaming#renaming-the-default-branch-from-master


## **Welcome!**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Titles shouldn't need the additional bold-ificiation here i.e. drop the **'s, please :)
(surprised markdownlint didn't pick up on this, to be honest!

There are exceptions to this rule, but adding what is effectively a <b>bold</b> tag to all headings is unnecessary.


Version control is the essence of engineering here at the Shed. Git is a free
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"the Shed" -> "The Shed" 👍

version control system that's responsible for everything GitHub related that
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[git is] responsible for everything GitHub related

It's for everything git related. GitHub is just one of many git-based version control system.

happens locally on your computer. This cheatsheet features the most important
and commonly used Git commands for easy reference.

Full Git Techincal Guide can be
[here](https://github.com/TheDataShed/EngineeringHandbook/blob/main/guides/technical-guides/git.md).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be swapped to a relative link, please? e.g. guides/technical-guides/git.md


## **Cheatsheet includes:**

1. Installation & GUI
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The list shouldn't be indented - it may confuse the renderer and turn this in to a code block, instead of an ordered list!

2. Setup
3. Setup & Init
4. Stage & Snapshot
5. Branch & Merge
6. Inspect & Compare
7. Tracking Path Changes
8. Ignoring Patterns
9. Share & Update
10. Rewrite History
11. Temporary Commits

## **Installation & GUI**

With platform specific installers for Git, GitHub also provides the ease of
staying up-to-date with the latest releases of the command line tool while
providing a graphical user interface for day-to-day interaction, review, and
repository synchronisation.

You can download Git for Windows and Mac [here](https://git-scm.com/).

If you prefer the Git Desktop client, navigate
[here](https://desktop.github.com/).

Alternative GUI programs are also available:

- [SourceTree](https://www.sourcetreeapp.com/)
- [Git Kraken](https://www.gitkraken.com/)
- [VS Code Extension - GitLens](https://marketplace.visualstudio.com/items?itemName=eamodio.gitlens)

PsypherPunk marked this conversation as resolved.
Show resolved Hide resolved
---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the --- bits (<hr />) are unnecessary.


## **SETUP**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Titles should be in Title Case. CSS styling can apply the upper-casing if required, but can't remove the upper-casing if the underlying text is in all caps!


---

Configuring user information used across all local repositories.
PsypherPunk marked this conversation as resolved.
Show resolved Hide resolved

_Set a name that is identifiable you when review version history_.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be tempted to swap these for the next level of headings, rather than an italicised string!


git config --global user.name “[firstname lastname]”
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whilst the 4-space indent does work for code blocks, it means we can't benefit from any syntax-highlighting offered by the renderer.

Better to swap these to be code blocks marked by three backticks, and a language hint e.g.

git config --global user.name "[firstname lastname]"


_Set an email address_.

git config --global user.email “[valid-email]”
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you swap the smart-quotes for vanilla double-quotes, please?

Copy-pasting the command in some terminals with those smart quotes will cause errors.


_Adding the `--local` option or not passing a config level option at all, will
set the user.name for the current local repository._.

git config --local user.name "[First] [Last]"
git config --local user.email “[valid-email]”

## **SETUP & INIT**

Below are commands for initializing and cloning repositories.

_Initialise an existing directory as a Git repository_.

git init

_Retrieve an entire repository from a hosted location via URL_.

git clone [url]

---

## **STAGE & SNAPSHOT**

---

Working with snapshots and the Git staging area.

_Show modified files in working directory, staged for your next commit_.

git status

_Add a file as it looks now to your next commit (stage)_.

git add [file]

_Commit your staged content as a new commit snapshot_.

git commit -m “[descriptive message]”

_Upload any changes to your local repository to your remote repository_.

git push

_Upload any changes to your local repository to your remote repository,
rewriting the git history (index)_.

NOTE: It's normally discouraged except for certain use cases like squashing
commits.

git push -f
PsypherPunk marked this conversation as resolved.
Show resolved Hide resolved

_If you find yourself in a situation where you are required to do a force push,
you can push multiple branches and only force one like so: (+ denotes that the
following branch will be force pushed (issue/1))_.

git push origin +issue/1 issue/2

_Unstage a file while retaining the changes in working directory_.

git reset [file]

_Diff of what is changed but not staged_.

git diff

_Diff of what is staged but not yet commited_.

git diff --staged

---

## **BRANCH & MERGE**

---

Isolating work in branches, changing context, and integrating changes.

_List your branches. a\*will appear next to the currently active branch\._

git branch

_Create a new branch_.

git branch [branch-name]

_Switch to another branch and check it out into your working directory_.

git checkout

_List out all branches in the local repository_.

git branch

_Undo any changes you make and return to your previous branch_.

git switch [branch-name]

_If you instead want to keep your changes and continue from here, you can use
this command to create a new branch from this point_.

git switch -c [new-branch-name]

_Switch to the specified branch. Creates the branch in your local repository if
it doesn't already exist_.

git checkout -b [branch_name]
PsypherPunk marked this conversation as resolved.
Show resolved Hide resolved

_Merge the specified branch’s history into the current one_.

git merge [branch]

_Show all commits in the current branch’s history_.

git log

---

## **INSPECT & COMPARE**

---

Examining logs, diffs and object information.

_Show the commit history for the currently active branch_.

git log

_Show commits on the current branch that aren't on `branchA`_.

git log branchA..

_Show commits on the `branchA` that aren't on the current branch_.

git log ..branchA

_Show the commits that changed file, even across renames_.

git log --follow [file]

_Get the author of a commit_.

git blame [commit_hash]

_Show the diff of what is in `branchA` that is not in `branchB`_.

git diff branchB...branchA

_Show any object in Git in human-readable format_.

git show [SHA]

---

## **TRACKING PATH CHANGES**

---

Versioning file removes and path changes.

_Delete the file from project and stage the removal for commit_.

git rm [file]
PsypherPunk marked this conversation as resolved.
Show resolved Hide resolved

_Remove it from Git's control but leave the file in place_.

git rm --cached

_Change an existing file path and stage the move_.

git mv [existing-path] [new-path]

_Show all commit logs with indication of any paths that moved_.

git log --stat -M

---

## **IGNORING PATTERNS**

---

Preventing unintentional staging or committing of files.

_Save a file with desired patterns as .gitignore with either direct string
PsypherPunk marked this conversation as resolved.
Show resolved Hide resolved
matches or wildcard globs_.

You can navigate to the
[gitignore](https://www.toptal.com/developers/gitignore/) website where you can
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our git policies page suggests the use of https://www.gitignore.io/, so this should probably be consistent!

create ignore files with ease, if you prefer.

For python projects you can also use
[cookiecutter-pypackage](https://github.com/TheDataShed/cookiecutter-pypackage).

logs/
*.notes
pattern*/

_System wide ignore patern for all local repositories_.

git config --global core.excludesfile [file]

---

## **SHARE & UPDATE**

---

Retrieving updates from another repository and updating local repos.

_Add a git URL as an alias_.

git remote add [alias] [url]

_Fetch down all the branches from that Git remote_.

git fetch [alias]

_Merge a remote branch into your current branch to bring it up to date_.

git merge [alias]/[branch]

_Transmit local branch commits to the remote repository branch_.

git push [alias] [branch]

_Fetch and merge any commits from the tracking remote branch IF
remote-tracking_.

git pull
PsypherPunk marked this conversation as resolved.
Show resolved Hide resolved

_If remote-tracking is not set up, you need to be more explicit to pull_.

git pull [remote] [branch]

---

## **REWRITE HISTORY**

---

Rewriting branches, updating commits and clearing history.

_Apply any commits of current branch ahead of specified one_.

git rebase [branch]

_Clear staging area, rewrite working tree from specified commit_.

git reset --hard [commit]

---

## **TEMPORARY COMMITS**

---

Temporarily store modified, tracked files in order to change branches.

_Save modified and staged changes_.

git stash

_List stack-order of stashed file changes_.

git stash list

_Write working from top of stash stack_.

git stash pop

_Discard the changes from top of stash stack_.

git stash drop
GooseLF marked this conversation as resolved.
Show resolved Hide resolved