Git branch - common operations and commands for working with branches in git

Git branch - common operations and commands for working with branches in git

So, here is a list of common commands used for branches in git and explanations for them:

  • Create an empty repository in the current folder:

    git init

  • View the log of all commits (HEAD should always be at the top, followed by commits that were in the project before it):

    git log

  • View what has changed since the last commit:

    git show

  • Pull all new changes from the server. Used to check if something new has appeared:

    git fetch

  • See which branch we are currently on:

    git branch

  • Create a new branch and immediately switch to it. Instead of name write the name of your new branch:

    git checkout -b name origin/main // or git checkout -b name origin/stage if you use stage instead of main

  • Delete a branch. Instead of name write the name of the branch you want to delete (you can specify several branches separated by a space - after executing the command they will all be deleted):

    git branch -D name

Also, for convenience, you can write aliases (abbreviations) for basic git commands:

  • Open a terminal (bash) and create a .gitconfig file in the root folder:

    touch ~/.gitconfig

  • Open this file:

    open ~/.gitconfig

  • Write aliases in it and save:

    [alias]
        co = checkout
        st = status
        br = branch
    
  • Close the terminal and open it again - now the commands git br, git st, git co should work.