Home

Git Cheat Sheet

A version control system that manages changes to files.

Cloning and Initializing

   
git clone {https} Pulls github repository and initializes it.
git init Creates .git file to allow for pushing.

Committing

   
git status Shows all files that were updated, created, or deleted and if the files are untracked, tracked, or staged. Tracked meaning Git tracks versioning of the file and staged meaning the file is ready to be committed. All staged files are tracked.
git add {File} Tells git to track and stage the file so it can be committed.
git add -A Tracks and stages all the files and folders listed in status.
git commit -m ”{Title}” -m ”{Description}” Commits staged files. It is recommended to have your Title be in the present tense. Ex: “Adds some stuff”

Resetting Commits

   
git reset -hard {commit hash} Resets to commit specified and deletes any commits after that commit.
git reset {commit hash} Resets to commit specified and unstages changes.
git reset -soft {commit hash} Resets to commit specified, but leaves files staged.

Reverting Commits

   
git revert {commit hash} Makes a new commit that undos the changes from the commit hash. Does the opposite of the changes made in that commit hash.

Commit Logs

   
git log Lists commit history. This can tell you the commit hashes.
git log {branch} Lists commit history for that branch.

Pushing

   
git push {location} {branch} Pushes commits to the branch at the specified location.
git remote -v Shows locations you can push to.
git remote add {location} {https} Adds a location you can push to.
git remote remove {location} Removes a location you can push to.

Pulling

   
git pull {location} {branch} Pulls changes made from location’s branch. Pulling is the same as fetching and merging.
git fetch {location} The metadata in the local repo is updated, but not files are downloaded.
git fetch Fetches all the branches.
git merge {location}/{branch} Incorporate the changes from fetch into the specified branch. This downloads the changes from the remote repo.

Branches

   
git checkout -b {branch} Creates a new branch.
git branch Shows all branches. * is for current branch.
git checkout {branch} Switches branch.
git branch -d {branch} Deletes branch.

Stashing

   
git stash Saves changes to a temporary storage area.
git stash list Lists all the stashes.
git stash pop Restore changes from the stash to your current directory and removes the last stash.
git stash apply Restore changes from the stash to your current directory and keeps the last stash.
git stash drop Removes the last stash.
git stash drop {index} Removes the stash at the index.

Setting Personal Access Tokens

Settings $\rightarrow$ Developer settings $\rightarrow$ Personal access tokens $\rightarrow$ Generate new tokens

   
git remote set-url {location} https://{username}:{personal access token}@github.com/{owner’s username}/{repository}.git Gives permissions for the location.

First push

  1. git init
  2. git add -A
  3. git commit -m “{Title}” -m “{Description}”
  4. git remote add {location} {https}
  5. git remote set-url {location} https://{username}:{personal access token}@github.com/{owner’s username}/{repository}.git
  6. git push {location} {branch}

Rebasing

   
git rebase {branch} Takes the commits from current branch and puts them ontop of the specified branch.

Pull Requests

   
Create a merge commit Combines the changes from the source branch and the target branch and makes a commit to the target branch with the changes. Preserves the history.
Squash and merge Combines all the changes from the source branch and makes a commit to the target branch with the changes. This removes the source branch. Doesn’t preserve the history.
Rebase and merge Commits all the previous commits in the source branch and adds them to the target branch. This removes the source branch. Preserves the history.

When your branch says behind main

<<<<<<< HEAD
// Your changes in the current branch
=======
// Changes from the branch you are merging with
>>>>>>> branch-name
  1. Once resolved commit and push your update
  2. It should now say ahead of main

Setting up with SSH

  1. ssh-keygen -t ed25519 -C {github email}
  2. eval "$(ssh-agent -s)"
  3. inside file ~/.ssh/config
     Host *
         AddKeysToAgent yes
         IdentityFile ~/.ssh/id_ed25519
    
  4. Go to github settings -> SSH and GPG keys -> New SSH key
  5. Paste the contents of id_ed25519.pub into github and press Add Key
  6. To test if it worked run: ssh -T git@github.com and you should see Successfully Authenticated
  7. To use with SSH make sure your github repo locations are SSHs(they starts with git@github.com)

Adding a warning when pushing to main/master from another branch

#!/bin/bash

current_branch=$(git symbolic-ref --short HEAD)

while read local_ref local_oid remote_ref remote_oid
do
	if [[ "$current_branch" != "main" && "$current_branch" != "master" ]]; then
		if [[ $remote_ref =~ master || $remote_ref =~ main ]]; then
			read -p "You're about to push to main/master. Are you sure? [y/n] " -n 1 -r < /dev/tty
    	    echo
			if [[ $REPLY != "y" || $REPLY != "Y" ]]; then
				exit 1
			fi
		fi
	fi
done

exit 0

Git Ignore

A file called .gitignore which is used to ignore folders or files when pushed to github.

Git ignores use regexes.

This simply doesn’t push the folder called “Folder”, the file called “file”, and any files that start with regex. that are in the Regex folders.

#titles
Folder
file
Regex/regex.*