# Git Branches: Creating, Updating, Merging and DeletingThem

1.  Create and switch to a new branch in local repository:
    
    ```powershell
    git checkout -b new_feature
    ```
    
2.  Make changes in that branch, then to make commit:
    
    ```powershell
    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:
    
    ```powershell
    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:
    
    ```powershell
    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*)
    
    ```powershell
    git pull origin new_feature
    ```
    
5.  Checking Branch Status:
    
    ```powershell
    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:
    
    ```powershell
    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):
    
    ```powershell
    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.
    
    ```powershell
    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…*
    
    ```powershell
    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:
    
    ```powershell
    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
     
     ```powershell
     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.
