The Git Commands I Use Every Day

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 othe…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Wade Zimmerman

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

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 upstream head
# git push origin head
# git push github head

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"

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

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 -

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

7. Move uncommitted changes to new/existing branch

git switch -c new-branch
# git switch existing-branch

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() {
    git checkout "$(git branch --format='%(refname:short)' | grep $1"
}

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

fc dropdown


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Wade Zimmerman


Print Share Comment Cite Upload Translate Updates
APA

Wade Zimmerman | Sciencx (2022-09-17T12:34:13+00:00) The Git Commands I Use Every Day. Retrieved from https://www.scien.cx/2022/09/17/the-git-commands-i-use-every-day/

MLA
" » The Git Commands I Use Every Day." Wade Zimmerman | Sciencx - Saturday September 17, 2022, https://www.scien.cx/2022/09/17/the-git-commands-i-use-every-day/
HARVARD
Wade Zimmerman | Sciencx Saturday September 17, 2022 » The Git Commands I Use Every Day., viewed ,<https://www.scien.cx/2022/09/17/the-git-commands-i-use-every-day/>
VANCOUVER
Wade Zimmerman | Sciencx - » The Git Commands I Use Every Day. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/17/the-git-commands-i-use-every-day/
CHICAGO
" » The Git Commands I Use Every Day." Wade Zimmerman | Sciencx - Accessed . https://www.scien.cx/2022/09/17/the-git-commands-i-use-every-day/
IEEE
" » The Git Commands I Use Every Day." Wade Zimmerman | Sciencx [Online]. Available: https://www.scien.cx/2022/09/17/the-git-commands-i-use-every-day/. [Accessed: ]
rf:citation
» The Git Commands I Use Every Day | Wade Zimmerman | Sciencx | https://www.scien.cx/2022/09/17/the-git-commands-i-use-every-day/ |

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.