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 [email protected]
Change current repo name and email:
git config user.name "smutnyleszek"
git config user.email "[email protected]"
Colors in terminal for Git:
git config --global color.ui true
Turn on case-sensitive file name changes:
git config core.ignorecase false
Changin a remote url in config:
git remote set-url origin https://github.com/foo.git
New project
git init
git add .
git remote add origin URL
git commit -m "initial commit"
git push origin BRANCHNAME
Clone project
git clone URL [DIRECTORY]
Self-explanatary
git status
git add PATH
git rm PATH
git mv OLDFILE NEWFILE
Create commit and push it to remote
git commit -m "Message"
git push origin BRANCHNAME
Good 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~1
Show differences:
git diff
Revert to current HEAD:
git reset --hard HEAD
Check if file is being tracked by git
git ls-files FILENAME --error-unmatch
Create fresh repo clone:
git clone --bare https://github.com/user/repo.git
cd repo.git
Rewrite history:
#!/bin/sh
git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="Your Correct Name"
CORRECT_EMAIL="[email protected]"
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 --tags
Push corrected history:
git push --force --tags origin 'refs/heads/*'
To checkout your own version:
git checkout --ours -- FILE
git add FILE
To checkout the other version:
git checkout --theirs -- FILE
git add FILE
Create a branch
git branch BRANCHNAME
Switch to a branch
git checkout BRANCHNAME
List branches: local, remote or all
git branch
git branch -r
git branch -a
List branches with sha1
and last commmit
git branch -v
Delete branch
git branch -d BRANCHNAME
Delete branch on remote
git push origin --delete BRANCHNAME
Refresh list of remote branches
git remote update origin --prune
Make local branch track remote branch from origin
git branch -u origin/BRANCH BRANCH
Rename current branch
git branch -m NEWNAME
Rename current branch on remote
git push origin --delete OLDNAME
git push --set-upstream origin NEWNAME
Merge current branch with BRANCHNAME
git merge BRANCHNAME
Merge current branch with BRANCHNAME with only one output commit
git merge --squash BRANCHNAME
git commit -m "EXPLANATION"
git push
Send commit to branch
git push origin BRANCHNAME
Show log with or without default pager:
git log
git --no-pager log
Show only a range of history
git log OLDER_SHA..NEWER_SHA
Some useful flags:
--graph
- show branching and merging--oneline
- commit info in one line