Logo

Git Basics

Git Basics

Learning Outcomes

  • The difference between git and GitHub
  • Version control
  • Staging and committing in git
  • Pulling and pushing for Github

Git

Git is a version control system that allows you to track changes in any set of files. This can be thought of as Google Doc's "Revision History", where you can see the exact changes someone made at a given time stamp. In git, instead of time frames determining our version history as is done in Google Docs, we have control of when we make a change in a file by using commits.


Basic Git Commands

Let's say you want to implement version control on some group of files on your system. After installing git by following the steps outlined in the installations lesson, we will initialize a local git repository(or repo, which is a folder that can use git commands). To do so, we will need to:

  • Open up our command line and cd into the folder we would like to serve as the repository and run the git init command. This will create a .git subdirectory that contains Git metadata for the new repository.
  • Next, we will modify our files to prepare ourselves for our first commit. Run the command git status to see which files have been modified since the last commit.
  • When ready, we will place our files in the staging area, which specifies which files will be included in the next commit. You can do so by running:
git add <file(s)>    # stage a set of files
git add <directory>  # stage a directory
git add .            # stage all modified files
  • Lastly, we will commit our changes. You can either run git commit which will launch your shell and prompt you to write a commit message or you can do it in one command by running git commit -m "[your_message]".
  • Run git log to see your commits in your command line listed in reverse chronological order.

Where does Github fit in?

GitHub allows you to store your commits online in a GitHub repo which can be also referred to as your remote repository. This can be useful if you are working on multiple machines or working on a project with another person. You can store your commits on GitHub by pushing your local commits to a GitHub repository. This can be accomplished by a simple git push command. If some other computer pushed new commits to the repository, you can download those changes by going into your local repository and pulling them. This is similarly accomplished with the git pull command. You will get some practice with pushing and pulling in the next lesson.

Knowledge Check

  • Why is version control useful?
  • What is a repository? What's the difference between a local and a remote repository?
  • Why do we need to stage changes in git?

Additional Resources

Contribute to this lesson