Skip to content

Git Basics

Alex Verkerk edited this page Mar 12, 2021 · 1 revision

How to git good

The basics

(I will be generalising so don't worry about full technicalities)

The very basic local/remote structure

  • GitHub (plus GitLab, BitBucket and others) host remote repositories.
  • You can "copy" (clone) a remote repository, which sets it as a remote for your local repo.
  • Locally, you actually have two repositories:
    • Your local working repository
    • Your local copy of the remote repository (this is called origin)
  • git pull first runs git fetch origin/<branchname> to get changes from the remote, then runs git merge origin/<branchname> to merge remote changes into your local working repository
  • Locally, there are 5 different states that files can be in:
    • Committed
      • This is when all changes to a file are part of a commit
    • Staged
      • This is when a file has been added to "the staging area". Basically, you have run git add and are signalling the file is ready to commit
    • Unstaged
      • The state of a file you are working on that has previously been committed
    • Untracked
      • Basically a new file that hasn't been added to git yet
    • Ignored
      • A file that there is specific rule in the .gitignore file for, and will not be picked up by git

Commands

There are some basic commands you need to know:

  • git config
    • Generally you use this command once at the start of installing git, when you do the following:
      • git config --global user.name "My Name"
      • git config --global user.email "my@email.com"
  • git init
    • This is useful for initialising local git repos, however, it's usually easier to make a remote repo in github and clone it
  • git clone <repo link>
    • This is used to copy a remote repository down to your device
    • For https: [https://github.com/<your username>/<your repo name>.git]
    • For ssh (not for beginners): [git@github.com:<your username>/<your repo name>]
  • git status
    • This command is used to see what the state of your local repository looks like
    • This will show committed, staged, and unstaged files, as well as new files
  • git add <file path>
    • This command is often used with a . in the place of the file path, which just adds all files in the current folder into staging
    • You can used this command to specifically ignore some files when committing by only adding some files to your commit
  • git commit
    • By default, this will open up your editor of choice (ie. nano, vim, VSCode) and ask you to enter a commit message and save the file
    • You can also add flags to this command, which can be chained (ie. git commit -am "Initial Commit")
      • -a will stage all unstaged files
      • -m will allow you to write a message inline with the command
  • git push
    • This will allow you to push your committed changes up to your remote repository
    • If you're working off a new local branch, use push -u origin <branch name> to setup a branch in your remote repository and push to that
  • git pull
    • This will fetch down changes from your remote repository into your local copy of the remote, and then merge that into your working repository
  • git fetch
    • This will fetch down any changes into your local copy of the remote repo (origin)
    • Useful for checking out if any changes on your local main/master conflict with the remote if others have been working on it
  • git merge
    • This allows you to merge changes from one branch into another
    • Useful when changes have been made to master and you want to bring your branch up to date
  • git branch
    • Basically just lists all your branches. Use -a to show remote branches too
    • Use -D <branchname> to delete a local branch that has not been merged in the remote, and -d <branchname> to delete a local branch that has been merged in the remote
  • git checkout
    • Use this to both switch between branches and to make new branches
      • git checkout <branchname> to move to that branch
      • git checkout -b <new branchname> to create a branch and automatically switch to it
  • git log
    • This command will show you all the past commits for you branch
  • git stash
    • This will "stash" all your changes in the current repo, and remove them from current state. Useful if you want to save unfinished work and switch branches
    • The stash is saved as a stack following the Last-In-First-Out principle
    • You can view specific stash items by git stash show@{<index of stash item>}
  • git stash list
    • This will list stash items
  • git stash apply
    • This will add the top changes from the stash back to the working directory, without removing them from the stash
    • You can specify a stash item to apply (ie. git stash apply stash@{1})
  • git stash pop
    • This will apply stashed changes, and remove those changes from the stash
    • Again, you can do this for specific positions with git stash pop stash@{<stash index>}
  • git reset
    • Dangerous: this will reset back past commits and can be potentially destructive
    • You can use several options, but the most common are:
      • --soft to keep all the changes in the commits you're resetting past
      • --hard to hard wipe back to the commit you're resetting to
    • Usage: git reset <commit to reset to>
      • This can be a specific commit hash
      • Or you can use HEAD^ to revert your latest commit
      • Or you can use HEAD~<number of commits to rollback> to go a certain number of commits backward
  • git remote
    • Use git remote -v to list all your remotes
    • This will be covered more in the Fork-and-Pull section

Clone this wiki locally