Git Basics

Posted on Jan 2, 2021

What Is Git - Copies, Snapshots, Visibility

At its core git is all about making copies of a group of files and specifying snapshots of them as you make changes. These snapshots are used to provide visibility into the history and changes of the group of files over time.

Git is what is known as a version control system that enables you to track and manage changes in any set of files. Git is ubiquitously used in programming and sometimes used in writing, especially technical writing. This blog, for example, is managed using git.

How Git Works

Git enables you to designate a folder as a git repository and make complete copies of that folder. You can then make changes to the copies, without impacting the original. These copies can be compared to the original, highlighting the differences. When you are satisfied with your new version you can update the original with the changes.

This makes it easy to test and experiment with files without having to commit to the changes, or worry about losing the original. Even after you have updated the original git enables you to see a complete history of your changes, so you can revert to a previous version if you want.

Basic Workflow

  1. Make a copy of the contents of a folder.
  2. Make changes to the copied files.
  3. Add the changes to a snapshot that will become part of the history.
  4. Merge your updated copy into the original.

Git for Collaboration: Understanding Remotes

Git is a powerful tool for collaboration. It enables many people to work on the same files without confusion, or loss of work.

By hosting the git repository on a server instead of an individual’s computer many people can access it. GitHub and GitLab are an example of a git remotes.

This means that different people can make copies of the same set of files and make changes to them simultaneously. These different sets of changes all end up in the same remote repository and can be compared and combined. Any conflicts, places where two different copies change the same part of a file, get highlighted, so they can be resolved.

Remotes can be confusing because they enable you to perform some git commands through a website instead of through the command line on your local computer. This can make it unclear exactly what git commands they are running and how they relate to your code on your local computer. It is helpful to remember that the files on your computer are a copy of the git repository stored in the cloud.

Terms

  • branch: a complete copy of the files
  • commit: a set of updates
  • remote: a repository located in the cloud, e.g. GitHub
  • local: your computer as distinct from the remote repository

Making Changes Workflow

  1. Copy your remote repository - git clone https://github.com/python/cpython
  2. Move into the repository locally - cd cpython
  3. Pull any updates from the remote - git pull
  4. Create a new branch - git checkout -b fix/formatting
  5. Push your new branch to your remote - git push --set-upstream origin fix/formatting
  6. Add your changes - git add *
  7. Commit your changes - git commit -m "fixed formatting"
  8. Push your changes to your remote - git push
  9. Go to your remote repository and merge your branch in there.
  10. Switch back to your default branch - git checkout main or git checkout master
  11. Update your default branch with your recent changes - git pull
  12. Delete merged branches - $ git branch | grep -v '^*' | xargs git branch -d

For more common git commands and information on them take a look at the git cheat sheet