Git - working with branches

Branching strategies

  1. https://www.gitkraken.com/learn/git/best-practices/git-branch-strategy

Good gamification way of learning git branching

https://learngitbranching.js.org/

How to create branch from an old commit

  1. git branch <branchname> <commitHash>
  2. git checkout -b <branchname> <commitHash> (This is a combination of git checkout <commitHash> and git checkout -b <branchname>)
  3. checkout a branch and use reset
    # Create a new branch from HEAD
    git checkout -b <branchname>
    
    # Remove the commits made after <commit-id>
    git reset --hard <commit-id>
    
    # Push the new branch to the remote repository
    git push --set-upstream origin <branchname>
    
  4. Using GitHub or BitBucket UI. Go to the specific commit and create a branch from there.

Listing branches

command description
git branch -vv To find out which remote branch your local branch is tracking, command to give tracking branch
git status -sb To find out which remote branch your local branch is tracking, command to give tracking branch
git remote update command to update remote branches
git remote update --prune command to update remote branches. This will remove all remote branches which you have a local record of, but are no longer actually present on the remote.
git branch -m <newname> To rename the current local branch
git branch (local branch name) -u (remote branch name) To point local branch to a different remote branch
git branch -a command to list all branches
git branch -r Remote branches only.
git branch -l or git branch Local branches only.

Checking out branches

command description
git checkout -b feature/testFeatureBranch remotes/origin/develop To create a feature branch from remotes/origin/develop and checkout that feature branch
git checkout -b feature/testFeatureBranch Same step as above; But you don’t have to mention develop branch in the command if you are already on the develop branch
git checkout localBranch1 To find out which remote branch your local branch is tracking
git checkout localBranch2 To find out which remote branch your local branch is tracking
git status To see the status of the local repository

Remove all your local branches except main

Sometimes, you may want to delete all your local branches except main

git branch | grep -ve " main$" | xargs git branch -D

You can also use an alias:

alias gbr="git branch | grep -ve " main$" | xargs git branch -D"

Deleting branches

https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely

Delete a remote branch

$ git push <remote_name> --delete <branch_name>Delete Local Branch
$ git push <remote_name> -d <branch_name>Delete Local Branch

Delete local branch

git branch -d <branch_name>
git branch -D <branch_name>
  1. The -d option is an alias for –delete, which only deletes the branch if it has already been fully merged in its upstream branch.
  2. The -D option is an alias for –delete –force, which deletes the branch “irrespective of its merged status.” [Source: man git-branch]
  3. As of Git v2.3, git branch -d (delete) learned to honor the -f (force) flag.
  4. You will receive an error if you try to delete the currently selected branch.

My local workflow when working with branches

  1. Local branch
  2. Keep committing changes to it consistently
    1. Another approach is to move changes to the staging area
  3. Before switching to other branches, or pulling or merging or pushing to remote, squash.
    1. This will keep the history a little cleaner.
  4. Use stash sparingly.
    1. When we create branches from stashes, it includes everything from the stash into that branch.
    2. If there are changes that I want to keep in local only (not push it to remote ever), it gets tricky.