Learning Log logo

The Terminal

Git

A version control system that tracks changes in computer files

Frequent fliers

Code Function
pwd view current file path or location
ls view a list of files and folders in a directory
explorer . open the current directory in files (PC)
code . open the current directory in the text editor (PC)
git add file_name stage file for the next commit
git add -A stage all changed files for the next commit
git commit -m "Explanatory message" commit files in staging area to repo with a message

Branches

Branches are used to trial a new feature or design which can then be merged into the main branch or easily deleted/pruned

Code Function
git checkout branch-name switches to the named branch
git checkout -b new-branch-name creates and switches to a new named branch
git branch list the branches in the local repo
git branch -a list all the branches in local and remote repos
git branch -d branch_name deletes the named branch (when abandonning a feature)
git branch -m branch_name new_branch_name rename a branch
git branch -m branch_name new_branch_name rename a branch
git merge branch-name merges commits from the named branch to the current branch
git stash temporarily stores changes so you can switch branches without committing changes and without losing your work
git stash pop apply the changes that were stored
git clone clone a remote repo to your computer (local)
git push origin branch_name push changes from a branch to a remote repo
git remote -v list the remote repos that are connected to your local repo
git fetch downloads any files or commits to the computer
git remote remove origin removes the origin connection

Irregular commands

Code Function
mkdir directory_name make a new directory and name it
ls -a view a list of files and folders in a directory including hidden and system files
rm -r directory-name remove a directory ⚠ THIS IS PERMANENT AND DELETES THE DIRECTORY AND CONTENTS
touch file_name creates a new file (touch index.html)
git status check for new, modified or untracked project files
git log shows a history of the commits to a project
git diff compare the recent changes to the previously/current committed version
cp current-name new-file-name duplicate a file
cat view all the content in a file from the command line
rm file-name ⚠ Permanently deletes the file

Moving directories

Code Function
cd move into a directory
cd .. navigate up one directory
cd ../.. naviage back two directories
cd - return to your previous location

Create a new repo

  1. Go to Github and create a new repository using the green button on the Dashboard page
  2. Copy the SSH of the repo
  3. Go to the Command-Line
  4. cd➡ get to the right local directory
  5. mkdir➡ to create a new directory *only if necessary*
  6. pwd ➡ double check where you are
  7. git init
  8. touch README.md
  9. git ADD -A
  10. git commit -m "First commit"
  11. git branch -M main ➡ renames "master" to "main"
  12. git remote add origin (SSH)
  13. git push -u origin main ➡ "-u" sets 'upstream'

Retreat

Delete commits to return to an old version

  • You can (⚠ PERMANENTLY) delete recent commits until a selected return point using the commit hash
  • git reset --hard 123456 (commit hash sequence)
  • after you've deleted commits, to push changes to GitHib you'll need to command "git push --force-with-lease origin branch_name"

Hints

  • Panic exit the command line:
    "ctrl" + "c"
    "esc" "q"
  • Capitalise the first letter of git messages
  • Chaining commands allows multiple git commands to run simultanesouly or in rapid succession.