Posted on

Table of Contents

Git is a history and versioning tool used for storing and examining the history of a collection of files. It should be noted that git, is not github. You can use it to view and update project histories. When viewing a history it will update your local directory to match.

This article is primarily a compressed version of the git user manual and the git reference.

vocabulary

  • a repository is where all the files for a project live. A git repository also tracks change history.
  • a commit is a (snapshot of a) specific version of the code stored in a repository.
  • branches are different timelines of commits.
  • a head is (a pointer to) the latest commit on a given branch.
  • a tag is a pointer to specific commit.
  • a merge is when two branches become one.
  • a blob is the git object that holds file data
  • a tree is the object that holds directory structure for blobs and sub-trees. A commit is a snapshot of tree history.
  • to push is to update a remote repository you have write authority on with your new commits.
  • to pull is to take commits from a remote repository and apply them to your own.
  • when you use git to view a certain commit we say you've checked it out since git updates your working directory to be a copy of that commit.
  • staged changes are changes that are marked to be added to your next commit.
  • the index how git represents what files are being tracked in your working directory.

some commands

commit

The commit command is naturally what you use to create a new commit. There are multiple ways of telling the commit command what changes to commit.

If you run git commit it will simply commit all changes in the index. You can edit the index using git add and git rm.

If you run git commit <file1> <file2>... then it will ignore the index and only commit the listed files.

git commit -a will automatically git rm any files in the index that are removed form the working directory and then do a normal git commit

you can use the -m <msg> flag to attach an explanation of your changes.

branch

Branch is used to create and manage your branches. use git branch to list all existing branches. git branch <name> <opt:start_point> to create a new branch, optionally naming a previous commit as a start point, rather than the head. git branch -d <name> will delete the named branch.

switch

Switch is used to check out different branches of your repository. git switch <branch> will update your working directory to match that branch. you can also create a new branch using git switch -c <new branch>

merge

git merge <branch>is used to join the named branch with the current working branch. It requires that the named branch derived from the working branch. It replays all changes made in the named branch since it diverged from the currently checked out branch onto the checked out branch and then makes a commit of those changes. Any uncommitted changes will be lost. The commit contains the names of the parent branches and a log message.

If the merge results in conflict git merge --abort will attempt to undo the merge.

rebase

git rebase <host branch> <moving branch> replays all commits in the moving branch onto the host branch's head. This does not destroy the moving branch, it simply moves its start point to a different commit.

things you may want to do

make a fresh repository

run git init in the top directory of your project. Make sure all your files are added to your first commit.

track down bugs

If you know some previous commit worked, but your current head doesn't you can use bisect to slowly figure out what commit is at fault through binary search. Bisect will take you through your commit history 1 commit at a time. You will test every commit and tell git if it is bad or good, until it can figure out which commit started the problem.

For detailed instructions, see exploring-git-history in the git user manual.

look at a different branch

use git switch <branch name>

get a local copy of a remote repository

use git clone <repository>

keep your local copy of a repository up to date

use git fetch to update all remote tracked branches to the latest version of the remote repository.

track changes to repositories other than the one you cloned from

use git remote add <name> <url> to add another repository to your list of tracked repositories. You can later remove a tracked repository with git remote remove <name>

work on something else and come back

you can set aside a commit you're working on by running git stash push which will push all your current work into the stash and then revert you back to the most recent commit. When you're done you can run git stash pop to reapply your stashed changes