Skip to content

Git basic

Initializing a Repository in an Existing Directory

git init

File Status

Three Status

  1. Modified: the file has been changed but not committed yet
  2. Staged: a modified file is marked in its current version to go into your next commit snapshot
  3. Ommitted: the data is safely stored in local database

Checking the Status of Your Files

git status

Short Status

git status -s
git status --short

There are two columns to the output — the left-hand column indicates the status of the staging area and the right-hand column indicates the status of the working tree

 M README # (1)
MM Rakefile # (2)
A  lib/git.rb # (3)
M  lib/simplegit.rb  # (4)
?? LICENSE.txt  # (5)
  1. File is modified in the working directory but not yet staged
  2. Modified, staged and then modified again
  3. New files that have been added to the staging area
  4. Modified and staged
  5. New files that aren’t tracked

Cloning an Existing Repository

git clone https://github.com/libgit2/libgit2 mylibgit

Tracking/staging New Files

git add README

Viewing Changes

git diff() only shows changes that are still unstaged. If you’ve staged all of your changes, git diff will give you no output.

use git diff to see what is still unstaged
git diff
git diff --cached to see what got staged so far
git diff --cached

If you want to see what you’ve staged that will go into your next commit

git diff --staged

Committing Changes

git commit
git commit -m "Story 182: fix benchmarks for speed" # (1)
git commit -a -m 'Add new benchmarks' # (2)
  1. Inline commit message
  2. Skip the staging area

Removing Files

Remove files from staging area

git rm PROJECTS.md
git rm -f PROJECTS.md # (1)
  1. If you modified the file or had already added it to the staging area, you must force the removal with the -f option

keep the file in your working tree but remove it from your staging area

git rm --cached README

Viewing the Commit History

git log
git log -- graph

Undoing Things

git_undoing

Working with Remotes

Remote repositories are versions of your project that are hosted on the Internet or network somewhere.

Showing Your Remotes

git remote # (1)
git remote -v # (2)
  1. Lists the shortnames of each remote handle you’ve specified
  2. Shows the URLs that Git has stored for the shortname to be used when reading and writing to that remote

Adding Remote Repositories

git remote add <shortname> <url> # (1)
  1. The short name can be used to replace the full URL e.g. git fetch pb

Fetching and Pulling from Your Remotes

git fetch command only downloads the data to your local repository — it doesn’t automatically merge it with any of your work or modify what you’re currently working on. You have to merge it manually into your work when you’re ready.

git fetch <remote>

If your current branch is set up to track a remote branch , you can use the git pull command to automatically fetch and then merge that remote branch into your current branch.

git pull

Pushing to Your Remotes

git push <remote> <branch>
git push origin master

Inspecting a Remote

git remote show <remote>
git remote show origin

Renaming and Removing Remotes

change a remote’s shortname
git remote rename pb paul # (1)
  1. rename pb to paul
remove
git remote remove <remote>