loading...
Cover image for Git Basics

Git Basics

connoro7 profile image Connor Dillon ・3 min read

Repository Basics

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

git init project-name

To create a working copy of local repository

git clone /path/to/repository

To create a working copy of a remote server

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

git add -A
# or
git add *

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

git status

To commit your changes

git commit -m 'message'

To push your commits to the master branch

git push origin master

Branching

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

git checkout -b feature_x

Switch back to master

git checkout master

Delete branch named “feature_x"

git branch -d feature_x

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

git push origin <branch>

Updates and Merging

Update your local repository to the newest commit

git pull

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

git merge <branch>

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

git add <filename>

Preview differences before merging changes

git diff <source_branch> <target_branch>

Tagging and Logging

Study repository history

git log

Looks at most recent merge

git log -1

Setting a Project Up With Git (Locally)

  1. In your terminal, initialize a local repository
git init

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

  1. In Terminal, do: git add *
  2. 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
  4. In your terminal, run the following commands:
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:
"scripts": {
    "deploy": "gh-pages -d dist”
}
  1. In packages.json file, change (or add) the homepage section:
“homepage”: "https://your-GH-username.github.io/repository-name”

Example:

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

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

Posted on by:

connoro7 profile

Connor Dillon

@connoro7

Flannel and coffee enthusiast living the cliché software developer life in Seattle, WA. Huge fan of other people's dogs, music production, and fitness.

Discussion

markdown guide