We're hiring!
*

Quick hack: git reset upstream

Robert Foss avatar

Robert Foss
August 27, 2018

Share this post:

Reading time:

Working with a git based project that has a defacto upstream repository means that you perioducally want to fetch the canonical master branch. This can be simplified with a .gitconfig alias.

robertfoss@xps9570 ~/work/libdrm $ git ru
remote: Counting objects: 234, done.
remote: Compressing objects: 100% (233/233), done.
remote: Total 234 (delta 177), reused 0 (delta 0)
Receiving objects: 100% (234/234), 53.20 KiB | 939.00 KiB/s, done.
Resolving deltas: 100% (177/177), completed with 36 local objects.
From git://anongit.freedesktop.org/mesa/drm
   cb592ac8166e..bcb9d976cd91  master     -> upstream/master
 * [new tag]                   libdrm-2.4.93 -> libdrm-2.4.93
 * [new tag]                   libdrm-2.4.94 -> libdrm-2.4.94

The idea here is that we by issuing a single short command can fetch the latest master branch from the upstream repository of the codebase we're working on and set our local master branch to point to the most recent upstream/master one.

This works by looking for a remote called upstream (or falling back to origin if it isn't found). And resetting the local master branch to point at the upstream/master branch.

~/.gitconfig

Add this snippet under the [alias] section of your ~/.gitconfig file.

[alias]
    ru = "!f() { \
        REMOTES=$(git remote); \
        REMOTE=\"origin\"; \
        case \"$REMOTES\" in \
            *upstream*) \
                REMOTE=\"upstream\"; \
                ;; \
           esac; \
        git fetch $REMOTE; \
        git update-ref refs/heads/master refs/remotes/$REMOTE/master; \
        git checkout master >/dev/null 2>&1; \
        git reset --hard $REMOTE/master >/dev/null 2>&1; \
        git checkout - >/dev/null 2>&1; \
    }; f

If you have a closer look, you'll notice that the upstream remote is used if has been added, otherwise the origin remote is used. This selection is done using git running a shell script.

Example

This is what git ru might look like when used.

robertfoss@xps9570 ~/work/libdrm $ git remote -v
origin  git@gitlab.collabora.com:robertfoss/libdrm.git (fetch)
origin  git@gitlab.collabora.com:robertfoss/libdrm.git (push)
upstream    git://anongit.freedesktop.org/mesa/drm (fetch)
upstream    git://anongit.freedesktop.org/mesa/drm (push)

robertfoss@xps9570 ~/work/libdrm $ git log --pretty=log --abbrev-commit
cb592ac8166e - (HEAD -> master, upstream/master, tag: libdrm-2.4.92) bump version for release (4 months ago) 
c5a656818492 - freedreno: add fd_pipe refcounting (4 months ago) 
1ac3ecde2f2c - intel: add support for ICL 11 (4 months ago) 
bc9c789073c8 - amdgpu: Deinitialize vamgr_high{,_32} (4 months ago) 
[snip]

robertfoss@xps9570 ~/work/libdrm $ git ru
remote: Counting objects: 234, done.
remote: Compressing objects: 100% (233/233), done.
remote: Total 234 (delta 177), reused 0 (delta 0)
Receiving objects: 100% (234/234), 53.20 KiB | 939.00 KiB/s, done.
Resolving deltas: 100% (177/177), completed with 36 local objects.
From git://anongit.freedesktop.org/mesa/drm
   cb592ac8166e..bcb9d976cd91  master     -> upstream/master
 * [new tag]                   libdrm-2.4.93 -> libdrm-2.4.93
 * [new tag]                   libdrm-2.4.94 -> libdrm-2.4.94

robertfoss@xps9570 ~/work/libdrm $ git log --pretty=log --abbrev-commit
cb9d976cd91 - (HEAD -> master, upstream/master) xf86drm: fallback to normal path when realpath fails (4 hours ago) 
8389c5454804 - (tag: libdrm-2.4.94) Bump to version 2.4.94 (19 hours ago) 
f0c642e8df41 - libdrm: add msm drm uapi header (25 hours ago) 
[snip]

Thanks

This post has been a part of work undertaken by my employer Collabora.


Visit Robert's blog.


Add a Comment






Allowed tags: <b><i><br>Add a new comment:


Search the newsroom

Latest Blog Posts

Building a Board Farm for Embedded World

27/06/2024

With each board running a mainline-first Linux software stack and tested in a CI loop with the LAVA test framework, the Farm showcased Collabora's…

Smart audio filters with WirePlumber 0.5

26/06/2024

WirePlumber 0.5 arrived recently with many new and essential features including the Smart Filter Policy, enabling audio filters to automatically…

The latest on cmtp-responder, a permissively-licensed MTP responder implementation

12/06/2024

Part 3 of the cmtp-responder series with a focus on USB gadgets explores several new elements including a unified build environment with…

A roadmap for VirtIO Video on ChromeOS: part 3

06/06/2024

The final installment of a series explaining how Collabora is helping shape the video virtualization story for Chromebooks with a focus…

Hacking on the PipeWire GStreamer elements

05/06/2024

Last week I attended the GStreamer spring hackfest in Thessaloniki to work on the PipeWire GStreamer elements and connect with the community.

Transforming speech technology with WhisperLive

28/05/2024

The world of AI has made leaps and bounds from what It once was, but there are still some adjustments required for the optimal outcome.…

Open Since 2005 logo

Our website only uses a strictly necessary session cookie provided by our CMS system. To find out more please follow this link.

Collabora Limited © 2005-2024. All rights reserved. Privacy Notice. Sitemap.