Skip to main content

Command Palette

Search for a command to run...

Git Branches: Creating, Updating, Merging and DeletingThem

Updated
3 min readView as Markdown
Git Branches: Creating, Updating, Merging and DeletingThem
A
I automate things for fun and work.
  1. Create and switch to a new branch in local repository:

    git checkout -b new_feature
    
  2. Make changes in that branch, then to make commit:

    git add .
    git commit -m "Implemented new feature"
    
  3. Push this newly created and modified branch to remote repository(GitHub).

    For the first push of this branch:

    git push -u origin new_feature
    

    This creates our branch on the remote repository, pushes our commits, and sets up tracking.

    For upcoming pushes, we can simply use:

    git push
    

    Note: If we use git push without -u in the first time:

    • If the branch doesn't exist remotely, Git will suggest using --set-upstream.

    • If the branch exists (created by someone else), it'll push but won't set up tracking.

  4. To keep our local branch updated with the changes made in remote repository:

    (make sure we are in new_feature branch, if not then do checkout first to it)

    git pull origin new_feature
    
  5. Checking Branch Status:

    git branch
    

    This command lists all local branches, with the current branch highlighted.

  6. To ensure both main and feature branches are up to date before merging them:

    git checkout new_feature
    git pull origin new_feature
    git checkout main
    git pull origin main
    

    Note: We use the explicit form (git pull origin <branch>) for both branches. This works regardless of whether tracking is set up, making it a safer practice, especially in collaborative environments where branch configurations might vary.

  7. Alternative branch updation method (without checkout):

    git fetch origin main:main
    git fetch origin new_feature:new_feature
    

    We usually prefer the checkout-and-pull method because:

    • It's more natural .

    • It ensures we're working with the latest content in our working directory.

    • It helps avoid confusion by immediately reflecting changes in our local files.

    • It reduces the risk of merge conflicts later by keeping our local branch in sync with the remote.

  8. Merging Process.

    To merge the new_feature branch to the main branch, first we make sure all related branches are up to date with remote repo changes, then switch to the main branch, give merge command, and finally push this merged main branch to remote repository.

    git checkout main
    git merge new_feature
    git push origin main
    
  9. Deleting Branches

    After merging, we can delete the new_feature branch:

    a. Delete the local branch:

    Make sure we exit that branch we want to delete by checkout else we will get error: cannot delete branch ‘new_feature’ used by worktree…

    git branch -d new_feature
    

    Note: Use -D instead of -d to force delete a branch if it hasn't been merged yet.

    b. Delete the remote repo branch:

    git push origin --delete new_feature
    

    Note: For deleting remote repo branches, we must use —delete, as -d (or -D) is reserved for deleting local branches only

  10. Visual Workflow Diagram

    1. Create branch    main         A---B
                                         \
                        new_feature       C
    
    2. Push branch      main         A---B
                                         \
                        new_feature       C
                                          ^
                                          |
                                     origin/new_feature
    
    3. Merge to main    main         A---B---D
                                         \   /
                        new_feature       C-/
    
    4. After deletion   main         A---B---D
                                             ^
                                             |
                                        origin/main
    

    Explanation of nodes:

    A: An earlier commit on the main branch.

    B: The commit on main from which the new_feature branch is created.

    C: The commit made on the new_feature branch.

    D: The merge commit, combining the new_feature branch into main.