Index

Table of contents

GIT

config

configure user globally
git config --global user.email "[email]"
git config --global user.name "[name]"
configure user locally (single repository)
git config user.email "[email]"
git config user.name "[name]"
remember login (unix)
git config --global credential.helper cache
remember login (windows)
git config --global credential.helper wincred
update credential cache timeout for single repo in seconds
git config credential.helper 'cache --timeout=300'
change editor to vim
git config --global core.editor "vim"
change line endings to windows based line endings:
git config --global core.autocrlf false
change branch to push to (gerrit)
git config remote.origin.push HEAD:refs/for/master
show colors
git config --global color.ui auto
config file for ignoring files
.gitignore

git config file

disable most colors
[color]
    ui = false
    branch = false
    diff = false
    interactive = false
    status = false
    log = false

gitattributes

.gitattributes => defining attributes per path

Set the default behavior, in case people don't have core.autocrlf set.
* text=auto
Convert to os native line endings on checkout.
*.c text
*.h text
Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf
Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
possible attributes
text=auto        => The default option. Let git decide.
text eol=crlf    => CRLF endings, even on OSX or Linux.
text eol=lf      => LF endings, even on windows
binary           => binary, do not change line endings

common

update repository with all remote branches
git fetch --all

git clone [URL]
git pull --all
git push --all
clone in specified directory
git clone [URL] [DIRECTORY]
list remotes
document git remote -v
add new remote
git remote add origin [URL]
migrate
git pull --all
git remote set-url origin [NEW_URL]
git push --all
show modified files
git status
show lines changed in files
gir diff
show lines changed in files, ignore whitespace
git diff -w
show lines changed in files, don't use ANSI color codes
git diff --no-color
show lines changed after files have been staged
git status -v
show contents of a single commit
git diff [hash]^..[hash]
git commit, edit message in editor
git commit
commit with message
git commit -m "message"
ammend changes to last commit
git commit --amend
ammend author (name, email)
git commit --amend --reset-author
undo last commit
git reset --hard HEAD~1
rebase last 2 commits
git rebase -i HEAD~2
pull and rebase
git pull --rebase
revert single file
git checkout filename
revert a single file to a specified commit
git checkout [hash] [pah_to_file]
all files (will not delete new files)
git reset --hard
delete untracked files and directories
git clean -fd
force the current branch to the specified commit and drop all commits after it (careful!!!)
git reset --hard [commit]
push to a specific remote branch
git push origin [branch]
force overwrite a specific branch. (only if you know what you are doing)
git push origin development -f
git push origin development --force
show a listing of the latest commits
git log
search through commit messages
git log --grep=[query]
show latest commit message for branch
git show development

Branches

list local branches
git branch
list remote branches
git ls-remote
git branch -r
checkout local or remote branch
git checkout feature/IT-205
create a local branch
git checkout -b [branch]
create branch based on specified local or remote branch
git branch feature/nieuw --track /branch/to/base/on
delete local branch
git branch -d [branch]
delete local and/or remote branch
git push origin --delete [branch]
create remote branch for local branch
git push -u origin feature/smtp
merge [branch] onto current branch
git merge [branch]
abort merge
git merge --abort
create reverse commit of merge
git revert -m 1 [hash]
cherry pick a single commit
git cherry-pick [hash]
resolve merge conflict
git add -all
git commit

Tags

list all available tags
git tag
show only tags matching a pattern
git tag -l "v1.8.5*"
create a simple tag (commit pointer):
git tag v1.4-lw
create an advanced tag with message:
git tag -a v1.4 -m "my version 1.4"
push tag to origin
git push origin TAG_release_16

git stash

stash uncommitted changes
git stash
git stash push
list changes in stash
git stash list
inspect changes in stash
git stash show
apply stash changes to current commit
git stash apply
stash documentation
https://www.git-scm.com/docs/git-stash

ssh keys

generate key without pass phrase
ssh-keygen -t rsa
enable access without login through environment variable (2.3.0)
GIT_SSH_COMMAND="ssh -i [rsa_file]"
export GIT_SSH_COMMAND="ssh -i [rsa_file] -o StrictHostKeyChecking=no"
enable access without login globally or per repo (2.10.0)
git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"
enable access without login through ~/.ssh/config
IdentityFile [file]
use personal access token for github
git clone https://[user]:[token]@github.com/[owner]/[repo].git
sed -i.bak 's|[^/]*@github.com|[user]:[token]@github.com|' $(find -type f -path '*/.git/config')

scripting

checking for changes
if git diff-index --quiet HEAD --; then
    # No changes
else
    # Changes
fi

troubleshooting

how to fix (been root user): cannot open .git/FETCH_HEAD: Permission denied
sudo chown -R $USER .
how to fix "You must edit all merge conflicts and then mark them as resolved using git add"This happens because when fixing a conflict, you removed all code in the patch beeing applied to the branch you are rebasing on.If you are sure you have added all your changes
git rebase --skip