Git Commands | |
---|---|
Initialize a local Git repository | git init |
Check status | git status |
Add a file to the staging area | git add [file-name.txt] |
Commit changes | git commit -m “[commit message]” |
View changes | git log |
View changes (detailed) | git log –summary |
Add a remote repository | git remote add origin git@github.com/joshnh/[repository-name].git |
Set a repository’s origin branch to SSH | git remote set-url origin ssh://git@github.com/joshnh/[repository-name].git |
Push changes to remote repository (first time) | git push -u origin master |
Push changes to remote repository (subsequent times) | git push |
Pull changes from remote repository | git pull origin master |
Create a local copy of a remote repository | git clone ssh://git@github.com/joshnh/[repository-name].git |
All-purpose git command line to include directly to your alias list
1 |
for k in `git branch|perl -pe s/^..//`;do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k|head -n 1`\\t$k;done|sort -r |
Show git branches by date – useful for showing active branches
Print out list of all branches with last commit date to the branch, including relative time since commit and color coding
1 |
git commit --amend |
Add forgotten changes to the last git commit
It’s pretty common to forgot to commit a files, be it a modification, or a brand new file.
1 |
git log --format='%aN' | sort -u |
Show ALL branches
for k in git branch -r|awk '{print $1}'
;do echo -e git show --pretty=format:"%Cgreen%ci_%C(blue)%c r_%Cred%cn_%Creset" $k|head -n 1
$k;done|sort -r|awk -F”_” ‘{printf(“%s %17s %-22s %s\n”,$1,$2,$3,$4)}’
Get all git commits of a specific author in a nice format
git log –name-status –author=”[The Author’s Name]”
List all authors of a particular git project
1 |
LC_ALL=C tr -c "[:digit:]" " " < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR="1;32" grep --color "[^ ]" |
Matrix Style
1 |
git reflog show | grep '}: commit' | nl | sort -nr | nl | sort -nr | sed s/commit://g | sed -e 's/HEAD*@{[0-9]*}://g' |
show git commit history
1 |
find . \( -type d -empty \) -and \( -not -regex ./\.git.* \) -exec touch {}/.gitignore \; |
Add .gitignore files to all empty directories recursively from your current directory
1 |
git diff --stat `git log --author="XXXXX" --since="12 hours ago" --pretty=oneline | tail -n1 | cut -c1-40` HEAD |
Figures out what has changed in the last 12 hours
1 |
stat -f '%Sp %p %N' * | rev | sed -E 's/^([^[:space:]]+)[[:space:]]([[:digit:]]{4})[^[:space:]]*[[:space:]]([^[:space:]]+)/\1 \2 \3/' | rev |
enable color git
git config –global –add color.ui true
Git Tree Command with color and tag/branch, add this to ~/.gitconfig
1 |
git log --graph --oneline --all --decorate --color |
list of commits (simile gitk)
1 |
find ~ -maxdepth 2 -name .git -print | while read repo; do cd $(dirname $repo); git pull; done |
git pull all repos
1 |
for k in `git branch|sed s/^..//`;do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" "$k"`\\t"$k";done|sort |
Show git branches by date – useful for showing active branches
1 |
git remote -v | grep fetch | sed 's/\(.*github.com\)[:|/]\(.*\).git (fetch)/\2/' | awk {'print "https://github.com/" $1'} | xargs open |
Open the current project on Github
1 |
echo .DS_Store >> ~/.gitignore |
ignore .DS_Store forever in GIT
echo .DS_Store >> ~/.gitignore
git remove files which have been deleted
git add -u
Show GIT stats
git log –stat
diff’s on files
git diff –color-words file1 file2
Show git branches by date – useful for showing active branches
git for-each-ref –sort=-committerdate –format=”%1B[32m%(committerdate:iso8601) %1B[34m%(committerdate:relative) %1B[0;m%(refname:short)” refs/heads/
Number of commits per day in a git repository
git log | grep Date | awk ‘{print ” : “$4” “$3” “$6}’ | uniq -c
remove .DS_Store from repo
1 |
git log --reverse --pretty=oneline | cut -c41- | nl | sort -nr |
A nice way to show git commit history, with easy to read revision numbers instead of the default hash
Further more:
http://cheat.errtheblog.com/s/git
If you have a really cool one, please add it!
Source: commandlinefu.com
Plattform Test: Tested PASSED: on Snow Leopard
Git Version: git version 1.7.5.4