Skip to main content

Git Quick Start Guide

··622 words·3 mins
Photo by Yancy Min on Unsplash

Introduction #

Here are my notes for some of the more commonly used git commands along with initial setup for git in Linux.

Installation #

To begin, install as follows for Arch Linux:

$ sudo pacman -Sy git

Or

$ yay git

Pacman will include all required depencies.

Initial Configuration #

First, set your name and email address:

$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "email@address.com"

Then, set your preferred text editor (if you have one). I use nano:

$ git config --global core.editor "nano"

You can verify the updates with:

$ git config --global core.editor

Alternatively, you can edit the git configuration directly with:

$ git config --global --edit

Store Credentials #

In 2021, GitHub disabled authentication via password and now requires authentication with a token. The following command sets up the credential helper, where it will store your token in ~/.git-credentials:

$ git config --global credential.helper store

After you log in during a git push with your username and token, the username or email address and token will be stored in the above location.

Note: The token is stored in plain text, so use caution if that is a concern.

Cloning Repositories #

Repositories can be cloned with the following:

$ git clone https://github.com/<username>/<repository>.git

Updating Repositories #

The local record of a repository can be updated with the following command:

$ cd <repository>/
$ git pull

Adding, Committing, and Pushing Files / Folders #

Any files or directories that have been added, modified, or removed can be add to the list of changes to be pushed with the following command:

$ git add .

This function stages files that have been modified and deleted but new files that you have not added are not affected:

$ git commit -a

This function commits any staged changes:

$ git commit -m "message"

These arguments can be stacked as follows:

$ git commit -am "Add your commit message here"

Note: Without add, commit will handle any changes to files that have been modified or deleted, but will not incorporate any files that have been created.

Then finally pushed:

$ git push

If, for some reason, you would like to reset a commit:

$ git reset

These commands can be chained together with the AND operator:

$ git add . && git commit -am "Add your commit message here" && git push

Stashing Changes #

If you forget to update a repository before making changes, you can “stash” those changes and then re-apply them after running git pull.

First, stash the changes:

$ git stash

Then, update the local record of the repository:

$ git pull

Finally, re-apply the changes you previously made:

$ git stash apply

This has proven to be very useful for me when I forget to update a repository before making edits to the code.

Viewing / Changing Branches #

You can view the local branches of a repository with the following command:

$ git branch

Or teh remote branches with:

$ git branch -r

Or all branches (local and remote) with:

$ git branch -a

The current branch will be denoted with an asterisk. To change branches, use the following command:

$ git checkout <branch>

If you have made changes and want to create a new branch with those changes, you can use the following command:

$ git checkout -b <branch>

And then add, commit, and push as normal.

Deleting Branches #

If a local branch has been merged, it can be deleted with:

$ git branch -d <branch>

If it has not been merged, but you still want to delete it, you can use the following command:

$ git branch -D <branch>

A remote branch can be deleted with:

$ git push origin --delete <branch> 

References #

  1. https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens
  2. https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup#_first_time
  3. https://git-scm.com/book/en/v2/Appendix-C%3A-Git-Commands-Setup-and-Config
  4. https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage#_credential_caching
  5. https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning
  6. https://www.geeksforgeeks.org/difference-between-chaining-operators-in-linux/