Skip to content

Latest commit

 

History

History
232 lines (192 loc) · 4.21 KB

git.md

File metadata and controls

232 lines (192 loc) · 4.21 KB

Git

Enable diffing of untracked files

git add -N
git add --intent-to-add

Get the history of specific lines from a file

git blame -L <lines_range> <commit> -- <path>

Change the remote a branch is tracking

git branch <branch> -u <remote>/<branch>

Start tracking a remote branch

git branch --set-upstream-to=<remote>/<branch>

Stop tracking a remote branch

git branch --unset-upstream

Get specific files from another branch

git checkout <branch> -- <paths>

Check out any of the previous branches

git checkout @{-<number>}

Remove local (untracked) files

git clean -f -d

Edit the previous commit message

git commit --amend -m "New message"

Add alias

git config --global alias.<name> <command>

List all aliases

git config --get-regexp alias

Show only the staged changes

git diff --staged

Compare two branches by their tips

git diff <branch-1>..<branch-2>

See the files affected by a commit

git diff-tree -r --name-only --no-commit-id <commit>

List remote branches with last committer sorted by date

git for-each-ref --format='%(committerdate)%09%(authorname)%09%(refname)' | \
sort -k5n -k2M -k3n -k4n | grep remotes | \
awk -F "\t" '{ printf "%-32s %-27s %s\n", $1, $2, $3 }'

Log all commits not reachable from master

git log <branch> --not master

Log commits with file changes stats

git log --stat

Search for changes containing given string

git log -S '<search string>'

Log only merge commits

git log --merges

Log commits with changed files

git log --name-only

Succint log

git log --graph --oneline --decorate

Log with diff

git log --full-diff -p   

Squash all branch commits into one and merge them into current branch

git merge --squash <branch>

Delete a remote branch

git push origin --delete <branch>
git push origin :<branch>

Push a new local branch to a remote

git push -u origin <branch>

Push local branch to a different remote branch and track it

git push -u <remote> production:master

Rebase from the common ancestor

git rebase --onto master <ancestor_branch> <branch>

Show the history of the local branch

git reflog --date=local <branch>

Use new remote repository

git remote add <remote> git://path/to/repo.git
git fetch <remote>
git checkout --track <remote>/<branch>

Show remote information (including URL)

git remote show origin

Change the remote's URL

git remote set-url origin [email protected]/USERNAME/OTHERREPOSITORY.git

Update the remote branches list

git remote prune origin

Undo a merge inside a dirty working tree

git reset --merge

Unstage a file

git reset <file>

Force pull a rebased branch ignoring the local state

git fetch
git reset --hard origin/<branch>

Show the last commit matching query string

git show :/query

Show branches containing given SHA

git branch --contains <SHA>

Temporarily ignore file changes

git update-index --assume-unchanged <file>

One-commit feature branch workflow

git fetch --all -p
git checkout origin/master
git commit -m’changes’
git push origin HEAD:refs/heads/the-feature

Changing author info in the repository history

git filter-branch --env-filter '
OLD_EMAIL="[email protected]"
CORRECT_NAME="Marek Grzybek"
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

git push --force --tags origin 'refs/heads/*'

Links