Git Basics
Tags: git, github, devops
Published: 2020-07-31 05:11:37

<!-- TOC -->

- [Git Basics](#git-basics)
  - [Repository Basics](#repository-basics)
  - [Git Staging, Commits, and Pushing](#git-staging-commits-and-pushing)
  - [Branching](#branching)
  - [Updates and Merging](#updates-and-merging)
  - [Tagging and Logging](#tagging-and-logging)
  - [Setting a Project Up With Git Locally](#setting-a-project-up-with-git-locally)
  - [Setting a Git Repository and Committing to It](#setting-a-git-repository-and-committing-to-it)
  - [Using SSH to Connect to a Repository](#using-ssh-to-connect-to-a-repository) - [For an existing repository:](#for-an-existing-repository)
  - [Deploying to gh-pages](#deploying-to-gh-pages)

<!-- /TOC -->

## Repository Basics

Create a new directory, `cd` to it in your terminal, and perform the following command to create a new git repository:

```sh
git init project-name
```

To create a working copy of local repository

```sh
git clone /path/to/repository
```

To create a working copy of a remote server

```sh
git clone username@host:/path/to/repository
```

## Git Staging, Commits, and Pushing

Stage files via source control:

To add all changes to the index, which is the "queue" where uncommitted changes sit

```sh
git add -A
# or
git add *
```

To check current status of your branch, commits, and staged files

```sh
git status
```

To commit your changes

```sh
git commit -m 'message'
```

To push your commits to the master branch

```sh
git push origin master
```

## Branching

Create a new branch named "feature_x" and switch to it

```sh
git checkout -b feature_x
```

Switch back to master

```sh
git checkout master
```

Delete branch named “feature_x"

```sh
git branch -d feature_x
```

Push the branch to the remote repository so that others can access it

```sh
git push origin <branch>
```

## Updates and Merging

Update your local repository to the newest commit

```sh
git pull
```

Merge another branch into your active branch (check via git status)

```sh
git merge <branch>
```

If conflicts arise, make manual changes and then re-merge with git add

```sh
git add <filename>
```

Preview differences before merging changes

```sh
git diff <source_branch> <target_branch>
```

## Tagging and Logging

Study repository history

```sh
git log
```

Looks at most recent merge

```sh
git log -1
```

## Setting a Project Up With Git (Locally)

1. In your terminal, initialize a local repository

```sh
git init
```

> If necessary, don’t forget to add a `.gitignore` file

2. In Terminal, do: `git add *`
3. In Terminal, do: `git commit -m 'YOUR-COMMENT-HERE'`

## Setting a Git Repository and Committing to It

1. Go to GitHub and create a new repository, calling it `REPOSITORY-NAME`
2. In Terminal, do: `echo "# REPOSITORY-NAME" >> README.md`
3. Write to your `README.md` file: **[click here for reference](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet)**
4. In your terminal, run the following commands:

```sh
git add README.md
git commit -m “INITIAL COMMIT COMMENT"
git remote add origin https://github.com/GITHUB-USERNAME/REPOSITORY-NAME.git
git push -u origin master
```

## Using SSH to Connect to a Repository

#### For an existing repository:

1. Go to GitHub and hit `Clone or Download` and then select `Use SSH` and then copy the URL `sh git@github.com:…`
2. In Terminal, do `sh git remote set-url origin git@github.com:…`
3. In Terminal, do `sh git remote -v` to check if the process was completed correctly.

## Deploying to gh-pages

1. In Terminal, do: `npm init`
2. In Terminal, do: `npm i gh-pages` to install the proper dependencies
3. Create a `.gitignore` file, and add `node_modules` to the file.
4. In packages.json file, replace the `“scripts”: {}` default with:

```json
"scripts": {
    "deploy": "gh-pages -d dist”
}
```

5. In packages.json file, change (or add) the `homepage` section:

```json
“homepage”: "https://your-GH-username.github.io/repository-name”
```

**Example:**

`"homepage": "https://connoro7.github.io/my-project"`

6. Go to GitHub and create a new repository with the same name that you created in the previous step, “repository-name”
7. In Terminal, do `sh git init`
8. In Terminal, do `sh git add .` to add all to staging
9. In Terminal, do `sh git commit -m ‘COMMENT’`
10. Go back to GitHub and copy the `sh git remote add origin git@github.com…` line
11. In Terminal, paste the `sh git remote add origin git@github.com…` line
12. In Terminal, do `sh gitpush -u origin master`
13. In Terminal, do `npm run deploy`