Git: reference

Version control: commits, branches, GitHub and teamwork.

Getting started

Configuration

This is done once on your computer: the name and email will go into every commit.

git config --global user.name "First Last"
git config --global user.email "[email protected]"
git config --list

Creating and cloning

clone copies the whole repository together with its history.

git init                          # a new repository in the folder
git clone https://github.com/user/repo.git

The usual cycle

Status, index, commit

First the changes go into the index (add), then they are recorded in a commit. A commit message should explain what was done and why.

git status                # what has changed
git add file.txt           # add a file to the index
git add .                  # add everything
git commit -m "Add a login form"

Viewing changes

It is useful to look at git diff before every commit.

git diff                   # changes not yet staged
git diff --staged          # already staged
git log --oneline          # history in one line
git show abc1234           # commit details

Branches

Creating and switching

It is convenient to build a new feature in a separate branch so as not to break the main one.

git branch                  # list of branches
git switch -c feature-login # create and switch
git switch main             # go back
git branch -d feature-login # delete after merging

Merging

If the same lines were changed differently, Git will show a conflict: open the file, keep the right version, delete the markers <<<<<<<, =======, >>>>>>> and make a commit.

git switch main
git merge feature-login

Remote repository

push, pull, fetch

The -u flag remembers the branch, and after that it is enough to write just git push.

git remote add origin https://github.com/user/repo.git
git push -u origin main    # push
git pull                   # fetch and merge
git fetch                  # fetch changes only

Pull request

A pull request lets colleagues review the changes before they get into main.

git switch -c fix-typo
git commit -am "Fix a typo in the text"
git push -u origin fix-typo
# next on GitHub: Compare & pull request

Undoing

Reverting changes

revert is safe for shared branches because it does not rewrite history.

git restore file.txt              # discard changes in a file
git restore --staged file.txt     # unstage
git commit --amend -m "New message"   # amend the last commit
git revert abc1234                # a new commit that undoes the old one

Stashing

stash is useful when you need to switch to another branch quickly.

git stash          # stash uncommitted changes
git stash pop      # bring them back

.gitignore

Ignoring files

The .gitignore file lives in the project root. Passwords and keys (.env) must not end up in the repository.

node_modules/
.env
*.log
dist/

Useful commands

Help and search

git help <command> opens the full description.

git help commit
git blame file.txt          # who changed each line
git tag v1.0                # version tag
git log --graph --oneline --all

Learn Git with practice

Want to learn step by step? Your account has 67 lessons organised by chapter (50 core and 17 advanced, you can switch the level at any time) and tasks with code checking.