Skip to content

Git Commands

David Beechey edited this page Oct 18, 2024 · 1 revision

Initial setup

This is only necessary the first time you need to download the repo to a new device

git clone <ssh-url> - download the repo at <ssh-url> from github

(n.b. throughout this page wherever you see <text> you replace all of <some-text> with some argument (< and > included))

e.g. to clone the hyped-2024 repo you run [email protected]:Hyp-ed/hyped-2024.git

When you start a new piece of work

  1. git checkout master - checkout the main branch of the repo
  2. git pull - downloads changes from github to bring your local copy of the repo up to date with the remote repo
  3. git branch <name-of-branch> - create a new branch named <name-of-branch>
  4. git checkout <name-of-branch> - move to the branch you just created

When you are working on a branch

  1. git add <files> - stages the files to be included in the next commit (note that you can use . to stage all files in your present working directory)
  2. git commit -m "<commit-message>" - create a new commit. <commit-message> should be a description of what that commit changes
  3. git push - upload your changes to github
  4. The first time you work on a branch: open a PR on github
  5. Wait for reviews on your PR
  6. Fix issues raised in reviews
  7. Repeat until your PR is approved
  8. Click "Squash and Merge" on your PR

Other useful commands

git status - lists unstaged changes, staged changes, and the difference between your local branch and the remote branch

git log - lists all previous commits

git restore --staged <files> - unstages added changes that have not yet been committed

git revert HEAD~<n> - creates a new commit that undoes the previous <n> commits

git diff HEAD~<n> - gives the difference between the head of the branch and the commit <n> commits ago

(n.b. in each place I use <HEAD~<n>> you can also use a commit ID, which is found using git log)

Danger zone

These commands are fine to use if you haven't pushed the offending commits to github, if you have then you shouldn't use these

git reset HEAD~<n> --soft - deletes last <n> commits from your branch, keeps the changes they made to files staged

git reset HEAD~<n> --hard - deletes last <n> commits from your branch, and removes the changes to the files

git rebase -i - interactive rebase, lets you change the historical commits on your branch. This has more options than I can document here, and if you use it you are responsible for the mess you make