Search code examples
gitbranchgit-branchbranching-and-merging

Which command to use to create a new branch?


What's the difference between: git branch <branchname> and git checkout -b <branchname>, don't they both create a new branch?


Solution

    • git branch - See what branches are there and what's you're working on right now
    • git branch new_branch_name or git branch new_branch_name HEAD - Create new branch. Use letters, numbers and underscores as best practice. Branch created from the commit currently HEAD looking into on the current working branch
    • git checkout new_branch_name - Switch to other branch. HEAD will still look to same hash until new commit is done then HEAD moves accordingly. When switching branches, all files and folders are changing to match that branch immediately.
    • git checkout -b new_branch_name - Create new branch and switch to it at once