Git
From Attie's Wiki
(Difference between revisions)
m |
m |
||
Line 15: | Line 15: | ||
| git checkout ''<tag>'' || changes the working copy to the tag specified | | git checkout ''<tag>'' || changes the working copy to the tag specified | ||
|} | |} | ||
+ | |||
+ | ==Getting the first tag that contains a file== | ||
+ | <source lang="bash"> | ||
+ | #!/usr/local/bin/bash | ||
+ | if [ "$1" == "" ]; then | ||
+ | echo "usage: $0 <filename>" | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | if [ ! -e $1 ]; then | ||
+ | echo "$0: File does not exist ($1)" | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | echo -en "Finding first commit..." | ||
+ | COMMIT=`git log --pretty=oneline "$1" | tail -n 1 | cut -d " " -f 1` | ||
+ | if [ "$COMMIT" == "" ]; then | ||
+ | echo "$0: Error while retrieving commits for file \"$1\"" | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | echo -en "\rFinding tags containing commit $COMMIT..." | ||
+ | git tag -l --contains "$COMMIT" > /tmp/tag_contains | ||
+ | if [ "$?" != "0" ]; then | ||
+ | echo "$0: Error while retrieving tags containing commit \"$COMMIT\"" | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | echo -en "\rFinding first tag that contains $COMMIT..." | ||
+ | git for-each-ref --sort='-*authordate' --format='%(tag)' refs/tags/ | | ||
+ | while read a; do | ||
+ | if [ "$a" == "" ]; then | ||
+ | continue | ||
+ | fi | ||
+ | grep "$a" /tmp/tag_contains > /dev/null | ||
+ | if [ "$?" == "0" ]; then | ||
+ | LAST=$a | ||
+ | else | ||
+ | echo "$LAST" > /tmp/tag_first | ||
+ | break | ||
+ | fi | ||
+ | done | ||
+ | TAG=`cat /tmp/tag_first` | ||
+ | rm /tmp/tag_contains /tmp/tag_first | ||
+ | |||
+ | echo -en "\rFinding tag information..." | ||
+ | INFO=`git show "$TAG" | head -n 3 | tail -n 2` | ||
+ | if [ "$INFO" == "" ]; then | ||
+ | echo "$0: Error retrieving info for tag \"TAG\" (maybe it isn't annotated?)" | ||
+ | exit 1 | ||
+ | fi | ||
+ | |||
+ | echo -en "\r\0033[K" | ||
+ | echo -e "File:\t\t$1" | ||
+ | echo -e "First tag:\t$TAG" | ||
+ | echo "$INFO" | sed -re 's/^(.+):[ ]+(.+)$/\1:\t\t\2/' | ||
+ | </source> |
Revision as of 14:08, 30 September 2011
My git cheat sheet! See my svn cheat sheet
command | description |
---|---|
git clone <path> | retrieves the entire git repository to your local disk |
git pull | pulls any new revisions from the original path |
git push | pushes local revisions to the original path |
git tag | lists the avaliable tags |
git checkout <tag> | changes the working copy to the tag specified |
Getting the first tag that contains a file
#!/usr/local/bin/bash if [ "$1" == "" ]; then echo "usage: $0 <filename>" exit 1 fi if [ ! -e $1 ]; then echo "$0: File does not exist ($1)" exit 1 fi echo -en "Finding first commit..." COMMIT=`git log --pretty=oneline "$1" | tail -n 1 | cut -d " " -f 1` if [ "$COMMIT" == "" ]; then echo "$0: Error while retrieving commits for file \"$1\"" exit 1 fi echo -en "\rFinding tags containing commit $COMMIT..." git tag -l --contains "$COMMIT" > /tmp/tag_contains if [ "$?" != "0" ]; then echo "$0: Error while retrieving tags containing commit \"$COMMIT\"" exit 1 fi echo -en "\rFinding first tag that contains $COMMIT..." git for-each-ref --sort='-*authordate' --format='%(tag)' refs/tags/ | while read a; do if [ "$a" == "" ]; then continue fi grep "$a" /tmp/tag_contains > /dev/null if [ "$?" == "0" ]; then LAST=$a else echo "$LAST" > /tmp/tag_first break fi done TAG=`cat /tmp/tag_first` rm /tmp/tag_contains /tmp/tag_first echo -en "\rFinding tag information..." INFO=`git show "$TAG" | head -n 3 | tail -n 2` if [ "$INFO" == "" ]; then echo "$0: Error retrieving info for tag \"TAG\" (maybe it isn't annotated?)" exit 1 fi echo -en "\r\0033[K" echo -e "File:\t\t$1" echo -e "First tag:\t$TAG" echo "$INFO" | sed -re 's/^(.+):[ ]+(.+)$/\1:\t\t\2/'