<commit_before>---
layout: post
title: "Git Help"
date: 2020-02-25
categories: ['git', 'help']
---

> The purpose of this post is to serve as a summary and table of contents for various git help I've found useful over the years. I hope it helps someone else too.
> This will be a live document which will be published incomplete and be updated over time.

Git is a wonderful tool for source control management (scm), especially when working on multiple features as an individual or on a team.

Personally, I don't like to get too complicated with my git commands. I prefer to know the core of git well enough to serve me for 99% of of uses. Then using Google-fu for those 1% situations.

## Start with Terminology

| Term | Description |
|------|-------------|
| local | on the harddrive of the computer running your git commands |
| remote | central scm server (i.e. Github, Gitlab, Bitbucket, etc.) |
| origin | traditional tag associated with a repo's remote |
| commit | set of file changes with a message (think of this similar to a checkpoint save) |
| branch | ... |
| merge | ... |
| rebase | ... |
| push | upload your local commits to a remote |
| pull | download remote commits to your local |
| clone | download a remote repo to your local for the first time |
| staged | local files which have been prepared to be included in the next commit (the targets of `git add`) |
| unstaged | local files which have been included in a prior commit with changes not yet ready to be committed |
| untracked | local files within a repo's directory which have never been included in a commit |

---

### Set up ssh key for use with ssh git auth
```shell
ssh-keygen -t rsa -b 4096 -C "<EMAIL>"
# provide optional passphrase
eval "$(ssh-agent -s)"
ssh-add -K ~/.ssh/id_rsa

# copy public key to scm provider
# follow their instructions for where you set in your auth profile
cat ~/.ssh/id_rsa.pub
```

### Start a new local repo
```shell
mkdir localRepo
cd ./localRepo
git init
```

### Pull Remote Repo to Local (for the first time)
```shell
# assuming Github project fakerepo, owned by dlstadther
# with ssh auth
git clone git@github.com:dlstadther/fakerepo.git

# with https basic auth
git clone https://github.com/dlstadther/fakerepo.git

# ssh auth with custom directory name
git clone git@github.com:dlstadther/fakerepo.git foobar
```

### Update local references to remotes
```shell
git fetch origin
```

### Start new branch
```shell
# create and checkout a new branch "feature/my-feature" based off local master
git checkout -b feature/my-feature master
```

### View all locally tracked branches
```shell
git branch -a
```

### Change branches
```shell
# assumes already on master, and the existence of develop
git checkout develop

# switch back to previous branch
git checkout -
```

### Update branch with remote master
```shell
# without updating local master w/o merge commit
git fetch origin
git rebase origin/master

# with local update and merge commit
git checkout master
git pull origin master
git checkout -
git merge master
```

### Trash all tracked (and uncommitted) changes
```shell
git reset --hard
```

### Trash uncommitted changes for a single tracked file
```shell
git checkout HEAD -- path/to/file
```

---

## Important Links and Practices
### Use Git Flow
[Gitflow](https://nvie.com/posts/a-successful-git-branching-model/) is a branching and development practice which encourages project organization and release management.

While I like the gitflow branching model, I do not like to rely on the obfuscation of the gitflow commands. Although requiring more keystrokes, I prefer to be hyper-aware of the component git commands which follow the gitflow branching model. Check out this [gist](https://gist.github.com/dlstadther/5968c732584074ff351a5794917ff3fb) for a comparison of the git equivalents of gitflow commands.

### Write Useful Commit Messages
Commit messages contain a lot of value if used well and written effectively. I strongly recommend reading <NAME>'s blog post about [How to Write a Git Commit Message](https://chris.beams.io/posts/git-commit/).

### Git In Practice
A very thorough and practical approach to teaching in-depth git: [GitInPractice](https://github.com/MikeMcQuaid/GitInPractice).

### Git Flight Rules
Sometimes things go wrong and you need to know how to dig yourself out of a hole. In these moments, consult [git-flight-rules](https://github.com/k88hudson/git-flight-rules).

### Read Documentation
When all else fails, or you have ample curiosity (and time), checkout the [git manpages](https://git-scm.com/docs).
<commit_msg>Update wording and case of headings<commit_after>---
layout: post
title: "Git Help"
date: 2020-02-25
categories: ['git', 'help']
---

> The purpose of this post is to serve as a summary and table of contents for various git help I've found useful over the years. I hope it helps someone else too.
> This will be a live document which will be published incomplete and be updated over time.

Git is a wonderful tool for source control management (scm), especially when working on multiple features as an individual or on a team.

Personally, I don't like to get too complicated with my git commands. I prefer to know the core of git well enough to serve me for 99% of of uses. Then using Google-fu for those 1% situations.

## Start with Terminology

| Term | Description |
|------|-------------|
| local | on the harddrive of the computer running your git commands |
| remote | central scm server (i.e. Github, Gitlab, Bitbucket, etc.) |
| origin | traditional tag associated with a repo's remote |
| commit | set of file changes with a message (think of this similar to a checkpoint save) |
| branch | ... |
| merge | ... |
| rebase | ... |
| push | upload your local commits to a remote |
| pull | download remote commits to your local |
| clone | download a remote repo to your local for the first time |
| staged | local files which have been prepared to be included in the next commit (the targets of `git add`) |
| unstaged | local files which have been included in a prior commit with changes not yet ready to be committed |
| untracked | local files within a repo's directory which have never been included in a commit |

---

### Set up ssh key for use with ssh git auth
```shell
ssh-keygen -t rsa -b 4096 -C "<EMAIL>"
# provide optional passphrase
eval "$(ssh-agent -s)"
ssh-add -K ~/.ssh/id_rsa

# copy public key to scm provider
# follow their instructions for where you set in your auth profile
cat ~/.ssh/id_rsa.pub
```

### Start a new local repo
```shell
mkdir localRepo
cd ./localRepo
git init
```

### Pull remote repo to local (for the first time)
```shell
# assuming Github project fakerepo, owned by dlstadther
# with ssh auth
git clone git@github.com:dlstadther/fakerepo.git

# with https basic auth
git clone https://github.com/dlstadther/fakerepo.git

# ssh auth with custom directory name
git clone git@github.com:dlstadther/fakerepo.git foobar
```

### Update local references to remotes
```shell
git fetch origin
```

### Start new branch
```shell
# create and checkout a new branch "feature/my-feature" based off local master
git checkout -b feature/my-feature master
```

### View all locally tracked branches
```shell
git branch -a
```

### Change branches
```shell
# assumes already on master, and the existence of develop
git checkout develop

# switch back to previous branch
git checkout -
```

### Update branch with remote master
```shell
# without updating local master w/o merge commit
git fetch origin
git rebase origin/master

# with local update and merge commit
git checkout master
git pull origin master
git checkout -
git merge master
```

### Trash all tracked (and uncommitted) changes
```shell
git reset --hard
```

### Trash uncommitted changes for a single tracked file
```shell
git checkout HEAD -- path/to/file
```

---

## Important Links and Practices
### Use gitflow
[Gitflow](https://nvie.com/posts/a-successful-git-branching-model/) is a branching and development practice which encourages project organization and release management.

While I like the gitflow branching model, I do not like to rely on the obfuscation of the gitflow commands. Although requiring more keystrokes, I prefer to be hyper-aware of the component git commands which follow the gitflow branching model. Check out this [gist](https://gist.github.com/dlstadther/5968c732584074ff351a5794917ff3fb) for a comparison of the git equivalents of gitflow commands.

### Write useful commit messages
Commit messages contain a lot of value if used well and written effectively. I strongly recommend reading <NAME>'s blog post about [How to Write a Git Commit Message](https://chris.beams.io/posts/git-commit/).

### Consult git in practice
A very thorough and practical approach to teaching in-depth git: [GitInPractice](https://github.com/MikeMcQuaid/GitInPractice).

### Bookmark git flight rules
Sometimes things go wrong and you need to know how to dig yourself out of a hole. In these moments, consult [git-flight-rules](https://github.com/k88hudson/git-flight-rules).

### Read documentation
When all else fails, or you have ample curiosity (and time), checkout the [git manpages](https://git-scm.com/docs).

{'fraction_non_alphanumeric': 0.09167750325097529, 'fraction_numerical': 0.008885999133073256, 'mean_word_length': 4.0736668499175375, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 14, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '96707', 'n_tokens_mistral': 2673, 'n_tokens_neox': 2463, 'n_words': 1270}