Photoluminescence Math, Machines, and Music

Setting up Git

25 January 2017 Negligible notes Learning Git Git GitHub Programming tools

Terminology

Git, created by Linus Torvalds in 2005, is a software for tracking changes in different versions of a project consisting of plain text. I find Git commands hard to remember, partly because it use text to describe the interaction of multiple components of the project, namely the working tree, the local branch, and the remote. Anyway, I try to give succinct and correct descriptions.

A working tree is the directory where your project is. A local repository (local repo) is a subdirectory in the working tree named .git/; everything Git needs is saved in it. A remote repository (remote repo) stores the corresponding content in a hosting service, like GitHub.

When you commit, you create a commit object including relevant information in the working tree, along with a hash generated with respect to the author, time stamp, and file contents. Commits are linked to each other, making up a commit history. A stash is a commit object not yet committed.

When you clone a remote repo, you create a copy in the local device. When you fork a repo, you clone it and modify it, generating diverging histories. When you pull, you download files from the remote to the local. When you push, you upload files from the local to the remote.

Creating a repo

We use the convention that a $ indicates a command prompt. To create a local repo, cd into the top directory of your project, and run:

$ git init

Remember that nothing affects the remote repo unless you push. Therefore, if you have screwed up something and want to restart from scratch, you may consider deleting the whole .git/ and using git init again.

To create a new remote repo, sign up a Git service provider. The most popular choices are GitHub and Bitbucket. The remote url may either be set with respect to HTTPS or SSH protocol. In the web interface of a GitHub repo, there is a HTTPS URL that looks like:

https://github.com/violapterin/photoluminescence.git

And a SSH URL that looks like:

git@github.com:violapterin/photoluminescence.git

In Git, origin is a default name or the remote repo, which you can change if you want. to set the remote url of origin under SSH protocol:

$ git remote add origin <remote_url>

To change its url later:

$ git remote set-url origin <remote_url>

To list all remote repos added:

$ git remote -v

The result should look like:

origin git@github.com:violapterin/computer-notes.git (fetch)

origin git@github.com:violapterin/computer-notes.git (push)

To see more information about the remote:

$ git remote show origin

Ignoring certain files

It isn’t Git’s original purpose to track binary files. Binary files cannot be compared effectively, nor can it be merged or rebased normally.

Generally, to tell Git to ignore specified files, create a file named .gitignore in the top directory. As an illustration:

.log

*.pdf

/__pycache__/

Here, * is a wildcard. This way, .log in the top directory, all .pdf files anywhere, and everything in the subdirectory /__pycache__/ of the top directory, will be ignored.

After .gitignore is modified, those files tracked before but intended to be ignored, are still tracked. To truly stop tracking them, remove all the cache by

$ git rm -r --cached .

Configuring Git

In general, to configure something in Git, run: (Use --global to set it in all repos.)

$ git config class.attribute value

Alternatively, append the following line, in ~/.gitconfig: (The second line should start with a tab.) [class]

attribute = value

For example, to fill in username and email, set:

$ git config user.name "Raven I. Pilot"

$ git config user.email "violapterin@gmail.com"

As another example, the default editor for commit message is Vi. To change it to Vim, set:

$ git config core.editor "vim"

Or to color Git’s terminal output whenever possible, set:

$ git config color.ui auto

As a last example, by default non-ASCII characters such as Chinese characters are backslash-escaped with character codes in commit record; for instance, becomes \346\210\221. to show them as verbatim, set:

$ git config core.quotepath false

Using SSH keys

GitHub offers the choice to authenticate with HTTPS or SSH. In general, SSH is more secure, but sometimes it gets blocked by a firewall. Some older guides might recommend RSA, but in 2020 the GitHub official guide changed that to ED25519. ED25519 public keys are evidently shorter and considerably stronger.

To generate an ED25519 key, run:

$ ssh-keygen -t ed25519 -C "violapterin@gmail.com"

You will be asked for destination path, and a passphrase. With an SSH agent you have only to type the passphrase once. Remember, a passphrase should be reasonably long, like 6-word. Not only are nonalphanumeric ASCII characters allowed, but they are actually encouraged.

If you have several keys, you may rename the public keys (with extension .pub ) and private keys (without extension), as long as they share the same name without extension, say Ed25519_GitHub.pub and Ed25519_GitHub.pub.

In the web interface of GitHub, in “personal settings”, and in “SSH and GPG keys”, copy and paste the public key.

Under SSH protocol, authentication of SSH Key is easily managed by an SSH agent. (Under HTTPS, the user may also use a credential manager, which I haven’t tried.) When you commit, run the SSH agent to activate your key, by:

$ eval "$(ssh-agent -s)"

$ ssh-add ~/.ssh/Ed25519_github

January 25, 2017; revised July 30, 2021