Git - The Basics

May 9, 2023

Git is my latest obsession. A great resource that I've been tearing through is the  Pro Git book written by Scott Chacon and Ben Straub.

You can find a free download for the book here:

https://git-scm.com/book/en/v2

 

What is Git?

 

Git is a version control system that helps developers track changes made to their code over time. It is a powerful tool for collaboration and ensuring that code changes are made safely and efficiently. Originally coded by Linus Torvalds, also the creator of the Linux Operating System, he referred to Git as "the stupid content tracker". While there is a steep learning curve for Git and some limitations, overall it's a great soultion.

 

Give me the basics!

 

Some of the basics of Git that must be learned are:

  • Initializing a Git repository
  • Connecting your local repository to a remote repository server
  • Adding files
  • Committing changes
  • Pushing to remote
  • Pulling from remote
  • Viewing status

Initializing Git Repository

 

To start using Git, you need to create a repository. A repository is a directory where your code and version history will be stored. To create a new repository, navigate to your project directory in the terminal of your own local machine and use the git init command:

 

 

This command initializes an empty Git repository in the current directory.

 

Connecting to a Remote Repository

 

To collaborate with others on your project, you can connect your local repository to a remote repository hosted on a Git host such as GitHub. To do this, you need to create a new repository on Github and obtain its URL.

 

The steps to create a repository on Github:

 

  • Visit http://www.github.com
  • Create an account or sign in to your GitHub account and navigate to your dashboard.
  • Click the "+" icon in the top right corner of the page and select "New repository."
  • Choose a name for your repository and add a description if desired.
  • Select whether you want the repository to be public or private.
  • Choose whether you want to initialize the repository with a README file or not.
  • Select a license for your repository, or choose "None" if you don't want to include a     license.
  • Click "Create repository" to create your new repository.

 

Then, use the git remote add command to connect your local repository to the remote repository:

 

 

This command adds a new remote named origin and sets its URL to the URL of your remote repository. Now you can add and push changes from your local repository to the remote repository.

 

Adding Files

 

Before you can start tracking changes in Git, you need to add files to the repository. To add a file to the repository, use the git add command followed by the name of the file:

 

 

You can also add all files in a directory by specifying the directory name:

  

Committing Changes

 

Once you have added files to the repository, you are ready to commit them. Committing is the process of saving changes to the repository with a commit message that describes the changes you have made. To commit changes, use the git commit command:

 

The -m option allows you to add a commit message that describes the changes you have made.

 

Pushing to GitHub

 

Once you have committed changes to your local repository, you can push those changes to your remote repository on GitHub. In Git, a branch represents an independent line of development, and by default, the first branch is named master. It is important to use branches to isolate changes, experiment with new features, and collaborate on code without affecting the main branch. Use the git push command followed by the remote name and the branch name to push your changes:

 

 

This command pushes the changes in the master branch of your local repository to the origin remote repository on GitHub.

 

Pulling from GitHub

 

If changes have been made to the remote repository on GitHub by other team members, you can pull those changes to your local repository using the git pull command:

 

 

This command pulls changes in the master branch of the origin remote repository on GitHub to your local repository.

 

Viewing Status

 

At any point in time, you can view the status of your repository using the git status command. This command shows you which files have been modified and which files are not tracked:

 

 

Grab the Code

You can find the code from the examples above in one file here:

https://github.com/CodingInGreen/git-basics