Practing Git on Your Computer

Practice Git

Set up a new repository and practice committing

  1. make a new folder
  2. mkdir new_folder_name
  3. navigate into the folder
  4. cd new_folder_name
  5. initialize a git repository
    git init
  6. make a new document
  7. touch new_document_name
  8. commit the new document
  9. git add .
    git commit -m "Add new_document_name"
  10. Add a new document, and/or create a change to the existing document
  11. touch newer_document
    OR
    vim new_document
  12. Make a new commit
  13. git add .
    git commit -m "Make some other change"

Undo changes

  1. make a change. Don't add or commit it yet, but reset to the previous commit
  2. (make a change via vim or even a program like Word)
    git reset HEAD --hard
  3. check to verify the change is gone
  4. git status
  5. make another change, and commit it this time.
  6. (make a change via vim or even a program like Word)
    git add .
    git commit -m "Some short-lasting note here"
  7. reset the project back to the next most recent commit
  8. git reset HEAD~1 --hard
  9. verify the state of your project (in other words, the file and/or directories you changed) is back to the earlier commit.

Advanced .gitignore excersise

Challenge: Create a .gitignore file that ignores images (or a directory of your choice). Commit it to your repository. Test that it's working by adding an image (or something to an ignored directory) and then running git status. If it works, git should not notice a change.

Solutions

Create a file at the root of your git repository. Name it .gitignore. In it, include lines like this:

*.png
*.jpeg
*.jpg
*.tiff
*.gif
(add in other image file types you can think of)
Alternatively, if you're able to have flexibility in organizing the project files, you could create a directory that all images go in - we'll call this file "images" for the purpose of this example, and then add this to your .gitignore:
/images

Make sure you commit the .gitignore file before you test whether it works by adding an image.

Here are some resources for the times you have to deal with files that you'd like git to ignore but they are already tracked, but you want them out of project history: