Git

Git is a version control software originally started by Linux Toralds (The creator of Linux). It was designed as an alternative to other version control software like CVS or SVN. It differs from its competitors in that each user clones down the entire contents of the repository to their machine and push up changes that they make locally, only requiring the user to connect to the server to save their work.

If you are trying to learn git I recommend the git tutorial: https://git-scm.com/docs/gittutorial.

I'm not going to give too in depth of a tutorial, but I will explain some basic operations that are common with git and explain my way through them.

Cloning down a repository

As you can read above what makes git different from some of the other version control software out there git has a concept of a remote repository of code and that users clone down a copy of the code. For an example let's look at how you would clone down the repository that these notes are generated from:

git clone https://github.com/maker2413/Notes.git

This will clone down a copy of this repository to your computer in your current working directory. I do however want to take a moment to mention ssh keys with regards to git because I think is a vast superior way to interact with remote repositories. A great guide on generating an ssh key can be found here: https://git-scm.com/book/en/v2/Git-on-the-Server-Generating-Your-SSH-Public-Key.

Once you have an ssh key created you will just need to add the public key to the remote git server, which in this case of this repo is GitHub, however GitLab is also a popular public remote.

Git config

With the git cli you can configure almost any feature you could want to configure. Something cool about git configuration is that you can have both a global configuration, normally found in $HOME/.git/, and you can have a git config local to a repo.

Running git config <option> <value> will set it locally, while using: git config --global <option> <value> will set it globally. I'm not going to get into everything you can configure for git, but I am going to point out one of the config options to set early on.

git config --global user.name "John Doe"
git config --global user.email "johndoe@example.com"

You can see the list of config options with: git config --global --list.

Creating a commit

When you have made changes to a repo and you would like to push your changes up to the git server you need to first make a commit. To make a commit first you should run:

git status

In my case if I run this right now I get the following:

You can even see in that output that git is trying to tell us what to do next. We want to do git add on any files we want to include in the commit. Note you can add all changes git add *, but I would break that habbit if you have it or never do that to begin with as adding all files and blindly committing can lead to unintentionally pushing up confidential information.

After you add the files you want to commit you can simply run git commit to create a commit. You should be dropped into the default text editor on your system to add a commit message. You can also write your commit message with the -m flag, skipping over the editor step.

git add git.org
git commit -m "adding more context and examples to git doc"

Pushing and pulling

Once you have one or more commits and you would like to push them up to the remote git server you can simply run git push. It is wise, however to pull down any new changes that may be out on the remote. This can easily happen if you are collaborating with others. To pull down changes simply run: git pull.

git pull
git push

Subtree Commands

  • git remote add -f <repoAlias> <repoURL> [--squash]
  • git subtree add --prefix <subtreePath> <repoAlias> <branch> [--squash]
  • git subtree push --prefix <subtreePath> <repoAlias> <branch>
  • git subtree pull --prefix <subtreePath> <repoAlias> <branch>

Other helpful pages

If you still want to learn more consult the git cheat sheet.

This page was last updated: 2024-11-07 Thu 16:56. Source