The Git Commands I Use Every Day
Wade Zimmerman

Wade Zimmerman @wadecodez

About: Code by day; blog by night

Location:
California
Joined:
Jun 5, 2021

The Git Commands I Use Every Day

Publish Date: Sep 17 '22
445 32

1. Get all latest changes without merging

Stop pulling code that you think will break! Having fetch in your workflow allows you to grab updated code without immediately merging it. Once the code is fetched, you can check it out like any other branch. When you're satisfied, then you can merge.

git fetch --all
# git checkout upstream/some-branch
Enter fullscreen mode Exit fullscreen mode

2. Push upstream regardless of current branch name

Stop typing the branch names especially when they are long branch names. Just tell git you want to push the current branch to a remote location. HEAD is a key word that tells git to use the current branch.

git push production head
# git push origin head
# git push github head
Enter fullscreen mode Exit fullscreen mode

3. Label your stash

This is useful if you stash a lot of code and you want to remember what the stash contains at a glance.

git stash save -m "my work in progress"
Enter fullscreen mode Exit fullscreen mode

4. Use a stash from ages ago

Stop undoing stashes with git pop to get to an old stash. You can apply a stash that you created ages ago by using the following command.

# git stash list
git stash apply 3
Enter fullscreen mode Exit fullscreen mode

5. Checkout the previous branch

This is super helpful when you are working on small features and you want to compare behavior/performance by toggling branches. You don't have to type the names, just use the minus sign.

git checkout -
Enter fullscreen mode Exit fullscreen mode

I like this command so much that I made a quick YouTube shorts video showing it off!

6. Change the base of the branch after doing a checkout

This is useful if you created a new branch but you based it off the wrong branch. For example, say you wanted to branch from the beta code, but you accidentally branched using the production code.

git rebase --onto beta production feature
# git rebase newBase oldBase currentBranch
Enter fullscreen mode Exit fullscreen mode

7. Move uncommitted changes to new/existing branch

git switch -c new-branch
# git switch existing-branch
Enter fullscreen mode Exit fullscreen mode

Bonus - Fuzzy Checkout

This custom command allows you to quickly switch to another branch without typing the entire name. This is super useful when you are using a naming convention and you are tired of typing a prefix like feature/ or issue/

function fc() {
    gco "$(git branch --format='%(refname:short)' | grep $1)"
}
Enter fullscreen mode Exit fullscreen mode

If your branch name was called feature/dropdown-select-color you could quickly switch branches by doing something like this.

fc dropdown
Enter fullscreen mode Exit fullscreen mode

Comments 32 total

  • Al - Naucode
    Al - NaucodeSep 17, 2022

    Great article, keep the good work! Liked and followed! 🚀

  • spO0q
    spO0qSep 17, 2022

    you can also type git push -u origin HEAD for the upstream.

    • Wade Zimmerman
      Wade ZimmermanSep 17, 2022

      Yeah definitely use the -u or --set-upstream flag if you use git pull in your workflow otherwise you'll have to type git pull origin <branch>.

      I don't use pull though

  • Motabar Javaid
    Motabar JavaidSep 17, 2022

    Thanks Wade!

  • Mahmoud Harmouch
    Mahmoud HarmouchSep 17, 2022

    Never heard of git switch; Good to know. I am now wondering about the difference between switch and checkout.

    Thanks for sharing, BTW! Keep it up!

    • Valentin Nechayev
      Valentin NechayevSep 18, 2022

      "switch" and "restore" are relatively new offsprings from "checkout" (approx. 2 years old). It's not strange that many guides still omit them.

    • Jesse Phillips
      Jesse PhillipsSep 19, 2022

      Switch is an improved interface from checkout. Have not needed checkout though: git switch --detach , is a bit longer.

      For example previous branch: git switch -

  • gabrielalfianali
    gabrielalfianaliSep 17, 2022

  • Luthira Geesilu
    Luthira GeesiluSep 17, 2022

    Thanks Wade!

  • José Pablo Ramírez Vargas
    José Pablo Ramírez VargasSep 18, 2022

    Call me crazy, but I have never, EVER, had to type a single git command. I use Visual Studio Code with the Git Graph extension. It does everything for me without having to invest brain cells in learning a bunch of CLI commands.

    • Wade Zimmerman
      Wade ZimmermanSep 18, 2022

      It's worth it because then you can pretend to be a hacker!

    • Valentin Nechayev
      Valentin NechayevSep 18, 2022

      Does it allow interactive rebase? Rebase onto another branch? Bisecting?
      Pushing of a specific revision (not head)?
      (I mention only actions I have to do regularly)

    • Jesse Phillips
      Jesse PhillipsSep 19, 2022

      The issue I have had is that people don't know how to use their UI. This is true with command line as well, but then we would at least be in the same environment.

      If we used the same GUI then that would be the same. But I find every GUI to be dangerous. They try their best to help but always make it easy to throw out unsaved changes (git hates doing this).

      I have not found a git GUI where I don't need to spend mental energy getting the GUI to do what I want.

      • Mark Roeling
        Mark RoelingSep 21, 2022

        But I find every GUI to be dangerous.
        I almost agree. I find most git GUI too dangerous. I use GitCola for basic things, I but almost always fall back to the good old cli.

        Btw, new command learned! git switch -c new-branch
        I normally run git stash ; git checkout -b new-branch ; git stash pop to achieve the same result.

        • Jesse Phillips
          Jesse PhillipsSep 21, 2022

          But switch -c and checkout -b are the same. They both require stash if git needs to update a modified file.

    • Tahsin Ahmed
      Tahsin AhmedSep 19, 2022

      I use that too but being old school have it's own level of satisfaction :P Cheers

    • Madalin Ignisca
      Madalin IgniscaSep 19, 2022

      So you depend on many others for minimal needed git things for CI/CD?

      • José Pablo Ramírez Vargas
        José Pablo Ramírez VargasSep 19, 2022

        I have my minions for CI/CD, yes. I focus on programming, and then I let them lift that part for me. :-)

    • Ivan Karabadzhak
      Ivan KarabadzhakSep 21, 2022

      I work inside IDE, but use console for git just for fun.

      If I should make some complex merge, then I prefer to use some GUI to be sure everything is going OK.

    • PNS11
      PNS11Sep 26, 2022

      So you're not the go-to guy when things break in unexpected ways in your workplace.

  • Jessica Alves
    Jessica AlvesSep 18, 2022

    Nice article! I also like the
    git add --all -p

  • Benjamin Bryant
    Benjamin BryantSep 18, 2022

    Cool, article! I'll make use of this one:
    git switch -c new-branch

  • mrcod3r-ir
    mrcod3r-irSep 19, 2022

    tnx

  • ૮༼⚆︿⚆༽つ
    ૮༼⚆︿⚆༽つSep 19, 2022

    I use: fzf.fish, gitui, lazygit, difftastic, delta, git-filter-repo, and git-branchless; for git-ing my stuff. Don't ask me why I don't use VSCode 😂

  • Troy Kirinhakone
    Troy KirinhakoneSep 20, 2022

    Thanks! Now we need some noble person to make such an article on git submodules, especially in a monorepo such as Nx.

  • Ivan Karabadzhak
    Ivan KarabadzhakSep 21, 2022

    Great article Wade, Thank you!

  • Dan Jones
    Dan JonesSep 22, 2022

    I would encourage you to add that last function as a git alias, rather than a command in your .bashrc.

    Something like:

    git config --global alias.fc 'f(){git checkout "$(git branch --format="%(refname:short)" | grep $1)";}; f'
    
    Enter fullscreen mode Exit fullscreen mode

    Then, you call it with git fc dropdown. Sure, it's more characters to type, but it enforces mentally that it's a git command that you're running, and, if you ever switch shells, you don't have to move that over.

  • Vincent Amstoutz
    Vincent AmstoutzOct 10, 2022

    One of the most interesting cheat sheets I've seen on Dev.to and Medium if it's not the most interesting one, thanks for the tips! @wadecodez

  • Valentin D.
    Valentin D.Oct 26, 2022

    I'm sorry, maybe I'm missing something. But why do need to type git push production head if you can just do git push? It will push your current branch to it's remote.

Add comment