Git as railroad

Git as railroad
Photo by Neelkamal Deka / Unsplash

I struggled a lot to understand Git. Once I started thinking of it in terms of a railroad — with stations, tracks, train cars, and a station master (Git) — it finally clicked. This post captures how I first made sense of Git.

  1. git init builds a train station in the local directory. The first platform (branch) exists, but no train cars (commits) yet.
    git init
    git init
  2. gh repo create builds the remote train station on GitHub. Now there are two stations (local and remote), but no tracks connecting them.
    gh repo create
    gh repo create
  3. git add gathers up loose cargo (changes since the last add) and loads them on a new train car waiting in the yard (staging area).
  4. git commit attaches that car to the train on the tracks (writes the staged changes into history).
  5. git push origin main lays the tracks from the local main platform to the remote station named origin (if it doesn't exist yet), and tells the engineer to drive the train there.
    git push
  6. git switch -c <branch-name> builds a new fork in the track (a new branch) where we can work on a particular task in a development setting. This “fork” is separate from the concept of forking a GitHub repository. We can merge our development branch back into main with a pull request, share it with others using git push origin <branch-name>, or discard it entirely.
    git switch
    git switch

tags: #tools-and-languages, #git