Skip to content

Instantly share code, notes, and snippets.

@carloscodingroadtrip
Last active October 27, 2020 05:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carloscodingroadtrip/804a8efff3fda6bb9a72b6c9f0cad940 to your computer and use it in GitHub Desktop.
Save carloscodingroadtrip/804a8efff3fda6bb9a72b6c9f0cad940 to your computer and use it in GitHub Desktop.
Terminal and Git Cheat Sheet

Navigating the Terminal

Basics

Description Command
Current Directory . (single period)
Parent Directory .. (double period)
Home Directory ~ (tilde )
Make a new directory mkdir <directoryname>
Delete a directory (only works on empty folders) rmdir <foldername>
Delete a directory and its contents rm -R -f <foldername>
Make a new file touch <file1.ext>
Delete a file (This deletes the file permanently; use with caution.) rm <file1.ext>
Delete a file ONLY when you give confirmation rm -i <file1.ext>
Delete a file by FORCE without confirmation rm -f <file1.ext>
Delete multiple files without confirmation rm <file1.ext> <file2.ext> <file3.ext>
Move/Rename a file mv <file> <newFileName>
Move a file to the folder, possibly overwriting an existing file mv <file> <foldername>
Copy a file to a folder cp <file> <foldername>
Copy/Duplicate a file in the same directory cp <file> <newFileName>

Directory Navigation

Description Command
We use it to navigate and change directories cd
Print (current) working directory pwd
Change directory, e.g. cd Documents cd [folder_name]
Root Directory cd ~
Previous directory or folder you last browsed cd -
Move up to the parent directory cd ..
Move up two levels cd ../..
MAC Users: Open a finder window at current location open .
WINDOWS: Open a File Explorer at the current location start .
Open a URL open http://www.google.com
Open a file in your default text editor open -e <filename>

List Directory Contents

Description Command
list files in the current directory ls
list will show size, modified date and time, permissions. ls -l
list all files including hidden files starting with ‘.‘ ls -a
list shows sizes in human readable format ls -lh
list files and directories in reverse order ls -lr
list will display very long listing directory trees ls -R
list will show latest modified file or directory date as last ls -ltr
list file size in order, will display big in size first ls -lS
output the contents of a file cat <filename>
list all running processes ps
terminate existing process kill <process>

GIT Cheat Sheet

Cloning a repo with Git

Cloning repo to your local computer git clone <ssh-url>

Sync/Fetch from repo to local computer repo

git pull

Sync local branch with remote

Push branch to remote git push origin <branch>
Sync local branch with remote git push -u origin <new-branch>

Working with branches

Create and Switch to new local branch git checkout -b <new-branch>
Checkout a particular branch git checkout <branch>
Deletes local branch git branch -d <branchname>

Undo last commit

git reset --soft HEAD-1
Reset will rewind your current HEAD branch to the specified revision. In our example above, we'd like to return to the one before the current revision - effectively making our last commit undone.

Note the --soft flag: this makes sure that the changes in undone revisions are preserved. After running the command, you'll find the changes as uncommitted local modifications in your working copy.

If you don't want to keep these changes, simply use the --hard flag. Be sure to only do this when you're sure you don't need these changes anymore.

git reset --hard HEAD-1

Get diffstat of uncommitted work

git diff --stat HEAD

inspect a specific commit

Show commit info, diffstat and patch, you can replace 'HEAD' with the hash belonging to the commit you would like to inspect. git show HEAD

Show list of commits

Long version git log
Short oneline version git log --oneline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment