rescue

Rescued dogs find new purpose hunting giant invasive snails in the Galapagos

Giant African snails have invaded the Galapagos, but two rescued dogs are sniffing them out and helping researchers fix the ecology of the islands.




rescue

Your social media activity can help and hurt animal rescues

When you share a rescue organization's posts or comment on them, it may not always be what's best for the shelter.




rescue

Firefighters rescue small dog from house fire

Bakersfield firefighters pull an unresponsive pup from a fire and use a specialized animal oxygen mask to revive him.




rescue

Download this game to rescue cats in outer space, and save real-life cats at the same time

The student-designed mobile game Galactickitties lets you save space-flying felines and benefit the ASPCA too.




rescue

Crippled rescue dog finds forever home with Clooney family

The troubled life of Nate the terrier gets a Hollywood ending thanks to George and Amal Clooney.




rescue

Tracey and Jon Stewart are expanding their animal rescue farm

Former 'Daily Show' host and family reportedly have purchased a 45-acre farm near their current operation in New Jersey.




rescue

How Bill Nye will rescue us with science

The Science Guy is back, just in time to 'Save the World' with verifiable facts and guest stars like Alton Brown.



  • Arts & Culture

rescue

Introducing Meghan Markle's rescue dogs

Meghan Markle, who just got engaged to Prince Harry and will be joining the royal family, has developed a passion for saving pups in need.




rescue

This rescued bat can't stuff quite enough banana into those ridiculous cheeks

Before she flew back into the wild, Miss Alicia the bat met a fruit and fell in love.




rescue

Miracle puppy rescued from rubble in Bahamas

Trapped, starving dog is rescued weeks after Hurricane Dorian barreled through the Bahamas.




rescue

Rescuers take plastic fork out of sea turtle's nose

The turtle survived, but a video of the ordeal demonstrates why recycling can be a matter of life and death.




rescue

Rescuers save nearly 100 baby birds after Oakland tree collapses

Rescue workers swooped in and saved nearly 100 baby birds when tree in Oakland split and started to topple.




rescue

Hero rescues sea turtle from plastic debris

Watch as a WWE observer releases beautiful olive ridley sea turtle from entrapment.




rescue

When hikers need help, who pays for rescue?

If you run into trouble in the great outdoors, your rescue could come with a hefty price tag — but it all depends on where you are.



  • Wilderness & Resources

rescue

Kitty rescued in Bangladesh after getting stuck in a window

Video from Dhaka, Bangladesh shows a young man climbing out to rescue a kitty stuck in a window.




rescue

Meet Narwhal, the rescue puppy with a tail on his forehead

Narwhal, the healthy and happy unicorn puppy, has a tail growing out of his forehead.




rescue

Dog gear company plays fairy godmother to thousands of animal rescues

Max and Neo sends donation boxes to thousands of rescue groups every year.




rescue

This blind, deaf puppy was just rescued from the snow by a kind delivery driver

A UPS driver thought he saw something in the snow. Turns out it was a tiny, white puppy that had been abandoned.




rescue

Watch these rescue pups pick out their Christmas presents

A Rehoming Center in Ireland celebrates Christmas with adoptable dogs by allowing them to pick their own present from donated toys.




rescue

Dying rescue dog is a police K-9 for a day

Eddie Spaghetti, a dying rescue dog, gets to suit up and be a police K-9 for a day.




rescue

Rescue dogs find help in unlikely places

Businesses help homeless dogs get adopted through beer cans and pizza boxes.




rescue

Rescued baby pig feels the sun for the first time

Bella the pig lost his siblings and even a eye on a factory farm in Queensland, Australia, before he found a safe home.




rescue

They braved a war-torn region to rescue 47 animals from the 'Zoo of Sorrow'

47 animals were saved after a daring mission to a zoo in the Gaza Strip.




rescue

Orangutan reaches out to 'rescue' man

Orangutan extends hand to 'rescue' man in snake-infested water during safari in Borneo.




rescue

Comical rescue kitties defy gravity in 'Pounce'

Photographer Seth Casteel casts his lens on the priceless expressions of rescue cats and kittens bounding and jumping through the air.




rescue

1,000 rescued mutts live the life at Costa Rica's 'Land of the Strays'

'The Land of the Strays' is a dog's paradise.




rescue

What To Look For In A Rescue Cat From An Animal Shelter

If you have decided to get a new cat for your family, why not choose one from your local cat rescue home or animal shelter? There are so many animals that are in desperate need of a loving home.




rescue

GFA-supported Workers Use Boats for Rescue and Relief Efforts in Flood-Hit Kerala

Long-term 'desperate need' likely as thousands lose everything, reports ministry leader appealing for more 'helping hands'




rescue

Best-selling Author Gives Away Free Copies of Her New Book 'Rescue Your Retirement'

Financial Investigator Pamela Yellen Reveals Wealth-Killing Traps of 401(k)s, IRAs and Roth Plans — and How to Avoid Them




rescue

Mutual Rescue™ Launches National Film Festival Program To Benefit U.S. Shelters and Animal Welfare Groups

Starting in Colorado and Hawaii, Mutual Rescue launched film festivals, now seeks additional partnerships




rescue

Working Dog Rescue Group Launches Online Press Kit

Mission K9 Rescue saves working dog lives, features efforts in new online press kit




rescue

Mutual Rescue™ Founder Authors Book About How Adopting Animals Transforms Human Lives

Carol Novello is the author of "Mutual Rescue: How Adopting a Homeless Animal Can Save You, Too"




rescue

CBD Oil Manufacturer Named "Best" for Pets Partners with Working Dog Rescue Group

Receptra Naturals Teams Up with Mission K9 Rescue to Make a Difference for Retired Working Dogs




rescue

Mutual Rescue™ Book Released In Paperback Just In Time for Those Looking for a Great Quarantine Read

"Mutual Rescue: How Adopting a Homeless Animal Can Save You, Too" launches in paperback April 2020




rescue

RescueHer - Manufacturer of Luxury Rescue Kits, High-End Cosmetic and Personal Items Expand their Retail Business to Walmart.com and Jet.com

The ultimate rescue kits and products for women. Always prepared for any occasion!




rescue

SBI prepares Rs 20,000 cr bailout plan to rescue Yes Bank

SBI prepares Rs 20,000 cr bailout plan to rescue Yes Bank





rescue

Ohio man rescued after falling 60 feet at Grand Canyon

A 47 year-old Ohio man was rescued after falling 60 feet while hiking in the Grand Canyon yesterday. https://www.nps.gov/grca/learn/news/grand-canyon-rescue.htm




rescue

Woman Rescued After Fall at Grand Canyon National Park

Late this afternoon, park rangers rescued a 38 year old woman who had fallen approximately 50 feet near a popular view point in Grand Canyon National Park. https://www.nps.gov/grca/learn/news/news-2009-06-04-rescue.htm




rescue

Man Rescued After Fall at Grand Canyon National Park

Man rescued after fall in Grand Canyon. https://www.nps.gov/grca/learn/news/man-rescued-after-fall-at-grand-canyon-national-park.htm




rescue

Grand Canyon Resident Rescued After Fall

A resident of Grand Canyon National Park was rescued via short-haul this morning after suffering a fall during a recreational climb of Newton Butte. https://www.nps.gov/grca/learn/news/news-2009-07-07-short-haul.htm




rescue

Stranded Boaters Rescued, their Boat Freed in Grand Canyon

A private boating party stranded near Crystal Rapid in Grand Canyon National Park was able to continue down the river late Thursday afternoon after being rescued by National Park Service rangers. https://www.nps.gov/grca/learn/news/2010-02-26_rescue.htm




rescue

Man Rescued after Falling over Edge of Grand Canyon

At approximately 2:45 p.m., the Grand Canyon Regional Communications Center received a report that a man had fallen over the edge near Mather Point on the South Rim of Grand Canyon National Park https://www.nps.gov/grca/learn/news/5aug10-3_news.htm




rescue

Man Who Fled Rangers Rescued from Below Rim in Grand Canyon National Park

https://www.nps.gov/grca/learn/news/2011-04-22_over-edge.htm




rescue

Rangers Employ Technical Rescue Techniques to Retrieve Body from Below Rim

At approximately 10:45 a.m. today, rangers retrieved the body of a young male from below the South Rim of Grand Canyon National Park. https://www.nps.gov/grca/learn/news/2011-05-06_retrieval.htm




rescue

Man Rescued from Below Rim of Grand Canyon National Park

https://www.nps.gov/grca/learn/news/2011-07-25_fall.htm




rescue

Man Rescued After Fall in Grand Canyon National Park

https://www.nps.gov/grca/learn/news/2012-09-04_fall.htm




rescue

Lamb rescued by digger after falling 2ft down a drainpipe

Thankfully the little lamb was unharmed by the ordeal and was reunited with his mum




rescue

Committed to the wrong branch? -, @{upstream}, and @{-1} to the rescue

I get into this situation sometimes. Maybe you do too. I merge feature work into a branch used to collect features, and then continue development but on that branch instead of back on the feature branch

git checkout feature
# ... bunch of feature commits ...
git push
git checkout qa-environment
git merge --no-ff --no-edit feature
git push
# deploy qa-environment to the QA remote environment
# ... more feature commits ...
# oh. I'm not committing in the feature branch like I should be

and have to move those commits to the feature branch they belong in and take them out of the throwaway accumulator branch

git checkout feature
git cherry-pick origin/qa-environment..qa-environment
git push
git checkout qa-environment
git reset --hard origin/qa-environment
git merge --no-ff --no-edit feature
git checkout feature
# ready for more feature commits

Maybe you prefer

git branch -D qa-environment
git checkout qa-environment

over

git checkout qa-environment
git reset --hard origin/qa-environment

Either way, that works. But it'd be nicer if we didn't have to type or even remember the branches' names and the remote's name. They are what is keeping this from being a context-independent string of commands you run any time this mistake happens. That's what we're going to solve here.

Shorthands for longevity

I like to use all possible natively supported shorthands. There are two broad motivations for that.

  1. Fingers have a limited number of movements in them. Save as many as possible left late in life.
  2. Current research suggests that multitasking has detrimental effects on memory. Development tends to be very heavy on multitasking. Maybe relieving some of the pressure on quick-access short term memory (like knowing all relevant branch names) add up to leave a healthier memory down the line.

First up for our scenario: the - shorthand, which refers to the previously checked out branch. There are a few places we can't use it, but it helps a lot:

Bash
# USING -

git checkout feature
# hack hack hack
git push
git checkout qa-environment
git merge --no-ff --no-edit -        # ????
git push
# hack hack hack
# whoops
git checkout -        # now on feature ???? 
git cherry-pick origin/qa-environment..qa-environment
git push
git checkout - # now on qa-environment ????
git reset --hard origin/qa-environment
git merge --no-ff --no-edit -        # ????
git checkout -                       # ????
# on feature and ready for more feature commits
Bash
# ORIGINAL

git checkout feature
# hack hack hack
git push
git checkout qa-environment
git merge --no-ff --no-edit feature
git push
# hack hack hack
# whoops
git checkout feature
git cherry-pick origin/qa-environment..qa-environment
git push
git checkout qa-environment
git reset --hard origin/qa-environment
git merge --no-ff --no-edit feature
git checkout feature
# ready for more feature commits

We cannot use - when cherry-picking a range

> git cherry-pick origin/-..-
fatal: bad revision 'origin/-..-'

> git cherry-pick origin/qa-environment..-
fatal: bad revision 'origin/qa-environment..-'

and even if we could we'd still have provide the remote's name (here, origin).

That shorthand doesn't apply in the later reset --hard command, and we cannot use it in the branch -D && checkout approach either. branch -D does not support the - shorthand and once the branch is deleted checkout can't reach it with -:

# assuming that branch-a has an upstream origin/branch-a
> git checkout branch-a
> git checkout branch-b
> git checkout -
> git branch -D -
error: branch '-' not found.
> git branch -D branch-a
> git checkout -
error: pathspec '-' did not match any file(s) known to git

So we have to remember the remote's name (we know it's origin because we are devoting memory space to knowing that this isn't one of those times it's something else), the remote tracking branch's name, the local branch's name, and we're typing those all out. No good! Let's figure out some shorthands.

@{-<n>} is hard to say but easy to fall in love with

We can do a little better by using @{-<n>} (you'll also sometimes see it referred to be the older @{-N}). It is a special construct for referring to the nth previously checked out ref.

> git checkout branch-a
> git checkout branch-b
> git rev-parse --abbrev-rev @{-1} # the name of the previously checked out branch
branch-a
> git checkout branch-c
> git rev-parse --abbrev-rev @{-2} # the name of branch checked out before the previously checked out one
branch-a

Back in our scenario, we're on qa-environment, we switch to feature, and then want to refer to qa-environment. That's @{-1}! So instead of

git cherry-pick origin/qa-environment..qa-environment

We can do

git cherry-pick origin/qa-environment..@{-1}

Here's where we are (🎉 marks wins from -, 💥 marks the win from @{-1})

Bash
# USING - AND @{-1}

git checkout feature
# hack hack hack
git push
git checkout qa-environment
git merge --no-ff --no-edit -                # ????
git push
# hack hack hack
# whoops
git checkout -                               # ????
git cherry-pick origin/qa-environment..@{-1} # ????
git push
git checkout -                               # ????
git reset --hard origin/qa-environment
git merge --no-ff --no-edit -                # ????
git checkout -                               # ????
# ready for more feature commits
Bash
# ORIGINAL

git checkout feature
# hack hack hack
git push
git checkout qa-environment
git merge --no-ff --no-edit feature
git push
# hack hack hack
# whoops
git checkout feature
git cherry-pick origin/qa-environment..qa-environment
git push
git checkout qa-environment
git reset --hard origin/qa-environment
git merge --no-ff --no-edit feature
git checkout feature
# ready for more feature commits

One down, two to go: we're still relying on memory for the remote's name and the remote branch's name and we're still typing both out in full. Can we replace those with generic shorthands?

@{-1} is the ref itself, not the ref's name, we can't do

> git cherry-pick origin/@{-1}..@{-1}
origin/@{-1}
fatal: ambiguous argument 'origin/@{-1}': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

because there is no branch origin/@{-1}. For the same reason, @{-1} does not give us a generalized shorthand for the scenario's later git reset --hard origin/qa-environment command.

But good news!

Do @{u} @{push}

@{upstream} or its shorthand @{u} is the remote branch a that would be pulled from if git pull were run. @{push} is the remote branch that would be pushed to if git push was run.

> git checkout branch-a
Switched to branch 'branch-a'
Your branch is ahead of 'origin/branch-a' by 3 commits.
  (use "git push" to publish your local commits)
> git reset --hard origin/branch-a
HEAD is now at <the SHA origin/branch-a is at>

we can

> git checkout branch-a
Switched to branch 'branch-a'
Your branch is ahead of 'origin/branch-a' by 3 commits.
  (use "git push" to publish your local commits)
> git reset --hard @{u}                                # <-- So Cool!
HEAD is now at <the SHA origin/branch-a is at>

Tacking either onto a branch name will give that branch's @{upstream} or @{push}. For example

git checkout branch-a@{u}

is the branch branch-a pulls from.

In the common workflow where a branch pulls from and pushes to the same branch, @{upstream} and @{push} will be the same, leaving @{u} as preferable for its terseness. @{push} shines in triangular workflows where you pull from one remote and push to another (see the external links below).

Going back to our scenario, it means short, portable commands with a minimum human memory footprint. (🎉 marks wins from -, 💥 marks the win from @{-1}, 😎 marks the wins from @{u}.)

Bash
# USING - AND @{-1} AND @{u}

git checkout feature
# hack hack hack
git push
git checkout qa-environment
git merge --no-ff --no-edit -    # ????
git push
# hack hack hack
# whoops
git checkout -                   # ????
git cherry-pick @{-1}@{u}..@{-1} # ????????
git push
git checkout -                   # ????
git reset --hard @{u}            # ????
git merge --no-ff --no-edit -    # ????
git checkout -                   # ????
# ready for more feature commits
Bash
# ORIGINAL

git checkout feature
# hack hack hack
git push
git checkout qa-environment
git merge --no-ff --no-edit feature
git push
# hack hack hack
# whoops
git checkout feature
git cherry-pick origin/qa-environment..qa-environment
git push
git checkout qa-environment
git reset --hard origin/qa-environment
git merge --no-ff --no-edit feature
git checkout feature
# ready for more feature commits

Make the things you repeat the easiest to do

Because these commands are generalized, we can run some series of them once, maybe

git checkout - && git reset --hard @{u} && git checkout -

or

git checkout - && git cherry-pick @{-1}@{u}.. @{-1} && git checkout - && git reset --hard @{u} && git checkout -

and then those will be in the shell history just waiting to be retrieved and run again the next time, whether with CtrlR incremental search or history substring searching bound to the up arrow or however your interactive shell is configured. Or make it an alias, or even better an abbreviation if your interactive shell supports them. Save the body wear and tear, give memory a break, and level up in Git.

And keep going

The GitHub blog has a good primer on triangular workflows and how they can polish your process of contributing to external projects.

The FreeBSD Wiki has a more in-depth article on triangular workflow process (though it doesn't know about @{push} and @{upstream}).

The construct @{-<n>} and the suffixes @{push} and @{upstream} are all part of the gitrevisions spec. Direct links to each:



    • Code
    • Front-end Engineering
    • Back-end Engineering

    rescue

    Committed to the wrong branch? -, @{upstream}, and @{-1} to the rescue

    I get into this situation sometimes. Maybe you do too. I merge feature work into a branch used to collect features, and then continue development but on that branch instead of back on the feature branch

    git checkout feature
    # ... bunch of feature commits ...
    git push
    git checkout qa-environment
    git merge --no-ff --no-edit feature
    git push
    # deploy qa-environment to the QA remote environment
    # ... more feature commits ...
    # oh. I'm not committing in the feature branch like I should be

    and have to move those commits to the feature branch they belong in and take them out of the throwaway accumulator branch

    git checkout feature
    git cherry-pick origin/qa-environment..qa-environment
    git push
    git checkout qa-environment
    git reset --hard origin/qa-environment
    git merge --no-ff --no-edit feature
    git checkout feature
    # ready for more feature commits

    Maybe you prefer

    git branch -D qa-environment
    git checkout qa-environment

    over

    git checkout qa-environment
    git reset --hard origin/qa-environment

    Either way, that works. But it'd be nicer if we didn't have to type or even remember the branches' names and the remote's name. They are what is keeping this from being a context-independent string of commands you run any time this mistake happens. That's what we're going to solve here.

    Shorthands for longevity

    I like to use all possible natively supported shorthands. There are two broad motivations for that.

    1. Fingers have a limited number of movements in them. Save as many as possible left late in life.
    2. Current research suggests that multitasking has detrimental effects on memory. Development tends to be very heavy on multitasking. Maybe relieving some of the pressure on quick-access short term memory (like knowing all relevant branch names) add up to leave a healthier memory down the line.

    First up for our scenario: the - shorthand, which refers to the previously checked out branch. There are a few places we can't use it, but it helps a lot:

    Bash
    # USING -
    
    git checkout feature
    # hack hack hack
    git push
    git checkout qa-environment
    git merge --no-ff --no-edit -        # ????
    git push
    # hack hack hack
    # whoops
    git checkout -        # now on feature ???? 
    git cherry-pick origin/qa-environment..qa-environment
    git push
    git checkout - # now on qa-environment ????
    git reset --hard origin/qa-environment
    git merge --no-ff --no-edit -        # ????
    git checkout -                       # ????
    # on feature and ready for more feature commits
    Bash
    # ORIGINAL
    
    git checkout feature
    # hack hack hack
    git push
    git checkout qa-environment
    git merge --no-ff --no-edit feature
    git push
    # hack hack hack
    # whoops
    git checkout feature
    git cherry-pick origin/qa-environment..qa-environment
    git push
    git checkout qa-environment
    git reset --hard origin/qa-environment
    git merge --no-ff --no-edit feature
    git checkout feature
    # ready for more feature commits

    We cannot use - when cherry-picking a range

    > git cherry-pick origin/-..-
    fatal: bad revision 'origin/-..-'
    
    > git cherry-pick origin/qa-environment..-
    fatal: bad revision 'origin/qa-environment..-'

    and even if we could we'd still have provide the remote's name (here, origin).

    That shorthand doesn't apply in the later reset --hard command, and we cannot use it in the branch -D && checkout approach either. branch -D does not support the - shorthand and once the branch is deleted checkout can't reach it with -:

    # assuming that branch-a has an upstream origin/branch-a
    > git checkout branch-a
    > git checkout branch-b
    > git checkout -
    > git branch -D -
    error: branch '-' not found.
    > git branch -D branch-a
    > git checkout -
    error: pathspec '-' did not match any file(s) known to git

    So we have to remember the remote's name (we know it's origin because we are devoting memory space to knowing that this isn't one of those times it's something else), the remote tracking branch's name, the local branch's name, and we're typing those all out. No good! Let's figure out some shorthands.

    @{-<n>} is hard to say but easy to fall in love with

    We can do a little better by using @{-<n>} (you'll also sometimes see it referred to be the older @{-N}). It is a special construct for referring to the nth previously checked out ref.

    > git checkout branch-a
    > git checkout branch-b
    > git rev-parse --abbrev-rev @{-1} # the name of the previously checked out branch
    branch-a
    > git checkout branch-c
    > git rev-parse --abbrev-rev @{-2} # the name of branch checked out before the previously checked out one
    branch-a

    Back in our scenario, we're on qa-environment, we switch to feature, and then want to refer to qa-environment. That's @{-1}! So instead of

    git cherry-pick origin/qa-environment..qa-environment

    We can do

    git cherry-pick origin/qa-environment..@{-1}

    Here's where we are (🎉 marks wins from -, 💥 marks the win from @{-1})

    Bash
    # USING - AND @{-1}
    
    git checkout feature
    # hack hack hack
    git push
    git checkout qa-environment
    git merge --no-ff --no-edit -                # ????
    git push
    # hack hack hack
    # whoops
    git checkout -                               # ????
    git cherry-pick origin/qa-environment..@{-1} # ????
    git push
    git checkout -                               # ????
    git reset --hard origin/qa-environment
    git merge --no-ff --no-edit -                # ????
    git checkout -                               # ????
    # ready for more feature commits
    Bash
    # ORIGINAL
    
    git checkout feature
    # hack hack hack
    git push
    git checkout qa-environment
    git merge --no-ff --no-edit feature
    git push
    # hack hack hack
    # whoops
    git checkout feature
    git cherry-pick origin/qa-environment..qa-environment
    git push
    git checkout qa-environment
    git reset --hard origin/qa-environment
    git merge --no-ff --no-edit feature
    git checkout feature
    # ready for more feature commits

    One down, two to go: we're still relying on memory for the remote's name and the remote branch's name and we're still typing both out in full. Can we replace those with generic shorthands?

    @{-1} is the ref itself, not the ref's name, we can't do

    > git cherry-pick origin/@{-1}..@{-1}
    origin/@{-1}
    fatal: ambiguous argument 'origin/@{-1}': unknown revision or path not in the working tree.
    Use '--' to separate paths from revisions, like this:
    'git <command> [<revision>...] -- [<file>...]'

    because there is no branch origin/@{-1}. For the same reason, @{-1} does not give us a generalized shorthand for the scenario's later git reset --hard origin/qa-environment command.

    But good news!

    Do @{u} @{push}

    @{upstream} or its shorthand @{u} is the remote branch a that would be pulled from if git pull were run. @{push} is the remote branch that would be pushed to if git push was run.

    > git checkout branch-a
    Switched to branch 'branch-a'
    Your branch is ahead of 'origin/branch-a' by 3 commits.
      (use "git push" to publish your local commits)
    > git reset --hard origin/branch-a
    HEAD is now at <the SHA origin/branch-a is at>

    we can

    > git checkout branch-a
    Switched to branch 'branch-a'
    Your branch is ahead of 'origin/branch-a' by 3 commits.
      (use "git push" to publish your local commits)
    > git reset --hard @{u}                                # <-- So Cool!
    HEAD is now at <the SHA origin/branch-a is at>

    Tacking either onto a branch name will give that branch's @{upstream} or @{push}. For example

    git checkout branch-a@{u}

    is the branch branch-a pulls from.

    In the common workflow where a branch pulls from and pushes to the same branch, @{upstream} and @{push} will be the same, leaving @{u} as preferable for its terseness. @{push} shines in triangular workflows where you pull from one remote and push to another (see the external links below).

    Going back to our scenario, it means short, portable commands with a minimum human memory footprint. (🎉 marks wins from -, 💥 marks the win from @{-1}, 😎 marks the wins from @{u}.)

    Bash
    # USING - AND @{-1} AND @{u}
    
    git checkout feature
    # hack hack hack
    git push
    git checkout qa-environment
    git merge --no-ff --no-edit -    # ????
    git push
    # hack hack hack
    # whoops
    git checkout -                   # ????
    git cherry-pick @{-1}@{u}..@{-1} # ????????
    git push
    git checkout -                   # ????
    git reset --hard @{u}            # ????
    git merge --no-ff --no-edit -    # ????
    git checkout -                   # ????
    # ready for more feature commits
    Bash
    # ORIGINAL
    
    git checkout feature
    # hack hack hack
    git push
    git checkout qa-environment
    git merge --no-ff --no-edit feature
    git push
    # hack hack hack
    # whoops
    git checkout feature
    git cherry-pick origin/qa-environment..qa-environment
    git push
    git checkout qa-environment
    git reset --hard origin/qa-environment
    git merge --no-ff --no-edit feature
    git checkout feature
    # ready for more feature commits

    Make the things you repeat the easiest to do

    Because these commands are generalized, we can run some series of them once, maybe

    git checkout - && git reset --hard @{u} && git checkout -

    or

    git checkout - && git cherry-pick @{-1}@{u}.. @{-1} && git checkout - && git reset --hard @{u} && git checkout -

    and then those will be in the shell history just waiting to be retrieved and run again the next time, whether with CtrlR incremental search or history substring searching bound to the up arrow or however your interactive shell is configured. Or make it an alias, or even better an abbreviation if your interactive shell supports them. Save the body wear and tear, give memory a break, and level up in Git.

    And keep going

    The GitHub blog has a good primer on triangular workflows and how they can polish your process of contributing to external projects.

    The FreeBSD Wiki has a more in-depth article on triangular workflow process (though it doesn't know about @{push} and @{upstream}).

    The construct @{-<n>} and the suffixes @{push} and @{upstream} are all part of the gitrevisions spec. Direct links to each:



      • Code
      • Front-end Engineering
      • Back-end Engineering

      rescue

      Committed to the wrong branch? -, @{upstream}, and @{-1} to the rescue

      I get into this situation sometimes. Maybe you do too. I merge feature work into a branch used to collect features, and then continue development but on that branch instead of back on the feature branch

      git checkout feature
      # ... bunch of feature commits ...
      git push
      git checkout qa-environment
      git merge --no-ff --no-edit feature
      git push
      # deploy qa-environment to the QA remote environment
      # ... more feature commits ...
      # oh. I'm not committing in the feature branch like I should be

      and have to move those commits to the feature branch they belong in and take them out of the throwaway accumulator branch

      git checkout feature
      git cherry-pick origin/qa-environment..qa-environment
      git push
      git checkout qa-environment
      git reset --hard origin/qa-environment
      git merge --no-ff --no-edit feature
      git checkout feature
      # ready for more feature commits

      Maybe you prefer

      git branch -D qa-environment
      git checkout qa-environment

      over

      git checkout qa-environment
      git reset --hard origin/qa-environment

      Either way, that works. But it'd be nicer if we didn't have to type or even remember the branches' names and the remote's name. They are what is keeping this from being a context-independent string of commands you run any time this mistake happens. That's what we're going to solve here.

      Shorthands for longevity

      I like to use all possible natively supported shorthands. There are two broad motivations for that.

      1. Fingers have a limited number of movements in them. Save as many as possible left late in life.
      2. Current research suggests that multitasking has detrimental effects on memory. Development tends to be very heavy on multitasking. Maybe relieving some of the pressure on quick-access short term memory (like knowing all relevant branch names) add up to leave a healthier memory down the line.

      First up for our scenario: the - shorthand, which refers to the previously checked out branch. There are a few places we can't use it, but it helps a lot:

      Bash
      # USING -
      
      git checkout feature
      # hack hack hack
      git push
      git checkout qa-environment
      git merge --no-ff --no-edit -        # ????
      git push
      # hack hack hack
      # whoops
      git checkout -        # now on feature ???? 
      git cherry-pick origin/qa-environment..qa-environment
      git push
      git checkout - # now on qa-environment ????
      git reset --hard origin/qa-environment
      git merge --no-ff --no-edit -        # ????
      git checkout -                       # ????
      # on feature and ready for more feature commits
      Bash
      # ORIGINAL
      
      git checkout feature
      # hack hack hack
      git push
      git checkout qa-environment
      git merge --no-ff --no-edit feature
      git push
      # hack hack hack
      # whoops
      git checkout feature
      git cherry-pick origin/qa-environment..qa-environment
      git push
      git checkout qa-environment
      git reset --hard origin/qa-environment
      git merge --no-ff --no-edit feature
      git checkout feature
      # ready for more feature commits

      We cannot use - when cherry-picking a range

      > git cherry-pick origin/-..-
      fatal: bad revision 'origin/-..-'
      
      > git cherry-pick origin/qa-environment..-
      fatal: bad revision 'origin/qa-environment..-'

      and even if we could we'd still have provide the remote's name (here, origin).

      That shorthand doesn't apply in the later reset --hard command, and we cannot use it in the branch -D && checkout approach either. branch -D does not support the - shorthand and once the branch is deleted checkout can't reach it with -:

      # assuming that branch-a has an upstream origin/branch-a
      > git checkout branch-a
      > git checkout branch-b
      > git checkout -
      > git branch -D -
      error: branch '-' not found.
      > git branch -D branch-a
      > git checkout -
      error: pathspec '-' did not match any file(s) known to git

      So we have to remember the remote's name (we know it's origin because we are devoting memory space to knowing that this isn't one of those times it's something else), the remote tracking branch's name, the local branch's name, and we're typing those all out. No good! Let's figure out some shorthands.

      @{-<n>} is hard to say but easy to fall in love with

      We can do a little better by using @{-<n>} (you'll also sometimes see it referred to be the older @{-N}). It is a special construct for referring to the nth previously checked out ref.

      > git checkout branch-a
      > git checkout branch-b
      > git rev-parse --abbrev-rev @{-1} # the name of the previously checked out branch
      branch-a
      > git checkout branch-c
      > git rev-parse --abbrev-rev @{-2} # the name of branch checked out before the previously checked out one
      branch-a

      Back in our scenario, we're on qa-environment, we switch to feature, and then want to refer to qa-environment. That's @{-1}! So instead of

      git cherry-pick origin/qa-environment..qa-environment

      We can do

      git cherry-pick origin/qa-environment..@{-1}

      Here's where we are (🎉 marks wins from -, 💥 marks the win from @{-1})

      Bash
      # USING - AND @{-1}
      
      git checkout feature
      # hack hack hack
      git push
      git checkout qa-environment
      git merge --no-ff --no-edit -                # ????
      git push
      # hack hack hack
      # whoops
      git checkout -                               # ????
      git cherry-pick origin/qa-environment..@{-1} # ????
      git push
      git checkout -                               # ????
      git reset --hard origin/qa-environment
      git merge --no-ff --no-edit -                # ????
      git checkout -                               # ????
      # ready for more feature commits
      Bash
      # ORIGINAL
      
      git checkout feature
      # hack hack hack
      git push
      git checkout qa-environment
      git merge --no-ff --no-edit feature
      git push
      # hack hack hack
      # whoops
      git checkout feature
      git cherry-pick origin/qa-environment..qa-environment
      git push
      git checkout qa-environment
      git reset --hard origin/qa-environment
      git merge --no-ff --no-edit feature
      git checkout feature
      # ready for more feature commits

      One down, two to go: we're still relying on memory for the remote's name and the remote branch's name and we're still typing both out in full. Can we replace those with generic shorthands?

      @{-1} is the ref itself, not the ref's name, we can't do

      > git cherry-pick origin/@{-1}..@{-1}
      origin/@{-1}
      fatal: ambiguous argument 'origin/@{-1}': unknown revision or path not in the working tree.
      Use '--' to separate paths from revisions, like this:
      'git <command> [<revision>...] -- [<file>...]'

      because there is no branch origin/@{-1}. For the same reason, @{-1} does not give us a generalized shorthand for the scenario's later git reset --hard origin/qa-environment command.

      But good news!

      Do @{u} @{push}

      @{upstream} or its shorthand @{u} is the remote branch a that would be pulled from if git pull were run. @{push} is the remote branch that would be pushed to if git push was run.

      > git checkout branch-a
      Switched to branch 'branch-a'
      Your branch is ahead of 'origin/branch-a' by 3 commits.
        (use "git push" to publish your local commits)
      > git reset --hard origin/branch-a
      HEAD is now at <the SHA origin/branch-a is at>

      we can

      > git checkout branch-a
      Switched to branch 'branch-a'
      Your branch is ahead of 'origin/branch-a' by 3 commits.
        (use "git push" to publish your local commits)
      > git reset --hard @{u}                                # <-- So Cool!
      HEAD is now at <the SHA origin/branch-a is at>

      Tacking either onto a branch name will give that branch's @{upstream} or @{push}. For example

      git checkout branch-a@{u}

      is the branch branch-a pulls from.

      In the common workflow where a branch pulls from and pushes to the same branch, @{upstream} and @{push} will be the same, leaving @{u} as preferable for its terseness. @{push} shines in triangular workflows where you pull from one remote and push to another (see the external links below).

      Going back to our scenario, it means short, portable commands with a minimum human memory footprint. (🎉 marks wins from -, 💥 marks the win from @{-1}, 😎 marks the wins from @{u}.)

      Bash
      # USING - AND @{-1} AND @{u}
      
      git checkout feature
      # hack hack hack
      git push
      git checkout qa-environment
      git merge --no-ff --no-edit -    # ????
      git push
      # hack hack hack
      # whoops
      git checkout -                   # ????
      git cherry-pick @{-1}@{u}..@{-1} # ????????
      git push
      git checkout -                   # ????
      git reset --hard @{u}            # ????
      git merge --no-ff --no-edit -    # ????
      git checkout -                   # ????
      # ready for more feature commits
      Bash
      # ORIGINAL
      
      git checkout feature
      # hack hack hack
      git push
      git checkout qa-environment
      git merge --no-ff --no-edit feature
      git push
      # hack hack hack
      # whoops
      git checkout feature
      git cherry-pick origin/qa-environment..qa-environment
      git push
      git checkout qa-environment
      git reset --hard origin/qa-environment
      git merge --no-ff --no-edit feature
      git checkout feature
      # ready for more feature commits

      Make the things you repeat the easiest to do

      Because these commands are generalized, we can run some series of them once, maybe

      git checkout - && git reset --hard @{u} && git checkout -

      or

      git checkout - && git cherry-pick @{-1}@{u}.. @{-1} && git checkout - && git reset --hard @{u} && git checkout -

      and then those will be in the shell history just waiting to be retrieved and run again the next time, whether with CtrlR incremental search or history substring searching bound to the up arrow or however your interactive shell is configured. Or make it an alias, or even better an abbreviation if your interactive shell supports them. Save the body wear and tear, give memory a break, and level up in Git.

      And keep going

      The GitHub blog has a good primer on triangular workflows and how they can polish your process of contributing to external projects.

      The FreeBSD Wiki has a more in-depth article on triangular workflow process (though it doesn't know about @{push} and @{upstream}).

      The construct @{-<n>} and the suffixes @{push} and @{upstream} are all part of the gitrevisions spec. Direct links to each:



        • Code
        • Front-end Engineering
        • Back-end Engineering