How to Configure Git

This is a quick reference guide for how to quickly configure Git.

The following git config commands will create the file: “.gitconfig”

This file houses all global info. about git, including aliases.

$ git config –global “<your name>”

$ git config –global “<your email>”

To determine if your name was saved to .gitconfig, issue the command “git config” and your name should display.

$ git config –global checkout // git co = git checkout

Instead of typing “git checkout", you just need to type: "git co

$ git config –global commit // git ci = git commit

Instead of typing “git commit", you just need to type: "git ci

$ git init

Create a new git repository — from within the directory you’re currently in. After you issue this command, you’ll have a .git directory.

Git Commands Post Repo Init & Config

After we add or modify a file in our working directory, we need to add it to our staging area. We can determine the status of the file using the following commands: git status

To get a file from our working directory into the staging area: git add <file>

The “index” file from within the .git directory represents the staging-area. The staging-area is also known as the index.

The word “commit” can be a verb and a noun; e.g., we commit something, which means to save it; “a commit” is also a version of our project.

The “objects” directory within the .git directory maintains all our commit history.

Files in our working directory -> repository: 1.) the staging area, 2.) the commit history

To commit a file with a corresponding message: git commit -m “<message>”

To add all files in the working directory to the staging area: git add .

To remove a file from the staging area: git reset HEAD — <file>

Important: every commit in git is a standalone version of your project.

When we commit changes to our projects, a filename of the name of the current branch is created in the .git directory refs/heads. Git defaults to the master branch, so the file “master” appears in this directory upon your first commit. Contained in the file is the hash of the most recent commit (for the branch). A branch is therefore just a pointer to a commit.

To see logs of all prior commits in reverse chronological order (most recent to oldest): git log, git log –oneline

To see the parent commit of a commit: git cat-file -p <hash>

To see all branches in your repo, as well as the one you’re on: git branch

To make a new branch: git branch <new-branch>

HEAD = a pointer to a reference of which branch you’re on; the file exists in the .git directory as the filename: HEAD

To switch to a new branch: git checkout <branch-name>

  • 1.) this will change the HEAD to point to the new branch — you can see this via git log
  • 2.) populate our index/staging-area with a snapshot of the commit we’re switching into
  • 3.) will copy the contents of your index/staging-area into the working directory

Fast-forward merge: we switch to the branch we want to merge into, e.g. git switch main; then we merge, e.g. git merge feature1;

To delete a branch: git branch -d <branch-delete>; you have to be off the branch you want to delete; this will also delete the refs/heads/<branch> file.

Three-way merge, e.g. git merge feature1, when there are commits to both the main branch and the feature1 branch.

Merge commit: a merge that can have more than one parent; you can see this in “merge:” when issuing a git log as two hash values will appear.

To checkout a branch and create it at the same time: git checkout -b <branch-name>

Conflict-resolution markers:

  • <<<<<<< HEAD (current change)
  • =======
  • >>>>>>> <branch-name> (incoming change)

We’ll remove the markers and adjust the file accordingly. We may keep everything, delete some stuff, or even add stuff.

After we do so, we wave the file, issue a “git add .”, and then a git commit -m “<message>”.

To abort a merge (after you started): git merge –abort

Local repository (repo) vs remote repo — hosted at a remote location, e.g. GitHub

To clone a repo: git clone // clone = copy

Read: How to connect to GitHub with SSH

How to connect to Github:

  1. Create public key and private key pair
  2. Add private key to ssh-agent
  3. Add public key to Github

To create a key pair: ssh-keygen -t rsa -b 4096 -C “<email used with Github> // t = type; b = bytes; C = label

In your home directory, you’ll now see two key files in your .ssh directory: (public key), and id_rsa (private key)

“ssh-agent” manages our ssh keys and remembers our pass phrase.

To run ssh-agent: eval “$(ssh-agent -s)”

Edit your ~/.ssh/config file: vi ~/.ssh/config

Copy & paste into your “config” file:

Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa

Add private key to ssh-agent: ssh-add -K ~/.ssh/id_rsa

Add public key to Github: on macOS: pbcopy < ~/.ssh/ // copies file contents to clipboard

In Github, click upper-right corner (your account) -> settings -> SSH and GPG keys -> New SSH key -> Add title and paste contents -> Add SSH key (to complete)

Add a remote repo: git remote add <remote-name, e.g. origin> <url>

Show linked remote repos that are linked/tracked: git remote

The file “config” lists your remote repo if one has been configured for the project.

To push your branch to the remote repo: git push <remote-name> <branch>, e.g. git push origin main; this also creates a remote tracking branch if one doesn’t exist

Remote-tracking branch — local branches, but references to the state of a remote branch in a remote repo; you can’t move these yourself

.git/refs/remotes directory created; ./git/refs/remotes/origin/master — remote tracking branch created as well

Update a remote repo: git push origin main // push changes to remote repo

For a co-worker to get a copy of a repo: git clone <url> <directory-name>; <directory-name> is optional

Default name of remote repository is origin after a clone.

To update remote repo: git push origin master

To fetch or update a local repo from a remote repo: git fetch <remote-repo-name>, e.g. get fetch origin

To see logs for remote: git log –remotes

To merge: git merge <remote-tracking-branch>, git merge origin/main

To pull down changes from a remote repo: git pull <remote> <branch>, e.g. git pull origin main // this does both a git fetch and a git merge

To re-update remote repo: git push origin main

Rebase: git rebase <base-branch>

What rebase does is takes all changes committed on one branch and replays them on another branch.

See all logs: git log –all

The first step of rebasing is to find the common ancestor of two branches; then it’s going to get the deltas; then it will save those changes to a temporary file; then it’ll reset the current branch to the same commit that we’re rebasing onto; the last step is to apply each change in turn.

Golden rule of rebasing — never rebase a public branch; because rebase creates entirely new commits, this can cause major problems when working with other people.

If changes are only in your local branch, you can rebase.

Rebase example: git rebase origin/main

Rebase example continued: git add .; git rebase –continue; git add .; git rebase –continue

To push changes: git push origin main

From second terminal, to refresh: git pull origin master