Git Tips and Tricks
Table of Contents
Git Config Example
1 |
[user] name = John Smith email = jsmith@example.com [color] diff = auto status = auto branch = auto [push] default = simple [alias] co = checkout ci = commit br = branch st = status unstage = reset HEAD -- last = log -1 HEAD |
Revert a Commit
gir --reset soft 'HEAD^'
Show a nice log history
git log --stat --pretty --graph
Push to a remote branch from a different local branch
For example, to push from local sc9
to remote master
:
git push origin sc9:master
Show git diff on Staged File
git diff --cached path/of/filename
Edit your last commit message
git commit --amend -m "New commit message"
Rename a local branch
git branch -m <newname>
Remove a Branch either Locally or Remotely
To delete a local branch:
git branch -d branchname
To remove a remote branch (if you know what you are doing!):
git push origin :branchname
Note - If you get the error:
error: unable to push to unqualified destination: the_remote_branch The destination refspec neither matches an existing ref on the remote nor begins with refs/, and we are unable to guess a prefix based on the source ref. error: failed to push some refs to ‘git@repository_name’
perhaps anyone else has already deleted the branch. try to synchronize your branch list with
git fetch -p
Some command line shortcuts
Add these to your .bashrc
or .profile
1
2
3
4
alias gits='git status'
alias gitd='git diff'
alias gitdc='git diff --cached'
alias gitl='git log --stat --pretty --graph'
Some git aliases and different ways to view logs
Add these to your .gitconfig
1 |
[alias] co = checkout ci = commit br = branch st = status unstage = reset HEAD -- last = log -1 HEAD ll = log --graph --pretty=format:'%Cred%h%Creset -%C(bold yellow)%d%Creset %s %Cgreen(%cr %cd) %Creset %C(blink yellow)%aN %ae%Creset' --abbrev-commit --date=local ll1 = log --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(cyan)%s%C(reset) %C(bold black)— %an%C(reset)%C(bold yellow)%d%C(reset)' --abbrev-commit --date=relative ll2 = log --graph --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(bold blue)%s%C(reset) %C(bold black)— %an%C(reset)%C(bold yellow)%d%C(reset)' --abbrev-commit --date=relative lg1 = log --graph --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(bold blue)%s%C(reset) %C(bold black)— %an%C(reset)%C(bold yellow)%d%C(reset)' --abbrev-commit --date=relative --no-merges lgm = log --graph --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(bold blue)%s%C(reset) %C(bold black)— %an%C(reset)%C(bold yellow)%d%C(reset)' --abbrev-commit --date=local lg2 = log --graph --all --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(blue)%s%C(reset) %C(bold black)— %an%C(reset)' --abbrev-commit log = !"git log --decorate=short" lg = !"git lg1" lgnotes = log --no-merges --format=format:'%C(bold blue)%h%C(reset) - %C(reset) %C(bold blue)%s%C(reset) %C(bold black)— %an%C(reset)%C(bold yellow)%d%C(reset)' --date=relative --abbrev-commit whoops = !"git reset --hard HEAD^" reset-hard = reset origin/master --hard hotfixes = !"git ll master..HEAD" missing = !"git ll HEAD..master" uncommit = reset --soft 'HEAD^' |