Git Command

Last Updated Aug 05, 2021

What is a Git Command?

Git is a distributed revision control system. If you've ever saved a document as "my document 1", then "my document 2", and so on, you have an idea of what Git does. But what if you make a mistake on "my document 2" and want to revert to an old version? Or what if your coworker wants to work on the document at the same time, and you're worried you'll end up with two different versions?

Git works by copying a file locally from an online repository. When you're done working on it, you submit the file back to the main repository it was copied from. The repository owner looks it over, and allows it to be "pulled" into the repo, updating the version for everyone.

Git works in the command line, but its capabilities are also available as a desktop program. If you're having trouble with Git in the command line, GitHub Desktop is a great way to learn how Git works before easing yourself into the command line.

Whichever Git you choose, you will have to exchange SSH keys for authentication.

Git Commands Quickstart

This is a quick overview of the Git checkout/checkin process commands from the command line.

  • Clone your fork onto your machine. This implicitly defines a remote repository named origin.`git clone git@github.com:githubusername/filename.git`
  • Set up the upstream remote. This links origin with main on GitHub.`git remote add upstream git@github.com:githubusername/filename.git`
  • Create a new branch. Git branch creates a branch of your origin fork that you can edit locally. Branch early and often.`git checkout -b my_git_demo`
  • Create a new file. Touch creates a new, empty file. Git Add adds any new files to the staging area (you are telling Git that you intend to commit the file you are adding).`touch git_demo add git_demo`
  • Create a commit. This adds your changes to your local repo.`git commit`
  • Add your work to the local repository. Before you add your work, check to make sure you have the latest changes from upstream.`git fetch upstream main` (this will fetch upstream changes) OR`git rebase upstream/main` (this will roll back your commit, apply the fetched changes, and then reapply your commit)
  • Push changes to your fork. This adds your local changes to your GitHub repository.`git push origin my_git_demo`
  • Finally, create a pull request in GitHub. This proposes the addition of your changes to the main repository. The repo owner will double-check your work before adding it to main.

Conclusion

There are many other useful Git commands, but an understanding of the Git process is more important at an early stage. Build yourself a git repository test environment and practice forking, cloning, git merge, and git pull. You'll eventually find you're using Git with confidence. And hey, if you make a mistake, you can always roll back your commits with the git log.