Configuration

git config --global user.email "[email protected]"
git config --global user.name "Daniele Salvagni"
git config --global core.editor nano
git config --global help.autocorrect 1

Basic commands

Remote

git remote add <alias> <url>  # add git URL as an alias
git remote show <alias>       # show information about remote
git fetch <alias>             # fetch down all the **branches** from that remote
git merge <alias>/<branch>    # merge a remote branch into current branch
git merge --abort             # abort the merge
git push <alias> <branch>     # transmit local branch to remote repository
git pull                      # fetch and merge any commits from remote

Stash

git stash             # save modified and staged changes
git stash -u          # include untracked files
git stash list        # list stack-order of stashed file changes
git stash pop         # write working from top of stash stack
git stash drop [id]   # discard the changes from top of stack or by [id]

# alternative to include a message
git stash save "your stash message"

Remove an ignored file from git

This will remove a file from git that has been added to .gitignore while keeping it in the working directory

git rm --cached <file>

Branches

git branch     # local branches
git branch -r  # remote branches
git branch -a  # all branches
git branch -av # all branches with details
git checkout -b <branch>  # create and switch to a new branch
git checkout -            # switch to the last branch
git branch -d <branch>    # delete a branch
git switch <branch>       # switch to a branch, look for remote

Tagging

git tag -a v1.0.0 -m "The first official version"
git show v1.0.0
git describe --tags

git push --tags
git push origin v1.0.0  # same effect

Rewrite history

Apply any commits of current branch ahead of specified one

git rebase <branch>

Clear staging area, rewrite working tree from specified commit

git reset --hard <commit>
git clean -df  # to remove untracked files

Amend the last commit

git commit --amend -m "New commit message"

git add forgotten_file
git commit --amend --no-edit

Advanced commands

Backup untracked files

git ls-files --others --exclude-standard -z |\
xargs -0 tar rvf ~/backup-untracked.zip

Sync with remote, overwrite local changes

git fetch origin && git reset --hard origin/master && git clean -f -d

Undo last commit but keep changed files in stage

uncommit = reset --soft HEAD~1

Reset a single file

To reset both the working copy and its state in the Git index to that of HEAD:

git checkout HEAD -- file.txt

Rebase local branch to remote master

git pull --rebase origin master

or

git fetch origin
git rebase origin/master

Counting commits

git shortlog -s -n

Overview

Git Overview