Skip to content

Instantly share code, notes, and snippets.

@cblunt
Last active March 11, 2024 16:00
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save cblunt/860360 to your computer and use it in GitHub Desktop.
Save cblunt/860360 to your computer and use it in GitHub Desktop.
A simple git cheatsheet for reference.

Quick Summary

cd my_project

git init .
git add . # add everything
git commit -m 'Initial commit'

... make some changes ...

git add .  # adds all changed files to the stage to be committed
git commit -m 'Some changes'

git push origin master

... make some more changes ...

git add index.html css/screen.css # only add these two files to the stage
git commit -m 'Changed index and screen'

View the commit history

git log

Add a remote repository

git remote add origin git://server/my_project.git 
# git remote add origin file:///path/to/local/repositories/my_project.git # For a local (disk) repository

... and push your changes to it ...

git push origin master

Restoring a file from the repository (replace your working copy)

  1. Write some content to index.html

     echo "Hello" > index.html
     cat index.html
     #  => Hello
    
  2. Commit the new index.html

     git add index.html
     git commit -m 'Hello index.html'
    
  3. Overwrite the original content

     echo "Oops" > index.html
     cat index.html
     # => "Oops"
    
  4. Restore the original index.html from the repository

     git checkout index.html 
     $ cat index.html
     #   => Hello
    

Long Version

Assuming the following project:

mkdir -p my_project my_project/images my_project/css my_project/js
echo "<p>Hello World</p>" > my_project/index.html
touch my_project/js/application.js my_project/css/screen.css

cd my_project

Create a repository, and commit initial files.

git init .
git add .
git commit -m 'Initial commit'

Change and add files.

echo "<p>The world has changed.</p>" > index.html
git add index.html

index.html is now staged. This is a middle-ground between the repository and your working copy. A staged file is not yet committed to the repository, but is independent of any further changes to your working copy.

What this means is that you can continue working on index.html, but still commit the previously staged version:

Commit staged files to the repository...

git commit -m 'Changed content of index.html'

...or re-add your changed working copy

echo "<p>It changed again</p>" > index.html 
git add index.html
git commit -m 'Changed content of index.html'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment