An open API service indexing awesome lists of open source software.

https://github.com/cdobratz/git-command-cheat-sheet

This repository is to store a reference sheet of git and github commands.
https://github.com/cdobratz/git-command-cheat-sheet

git github

Last synced: about 1 month ago
JSON representation

This repository is to store a reference sheet of git and github commands.

Awesome Lists containing this project

README

          

# One More Git Cheatsheet

#### This document is study resources to prepare for the Github Foundation Certification.
Originally this repo started to house basic git commands. Recently I am adding my notes as I go through [this course](https://www.exampro.co/github-choose-an-exam) from Andrew Brown of ExamPro.

### Table of Contents

- [Git Overview of Terms](#git-overview-of-terms)
- [Basic Commands ](#basic_commands)
* [Create](#create)
- [From existing repo](#from-existing-repo)
- [From existing files](#from-existing-files)
* [Commit](#commit)
- [Commit only watches changes that have been marked explictly with add](#commit-only-watches-changes-that-have-been-marked-explictly-with-add)
* [View](#view)
* [Branches](#branches)
* [Update](#update)
* [Remote](#remote)
- [This describes a new commit that undoes previous commits](#this-describes-a-new-commit-that-undoes-previous-commits)
* [Useful Tools](#useful-tools)
+ [Git Workflow](#git-workflow)

![Octocat!](/images/cd_octocat_med.png "Octocat Flair!")


## Git Overview of Terms
- **Repository** (repo): Represents the container holding the codebase
- **Commit**: Represents a change of data in the local repo.
- **Tree**: Represents the entire history of a repo.
- **Remote**: A version of your project hosted elsewhere, used for exchanging commits.
- **Branches**: Divergent paths of developement, allowing isolated changes.
- **Main** (formally known as Master): common name for default branch
- **Clone**: Creates a complete local copy of a repo, including its history.
- **Checkout**: Switches between different branches or commits in your repo.
- **Pull**: Downloads changes from remote repo and merges them into your branch
- **Push**: Uploads local repo changes to a remote repo.
- **Fetch**: Downloads data from remote repo without integrating it into your work
- **Reset**: Undoes local changes, with options to unstage or revert commits.
- **Merge**: Combines multiple commit histories into one.
- **Staging** files: Prepares and organizes for a commit.
- Add: Adds changes to file or files bringing them to the staging area for the next commit.
- Commit: Saves changes as a snapshot in the local repo

- To see the complete documentation follow [Links to Git Docs](https://git-scm.com/docs)
- If your in the wild and get stuck use `git help` [command]


### Basic Commands

```bash
main # default level branch
origin # default upstream
HEAD # current branch
HEAD^ # parent of HEAD
```


### Create


##### From existing repo

Three main ways to clone a repo
- HTTPS
- SSH
- Github CLI

It is possible to clone entire repo or a single branch

```python
git clone [URL] #bring repo from existing repo
git clone ~/old ~/new
git clone --single-branch
```


##### From existing files

```python
git init`
git add my file #add file to repo from local to repo
git add . #add all files
git diff --staged #used after `git add` to review changes to code
```

### Commit


##### Watches changes that have been marked explictly with add

```python
git commit -m "Commit Message" # add changes of repo to main branch
git commit -a -m "Com Message" # -a: automatically stages all tracked modified files before commit
git commit --amend # Modifies most recent commit
git commit -m "Initial Commit" --allow empty # Creates empty commit to act as placeholder
git commit -m "Message" --author="name " # commit with specific author
git push # send local files to remote
git push -u origin # the -u sets upstream branch
```


### View

```python
git status # check local files to main github branch
git diff [oldid newid]
git log [-p] [file|dir]
git blame file
git show id
git show id:file
git branch # see branches
```


### Branches
Can be created through
- Issues tab
- w/ GitHub UI
- w/ GitHub Desktop
- In terminal (below)

```python
git branch # Lists all local branches
git branch [branch-name] # creates new branch
git branch -m [old-name][new-name] # Rename a branch
git branch -d anybranch # with -d anybranch will be deleted
git branch -a # Lists both remote and local branches
git checkout [branch-name] # switchs to branch-name
git checkout -b newbranch # this creates and moves to new branch
git merge # branches together
```


### Update
```python
git pull # get latest changes from branch
git fetch [remote-name] # Fetch updates withour pulling
git apply patch.diff
```


### Remote
```python
# Lists all remote repositories along with their URLs
git remote -v
git remote add [name] [URL]
git remote remove [name]
git remote rename [old-name] [new-name]
```

```pythoh
# Pushes a branch and its commits to the specific remote
git push [remote-name] [branch]

# Pull updates from a remote branch
git pull [remote-name] [branch]
```


##### This describes a new commit that undoes previous commits
```python
git reset --hard # NOT UNDONE, reset to last commit
git revert branch
git commit -a --amend # replaces prev commit
```


### Useful Tools

- `git log` *view log history*
- `git archive` *create release tarball*
- `git bisect` *binary search for defects*
- `git cherry-pick` *take single commit from elsewhere*
- `git fsck` *check tree*
- `git gc` *compress metadata *
- `git rebase` *forward-port local changes to remote branch*
- `git remote add URL` *register new remote repo for tree*
- `git stash` *Temporarily set aside changes*


#### Git Workflows

>##### Simple Github Workflow Example
>
> - Create new branch
> - Add new feature and code
> - add, commit, and push changes to the remote
> - Get changes reviewed by team member
> - Delete remote branch
> - Delete local branch
> - Pull new code on the remote master to local machine
>

##### Workflow with commands

```bash
git checkout -b my-new-branch
# ... make changes to files
git add .
git commit -m "my changes"
git push -u origin my-new-branch
```

#### Extra modifications
- After pull request complete
- `git tag 1.0.1`
- `git push --tags`

> The rest of the notes can be found at my [other Repo](https://github.com/AVhouse/Certification-Study-Notes)