Some reference notes of the most common used commands.
Contents:
Set up new global name and email:
git config --global user.name "John Doe"
git config --global user.email johndoe@example.comChange current repo name and email:
git config user.name "smutnyleszek"
git config user.email "smutnyleszek@gmail.com"Colors in terminal for Git:
git config --global color.ui trueTurn on case-sensitive file name changes:
git config core.ignorecase falseChangin a remote url in config:
git remote set-url origin https://github.com/foo.gitNew project
git init
git add .
git remote add origin URL
git commit -m "initial commit"
git push origin BRANCHNAMEClone project
git clone URL [DIRECTORY]Self-explanatary
git status
git add PATH
git rm PATH
git mv OLDFILE NEWFILECreate commit and push it to remote
git commit -m "Message"
git push origin BRANCHNAMEGood commit message template:
A short summary in max 72 characters.
- first done thing after a blank space in max 72 characters
- second done thing in max 72 characters
Undo git commit before push
git reset --soft HEAD~1Show differences:
git diffRevert to current HEAD:
git reset --hard HEADCheck if file is being tracked by git
git ls-files FILENAME --error-unmatchCreate fresh repo clone:
git clone --bare https://github.com/user/repo.git
cd repo.gitRewrite history:
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="your-old-email@example.com"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="your-correct-email@example.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tagsPush corrected history:
git push --force --tags origin 'refs/heads/*'To checkout your own version:
git checkout --ours -- FILE
git add FILETo checkout the other version:
git checkout --theirs -- FILE
git add FILECreate a branch
git branch BRANCHNAMESwitch to a branch
git checkout BRANCHNAMEList branches: local, remote or all
git branch
git branch -r
git branch -aList branches with sha1 and last commmit
git branch -vDelete branch
git branch -d BRANCHNAMEDelete branch on remote
git push origin --delete BRANCHNAMERefresh list of remote branches
git remote update origin --pruneMake local branch track remote branch from origin
git branch -u origin/BRANCH BRANCHRename current branch
git branch -m NEWNAMERename current branch on remote
git push origin --delete OLDNAME
git push --set-upstream origin NEWNAMEMerge current branch with BRANCHNAME
git merge BRANCHNAMEMerge current branch with BRANCHNAME with only one output commit
git merge --squash BRANCHNAME
git commit -m "EXPLANATION"
git pushSend commit to branch
git push origin BRANCHNAMEShow log with or without default pager:
git log
git --no-pager logShow only a range of history
git log OLDER_SHA..NEWER_SHASome useful flags:
--graph- show branching and merging--oneline- commit info in one line