You are not logged in.

#1 2020-05-18 02:31:42

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,111
Website

Some git tips

There are plenty of git tutorials Out There, and advanced users know the right web search terms to use...

This thread is for things that new or intermediate users have found that made their gitlife easier, and want to share. Of course there's no lack of such stuff, but it does no harm to have some here too.

First, to get it here in the same place, a nice terminal prompt I posted long ago. It immediately tells you what branch you're on, and if there is anything to commit. Makes my life easier every day:
https://forums.bunsenlabs.org/viewtopic … 060#p71060

More to follow with any luck...

Last edited by johnraff (2020-05-18 06:44:13)


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#2 2020-05-18 06:12:24

ohnonot
...again
Registered: 2015-09-29
Posts: 4,753
Website

Re: Some git tips

^ the git package itself includes some sort of git prompt function, but I have no clue how good it is.

My most recent git revelations have been happening here.


BL quote proposals to this thread please.
how to ask smart questions | my repos / my repos | my blog
---
Thank you for posting direct image links!

Offline

#3 2020-05-18 06:51:06

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,111
Website

Re: Some git tips

That built-in git prompt is what I'm using, and referring to in the link. It works pretty well for me.

And thanks for the thoughts on dotfiles - some nice hints there.

Agree about meld - I use it all the time, both for file and whole directory comparisons & merges. It really helps with little things you'd otherwise likely have missed.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#4 2020-06-08 05:57:33

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,111
Website

Re: Some git tips

Everyone has their git aliases, but mine definitely save me some time, especially when working on a Debian package in its own git repo (like the BL packages). In fact these are functions, which are more powerful than plain aliases, but they still go in ~/.bash_aliases.

First, I'd definitely recommend making an ssh keypair and using that to log in instead of a password. More secure, and if you use ssh-agent it's less work too. (Not often that happens.)  cool
Here's a tutorial:
https://www.digitalocean.com/community/ … -debian-10

ssh-agent should already be running if you're using BunsenLabs or regular Debian, triggered by /etc/X11/Xsession.d/90x11-common_ssh-agent so if you run 'ssh-add' and enter your password the agent will take over for the rest of that session. Anyway, that's why ssh-add is here.

These just save a few keystrokes, nothing special:

gs() {
    git status
}

# gc 'commit message'
gc() {
    git commit -am"$*"
}

# This one's not git-specific, and isn't used by itself, but in later functions.
# You'll only be prompted for a password the first time you use it.
gssh() {
    ssh_identity='/home/<user>/.ssh/id_rsa'
    if ssh-add -l | grep -qw "${ssh_identity}"
    then
        echo 'default ssh key already loaded'
    else
        echo 'adding default ssh key'
        ssh-add "${ssh_identity}" || { echo 'ssh-add failed' >&2; return 1;}
    fi
}

gp() {
    gssh || return 1
    git push || { echo "push failed" >&2; return 1;}
}

I'm using gp all the time.

These functions are only for when you're inside a debian source package repo, but save a lot of typing when you are:

# This one is for just after you've updated debian/changelog with 'dch', 
# but the repo is otherwise up to date.
# It will add a git commit with the new package version, 
# very handy for tracking back later:
gch() {
    version=$(dpkg-parsechangelog --show-field Version)
    case "$(git status --porcelain)" in
    ' M debian/changelog')
        echo "Changelog has been modified.";;
    '')
        echo "No changes to commit."
        return 1;;
    *)
        echo "Some other files have been changed. Commit them first."
        return 1;;
    esac
    git commit debian/changelog -m"Update changelog: ver. $version" || { echo "commit failed" >&2; return 1;}
}

# Add a tag using the latest version, checking first if everything's OK:
gtag() {
    if [[ -n "$(git status --porcelain)" ]]
    then
        echo "Some files have been changed. Commit them first."
        return 1
    else
        echo "All files up to date."
    fi
    latest_commit=$(git log -1 --pretty=%H)
    latest_commit_chlog=$(git log -1 --pretty=%H -- debian/changelog)
    latest_tag=$(git describe --tags)
    version=$(dpkg-parsechangelog --show-field Version)
    echo "Current version in changelog: $version"
    if [[ $latest_commit = "$latest_commit_chlog" ]]
    then
        if [[ $latest_tag = "$version" ]]
        then
            echo "Latest tag is already $latest_tag"
            return 0
        else
            echo "Tagging at latest version change, $version"
            git tag "$version" || { echo "tag failed" >&2; return 1;}
        fi
    else
        echo "Some commits have been made after changelog bump.
Update the changelog first" >&2
        return 1
    fi
}

# Tag (using gtag), and push to remote, again doing some checks:
gtagp() {
    gtag || return 1
    pstatus=$( git status --porcelain --branch )
    branch=$( git rev-parse --abbrev-ref HEAD )
    [[ $pstatus = *\[ahead\ * ]] && {
        echo "Branch $branch is ahead of remote.
Push commits first? (Y/n)"
        read -srn1
        case ${REPLY^} in
        Y|'')
            gp;;
        *)
            return 1;;
        esac
    }
    echo "pushing new tag..."
    gssh || return 1
    git push origin "$version"|| { echo "push $version failed" >&2; return 1;}
}

# All in one - commit changelog bump, push, tag and push tag:
gchp() {
    gch
    gp
    gtagp
}

So the later ones are getting a bit more complicated. I've tried to catch all the corner cases, but please post if you spot any others.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#5 2020-06-10 06:52:48

ohnonot
...again
Registered: 2015-09-29
Posts: 4,753
Website

Re: Some git tips

I'm not sure what the point of gssh is.
Maybe you aren't aware of ssh's "AddKeysToAgent yes" option?
See the Tip under https://wiki.archlinux.org/index.php/SSH_keys#ssh-agent


BL quote proposals to this thread please.
how to ask smart questions | my repos / my repos | my blog
---
Thank you for posting direct image links!

Offline

#6 2020-06-10 07:02:44

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,111
Website

Re: Some git tips

^I prefer not to load ssh keys if I'm not planning to use them that session. gssh just makes sure my key is loaded when it'll be needed. It's not meant to be run directly, but called in a couple of the other functions, like gp right after.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#7 2020-06-10 07:25:55

ohnonot
...again
Registered: 2015-09-29
Posts: 4,753
Website

Re: Some git tips

^ afaics your gssh function does exactly what "AddKeysToAgent yes" does. Quoting from the archwiki article I linked:

make all ssh clients, including git store keys in the agent on first use


BL quote proposals to this thread please.
how to ask smart questions | my repos / my repos | my blog
---
Thank you for posting direct image links!

Offline

#8 2020-06-11 01:07:13

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,111
Website

Re: Some git tips

^At first it looked to me as if that option would only work if the private key was not password protected, but what it does is prompt for password the first time you use ssh? If that's so, then yes gssh is doing the same.

I have code snippets like that in various scripts that use ssh, so they could be dropped out if that option is added to ~/.ssh/config I guess. Thanks for the tip!


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#9 2020-06-11 02:48:36

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,111
Website

Re: Some git tips

ohnonot wrote:

afaics your gssh function does exactly what "AddKeysToAgent yes" does

I've found two small differences when relying on the ssh/config entry to add keys to the agent:
1) After using ssh, so the keys (I have only one in fact) get added, 'ssh-add -l' does not return the path to the key, only the sha256 hash. Use 'ssh-add <path to key>' and  'ssh-add -l'  shows the key path too.
2) All keys are added, whereas my code snippet only adds one specific key.

But neither of these is important in my case, so thanks again.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

Board footer

Powered by FluxBB