Git quick tips #2: Working with many branches

I somehow often end up working on a few branches in parallel and some things make my life easier:

git switch -: Like the cd command git switch and git checkout take a – argument which takes you back to the previously checked out branch.

$ git branc…


This content originally appeared on DEV Community and was authored by Michael Kohl

I somehow often end up working on a few branches in parallel and some things make my life easier:

  1. git switch -: Like the cd command git switch and git checkout take a - argument which takes you back to the previously checked out branch.

    $ git branch
    * citizen428/long-branch-name
    $ git switch main
    Switched to branch 'main'
    Your branch is up to date with 'origin/main'.
    $ git switch -
    Switched to branch 'citizen428/long-branch-name'
    Your branch is up to date with 'origin/citizen428/long-branch-name'.
    

    I actually have checkout aliased to co and switch to sw (as well as git itselftog`) but I didn't want to confuse readers with those.

  2. git show branch:file_name: Especially during profile work I often have several branches that modify the same files. A quick way to see a file from another branch without having to check it out is git show, e.g. git show main:app/models/user.rb.

  3. git-rerere: Short for "reuse recorded resolution". Particularly useful for long-running branches (which ideally we shouldn't have but ideals don't tend to survive contact with reality). The process is automatic and doesn't generally require manual intervention, just make sure it's enabled in your git configuration, e.g. git config --global rerere.enabled true. The git docs have a very nice explanation.

  4. git stash branch: I sometimes start working in the wrong checkout and a quick and easy way to correct this is the following:

    $ git stash # stash current changes
    $ git stash branch  
    

    This will directly create a new branch from the stash instead of having to first create it and then using git stash pop. Keep in mind that the currently active branch will be used as the base, so unless you're already on main you may want to check that out first.

  5. git autocorrect: Not directly related to branch management but it can be useful for the commands I use rarely and have not aliased. You can configure it like this git config --global help.autocorrect 10, where the integer is a value in 10th of a second, so the above will wait for 1 second before running the autocorrected command:
    autocorrect in action


This content originally appeared on DEV Community and was authored by Michael Kohl


Print Share Comment Cite Upload Translate Updates
APA

Michael Kohl | Sciencx (2021-07-20T02:04:47+00:00) Git quick tips #2: Working with many branches. Retrieved from https://www.scien.cx/2021/07/20/git-quick-tips-2-working-with-many-branches/

MLA
" » Git quick tips #2: Working with many branches." Michael Kohl | Sciencx - Tuesday July 20, 2021, https://www.scien.cx/2021/07/20/git-quick-tips-2-working-with-many-branches/
HARVARD
Michael Kohl | Sciencx Tuesday July 20, 2021 » Git quick tips #2: Working with many branches., viewed ,<https://www.scien.cx/2021/07/20/git-quick-tips-2-working-with-many-branches/>
VANCOUVER
Michael Kohl | Sciencx - » Git quick tips #2: Working with many branches. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/20/git-quick-tips-2-working-with-many-branches/
CHICAGO
" » Git quick tips #2: Working with many branches." Michael Kohl | Sciencx - Accessed . https://www.scien.cx/2021/07/20/git-quick-tips-2-working-with-many-branches/
IEEE
" » Git quick tips #2: Working with many branches." Michael Kohl | Sciencx [Online]. Available: https://www.scien.cx/2021/07/20/git-quick-tips-2-working-with-many-branches/. [Accessed: ]
rf:citation
» Git quick tips #2: Working with many branches | Michael Kohl | Sciencx | https://www.scien.cx/2021/07/20/git-quick-tips-2-working-with-many-branches/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.