Basics of Git

git
_guides
Basics of git
Published

June 28, 2023

Still writing

What is git?

Git is a version control system. At it’s core, it is simply a tool to make management of codebases, or the files and folders that make up code, easier.

A long time ago, before verson control systems, when people manipulated codebases by copying around folders. When another person wanted to merge their changes into the code that someone else was working on, they had to copy and paste it in.

Obviously, this sucked. Now, we have version control, and git is the most popular one, used by the most popular code forge site, github.

How to use git

Get your code: git clone

The most basic, starting command.

git clone repositoryurl

Git has it’s own protocol, but https works as well, and it’s easier to use.

Because of this, you can use the url of the website that hosts the code as your code url.

git clone https://github.com/moonpiedumpling/moonpiedumpling.github.io/

↑ That’s the url of the repository for this website.

Save code to git

Code stored in a git repo exists in four states: unsaved changes, unstaged changes, staged changes, and committed changes, gone through in that order.

First, you open up a file and edit some code. Then you save your changes to the file. However, git doesn’t save your changes. The changes become unstaged changes, meaning git doesn’t even see them.

To stage changes:

git add file/folder

So to stage everything in the repository: git add .

Now, git sees your changes, but it still hasn’t saved them. They are staged changes, meaning that git can see them in order to commit them.

git commit -m "message here" commits your changes with a commit message, which makes keeping track of what you did easier, especially when you need to revert changes.

You can commit everything in one go using git commit -am, which is more convinient if you don’t need to stage changes.

git status shows staged and unstaged changes.

Interact with a remote repository

To upload your changes to a remote repository: git push

Pretty simple. Although you can push to a specific repo.

git push remoterepourl

And to bring changes from a remote repo down: git pull

Viewing and undoing changes

If you make a small change, you can just copy and paste stuff around to undo it. But if you are working on a large app, and a massive feature you intended to add didn’t work out, trying to just delete the code won’t really work out.

That’s where git comes in.

To view

git log shows the changes. q quits, and / searches. When searching, n to go to the next found string, and N (capital N) to go to the previous. It uses vim keybindings. Each change is noted by their SHA1 hash.

But this kind of sucks, so I recommend using a graphical interface to view changes. This is one function of “git forges”, websites like github, where they give you a graphical interface to view changes. However, there are locally installed applications with similar features, like gitg.

To revert to a change

git revert HEAD creates a new commit that undoes the previous commit.

git revert <SHA1 HASH> creates a new commit that reverts all the way back to the commit designated by the SHA1 hash of a commit.

git reset SHA1 has a similar feature, except it simply moves backwards in commits. If you make new commits, this results in a nonlinear search commit history. You’ll create a tree, or an octopus. Some people don’t like this, because it can be a bit of a pain to manage.

To revert a specific file to an older commit:

git checkout <SHA1 HASH> -- /path/to/file

How to actually use git

Yeah, saving and undoing things isn’t anything special. Your average word processor has these features. Carefully managed backups allow you to undo to any state you want. There are probably a thousand or more ways to send folders of whatever over the network.

Branches

Branches are one of the killer features of version control. Basically, each branch is it’s own copy of the codebase, which can be worked on without affecting other branches.

git checkout -b <branch name> creates a new branch.

git checkout <branch name> switches to a branch.

Then, commit away. Switching to another branch and only the changes applied to that branch are present.

In the wild, branches are commonly used to manage versioning. An older version, but still maintained version of a program will be kept in another branch, so people can still work on it.

git checkout manipulates the repository state. To manipulate branches, you must use git branch:

git branch -d <branch name> deletes a branch.