Version control with Git: Glossary

Key Points

  • Git is a version control tool; one of many.

  • GitHub is a repository hosting service; one of many.

  • Use version control to store versions neatly, restore previous versions, understand what happened (and why), and always know which is the current version.

Tracking changes with a local repository
  • git init initializes a new repository

  • git status shows the status of a repository

  • Files can be stored in a project’s working directory (which users see), the staging area (where the next commit is being built up) and the local repository (where commits are permanently recorded)

  • git add puts files in the staging area

  • git commit saves the staged content as a new commit in the local repository

  • Always write a log message when committing changes

Looking at history and differences
  • git log shows the commit history

  • git diff displays differences between commits

  • git switch -d recovers previous states of the repo

  • HEAD points to the commit you have checked out

  • master points to the tip of the master branch

  • git tag allows commits to be given a descriptive label

  • git difftool shows changes using your configured diff GUI

Commit advice
  • Commit messages explain why changes were made, so make them clear and concise

  • Follow conventions to give a history that is both useful, and easy to read

  • Only commit files which can’t be automatically recreated

  • List files to ignore by committing a .gitignore file

  • Selectively stage changes to files using git add --patch

  • git switch switches to another branch

  • git switch -c <branch_name> creates a new branch and switches to it

  • git merge <branch_name> merges into current branch

  • Use feature branches for new ideas and fixes, before merging into master

  • Merging does not delete any branches

Undoing changes
  • git restore <file> discards unstaged changes

  • git commit --amend allows you to edit the last commit

  • git revert undoes a commit, preserving history

  • git reset --hard undoes a commit by deleting history

Working from multiple locations with a remote repository
  • Git is the version control system: GitHub is a remote repositories provider.

  • git clone to make a local copy of a remote repository

  • git push to send local changes to remote repository

Collaborating with a remote repository
  • git pull merges remote changes into local branch of repository

  • rebase applies your changes on top of a new base (parent) commit

  • rebasing rewrites history

Pull Requests
  • A fork is a git clone into your (GitHub) account

  • A pull request asks the owner of a repository to incorporate your changes

  • Use issues and GitHub projects to plan your work

  • You can discuss code on both issues and pull requests

Conclusions and further information
  • Use version control whenever possible