How to Use Git

Let’s say you’re working on a project and you want to do some version control. You could either create a new folder for each version and keep track of changes in a text file. But that’s really inefficient for a large group project. So, it’ll make sense to use some version control software. Enter Git.

About Git

Git is a unique type of version control. It has an intuitive branching model. Branching is a method of keeping different features separate from a master branch. A master branch is automatically created and meant to keep track of all finished features. All other branches keep feature development clean and concise. Here is a diagram to show how Git does branching:

https://git-scm.com/about

Git Branching

Beyond branching, Git is extremely fast. Instead of keeping track of every single file, Git takes snapshots of each changed file. This allows for small data transfers and quick file checking.

Another major feature that Git allows is a user-friendly interface: GitHub. GitHub allows users to look at tons of current and past projects that use Git. I use GitHub to help manage a few projects I work on, and it is extremely intuitive and simple to use.

How to Use Git

Before immediately using Git, you will Git to be installed on your machine. I use a Linux computer, so installing Git is simple since you can use the built-in package manager. If you’re on Windows or a Mac, you can download an installer from www.git-scm.com. Now, let’s get started!

If you’re on a Windows, open Git Bash. If you’re on a Linux or Mac, just open up a terminal. Let’s make a project folder by running this command:

mkdir git-project

Now, we have a folder called “git-project” to create our git project in. Let’s enter that folder by running:

cd git-project

Next, let’s initiate our git project. Using the git init command we can create our project. We can also make our project have a unique name, so let’s do that like so:

git init first-git-project

Now, we have our first Git project made. Many people now would ask, how to we link this to GitHub. Well, it’s pretty simple. First, a project must be made on GitHub called “first-git-project” or whatever the name of your project is. Then, we need to use the git remote command to link our local project to the project on GitHub. We do this like so

git remote add http://www.github.com/username/projectname.git

This code is for a generic project. The concrete example for the project “first-git-project” is

git remote add http://www.github.com/sv4u/first-git-project.git

The next command to learn is how to commit to your project and push your changes to GitHub. First, we have to stage our file to be committed. We must use the git add command. To use it we run

git add filename

Then, we can commit the file normally. We use the git commit command. Since we’ve already staged our files with the git add command, we can run

git commit -a

This will open up the default editor. You can then type in your commit message and finalize the commit. Now let’s push these changes to GitHub. To do this, we’ll use the git push command. We’ll run

git push origin master

You’ll need to type in your GitHub username and password. Then your changes will show up on GitHub.

If you want to learn more about how to use Git, I suggest you go read the Git documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.