Skip to content

Instantly share code, notes, and snippets.

@luisjunco
Last active February 15, 2024 08:38
Show Gist options
  • Save luisjunco/d9d0a7d62e7633568533b7214f6af840 to your computer and use it in GitHub Desktop.
Save luisjunco/d9d0a7d62e7633568533b7214f6af840 to your computer and use it in GitHub Desktop.
Git Branches - Cheatsheet

Git Branches Cheatsheet

image


πŸ™Œ | Common operations and commands:

  1. git switch -c <branchname> (this will create branch and switch to it)

    • ex. "git switch -c feat/payments"
  2. git push -u origin <branchname> (this will configure the new branch in the remote repository)

    • ex. "git push -u origin feat/payments"
  3. Work on the new branch and make as many commits as you need

    • ex:
      • git commit -m "implement shopping cart"
      • git commit -m "implement css for shopping cart"
      • ...
  4. git switch <branchname> (switch to a different branch)

    • ex. "git switch main"
  5. git merge <branchname> (merge code from "branchname" into current branch)

    • ex. "git merge feat/payments"

After that, if you need to continue working on your branch (for example, to continue working on "feat/payments"):

  • git switch feat/payments (switch again to "feat/payments" branch)
  • make commit(s)
  • repeat points 4. and 5.

πŸ“Œ | More info:

Atlassian Tutorial: using branches


Git checkout vs. git switch:

  • In older versions of git, we used to use a similar command called git checkout (git switch was introduced just some years ago).
  • You may still see "git checkout" in a lot of tutorials, stackoverflow etc. πŸ¦•. You can find more info here.

β›³ | Practice:

Visualizing Git (a playground where you can try commands and see a diagram with the result)

Learn Git Branching (guided tutorial to learn about git branches)


πŸ† | Pro Tip

Sometimes we're not sure how to do something on git (or if the command that I'm about to use will destroy hours of work...).
Remember that you can always create a directory somewhere in your computer, open a command line, initialize a new repo... and practice with it in a safe place πŸŽ―πŸ˜‰

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment