- Distributed Version Control System.
- Working Directory.
- Staging Area.
- Local Repository.
- Remote Repository.
- Origin.
- Upstream.
- HEAD.
git config --global user.name "John Doe"
git config --global user.email [email protected]
git config --global core.editor emacs
NOTE: these values get stored in a .gitconfig
configuration file, under ~/.gitconfig (or ~/.config/git/config) most likely. There may be several such files, by levels of specificity (system, global, local).
Setting Up SSH Keys for GitHub.
git init
git init --initial-branch=main
git add .
git add <path/to/file>
git add <path/to/file> <path/to/other/file> <...>
git add -i <path/to/file>
git commit -m <“The comment goes here.”>
git commit -a
git status
git log
git pull
git push
git checkout <branch / commit>
NOTE: you can add all untracked files to the staging area with git add .
. Also, you can add blocks or sections of a file with
git add --interactive
or git add -i
for short.
git branch <new-branch>
git branch <new-branch> <base-branch>
git checkout -b <new-branch>
git branch --track <new-branch> origin/<base-branch>
git branch <new-branch> <commit-id>
git branch <new-branch> <tag>
git branch -m <current-branch-name> <new-branch-name>
NOTE: there are several ways to create branches off each other. The --track
flag is used to denote basing the new branch off a remote one.
NOTE: say you're working in the dev
branch locally, then you should push origin dev
, i.e., git push origin dev
.
git branch -r
git branch -a
git checkout <branch-to-merge-to>
git merge <branch-to-be-merged>
git branch -d <branch>
git branch -d -r origin/<branch>
git branch -M main
git remote add origin [email protected]:abidanBrito/git-masterclass.git
git remote add origin https://github.com/abidanBrito/git-masterclass.git
git remote add upstream https://github.com/abidanBrito/git-masterclass.git
git push --set-upstream <remote> <branch>
git push -u origin <local-branch>
git push -u origin --all
git push origin --delete <remote>
git remote -v
git remote set-url upstream https://github.com/abidanBrito/git-masterclass.git
git fetch upstream
git remote set-url origin https://github.com/abidanBrito/git-masterclass.git
git diff --<path/to/file> (untracked)
git diff --cached <path/to/file> (staged)
git diff --staged
git diff <branch-a> <branch-b> --<path-to-file>
git diff --name-status <branch-a> <branch-b>
git commit --amend
git commit --amend -m <"Your message goes here">
git reset <path/to/file>
git reset --hard
git reset --hard HEAD~1
git reset --hard <commit-to-reset-to>
git clean -n
git clean -f
git rm --cached -r -n .
git rm --cached -r -q .
git fetch
git fetch <remote> <branch>
git fetch upstream
git fetch origin main:tmp
git rebase tmp
git push origin HEAD:main
git branch -D tmp
NOTE: by default, git fetch
downloads files, references and commits from origin
.
Sometimes you may run into issues when setting up a local repo because of the new "main" default branch name on GitHub. Let's rename the local branch and set its upstream properly.
git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a
git remote -v
cat ./git/config
git repack -a -d --depth=250 --window=250
git fsck
git prune
git gc --prune=now
git gc --aggressive --prune=now
git-prune-packed
git rm -r --cached .
git lfs uninstall
git filter-branch --tree-filter 'rm -f FILENAME' HEAD
git lfs uninstall
git rm .gitattributes
git filter-branch --tree-filter 'rm -f FILENAME' HEAD
git push -f origin develop # rewrites history!
git commit --amend --author="Author Name <[email protected]>"
Alternatively, if you want to reset your email globally, you could do:
git config --global user.email "[email protected]"
git commit --amend --reset-author --no-edit
git rebase HEAD~1 --exec "git commit --amend --no-edit --date 'Mon Mar 14 19:02:52 2022 +0100'"
NOTE: git remote set-head origin -a
fetches and sets origin/HEAD
, so that the local repository has a local reference of what the remote repository considers to be the default branch.