DEV Community

Cover image for Git Basics
Connor Dillon
Connor Dillon

Posted on

Git Basics

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
Enter fullscreen mode Exit fullscreen mode

To create a working copy of local repository

git clone /path/to/repository
Enter fullscreen mode Exit fullscreen mode

To create a working copy of a remote server

git clone username@host:/path/to/repository
Enter fullscreen mode Exit fullscreen mode

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 *
Enter fullscreen mode Exit fullscreen mode

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

git status
Enter fullscreen mode Exit fullscreen mode

To commit your changes

git commit -m 'message'
Enter fullscreen mode Exit fullscreen mode

To push your commits to the master branch

git push origin master
Enter fullscreen mode Exit fullscreen mode

Branching

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

git checkout -b feature_x
Enter fullscreen mode Exit fullscreen mode

Switch back to master

git checkout master
Enter fullscreen mode Exit fullscreen mode

Delete branch named “feature_x"

git branch -d feature_x
Enter fullscreen mode Exit fullscreen mode

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

git push origin <branch>
Enter fullscreen mode Exit fullscreen mode

Updates and Merging

Update your local repository to the newest commit

git pull
Enter fullscreen mode Exit fullscreen mode

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

git merge <branch>
Enter fullscreen mode Exit fullscreen mode

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

git add <filename>
Enter fullscreen mode Exit fullscreen mode

Preview differences before merging changes

git diff <source_branch> <target_branch>
Enter fullscreen mode Exit fullscreen mode

Tagging and Logging

Study repository history

git log
Enter fullscreen mode Exit fullscreen mode

Looks at most recent merge

git log -1
Enter fullscreen mode Exit fullscreen mode

Setting a Project Up With Git (Locally)

  1. In your terminal, initialize a local repository
git init
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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”
}
Enter fullscreen mode Exit fullscreen mode
  1. In packages.json file, change (or add) the homepage section:
“homepage”: "https://your-GH-username.github.io/repository-name”
Enter fullscreen mode Exit fullscreen mode

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

Discussion (0)