Christopher Anabo
Christopher Anabo
Senior Tech Lead
Christopher Anabo

Notes

GIT Notes

GIT Notes

 

 
  Description Command
Clone repository git clone   

Create new Branch in Git

There are many ways to create a new Git branch. In most cases, it comes down to whether you are creating a branch from the main (master) branch or, for example, a new commit or tag
  • git branch [new_branch_name]

Create new Git Branch From Current Branch

The easiest and most popular way of creating a Git branch from the current branch is to use the git switch or git checkout command with the -c and -b options, respectively. The syntax for both commands is shown below:
  • git checkout -b [new_branch_name]
  • git switch -c [new_branch_name]
  •  

Create New Git Branch From Different Branch

Replace [new_branch_name] with the name of the new branch and [specific_different_branch] with the name of the existing branch from which the new one should be created. For example, to create a branch called new_branch from the master branch, enter:
  • git checkout -b new_branch master

Create a Branch from a Commit

A Git commit represents a snapshot of your repository as it saves the changes made in the code. A project can have multiple commits as it is revised and improved. Each commit has a unique ID called a hash, which can be used to create a branch based on that specific commit

  • To find the commit
    • git log --oneline

Creating a branch from a commit is especially helpful if you need to go back to a previous software version to fix a bug without removing any existing features.

 

  • git branch [new_branch_name] [commit_hash]

Create a Branch from a Tag

A tag is a reference to a specific point in a project's history, usually a final, unchangeable version of a commit. 

  • To find the tag
    • git tag
    • git tag -l ""
  •  
  • git branch [new_branch_name] [tag_name]

 

 

Create a Branch Using Detached HEAD State

A detached HEAD state happens when you check out a commit that is not formally part of a branch. The state allows you to make changes and commit them, but no branch is tracking the changes.

Create a Git branch from a detached HEAD state.

As the warning outlines, you can make changes based on the commit. However, changes are lost if you don't save them because no branch is set to track them.

To save the changes, create a new branch using the git branch command and switch to it with git switch. Then, stage the changes, and create a new commit. When you finish working, you can merge the changes into the master branch.

  • git checkout [commit_hash]
     
     
     

 

Introduction to git fetch, git pull, and git rebase

Before examining the differences, let's define some key operations:

  • git fetch: This command downloads commits, files, and refs from a remote repository into your local repo. Fetching is a safe way to review changes before integrating them into your local repository.

 

  • git pull: A git pull operation is essentially a git fetch followed by a git merge. It fetches changes from the remote repository (the branch specified) and then merges them into your current branch.

    • git pull origin main

When you run the command git pull origin main in the terminal, here's what's happening:

  • origin is the default name Git gives to the remote repository from which you cloned. It's a shorthand alias for the URL of the remote repository.
  • main is the primary branch from which you want to pull the changes. In many Git workflows, main is the central branch where the final source code of your project is kept.

When executed, this command will fetch changes from the main branch of the remote named origin and merge them into the branch that you're currently checked out to in your local repository.

 

  • git rebase: Rebase is a way to move or combine a sequence of commits to a new base commit. Git rebase is often used to maintain a cleaner, more linear project history by moving the entire feature branch to sit on top of the main branch.

    • > git fetch origin 

    • > git rebase origin/main

 

  1. git fetch origin

    • This command fetches all the changes from the remote repository named origin but doesn't merge any of these changes into your local branches. It updates your local repository's remote tracking branches (like origin/mainorigin/feature-branch, etc.). These tracking branches represent the state of the branches in your remote repository at the last fetch.
  2. git rebase origin/main

    • After fetching, this command is used to rebase the current local branch onto origin/main. Rebasing is the process of moving or combining a sequence of commits to a new base commit. It's a way to integrate changes from origin/main into your branch while keeping the history linear and clean.
    • Rebasing re-writes the commit history by creating new commits for each commit in the original branch, which can make the project history easier to understand with fewer merge commits than a typical merge operation would create.

 

  1. Handling conflicts

    • Git pull: If there are conflicts during a pull, Git pauses the merge and asks you to resolve the conflicts manually. After resolving conflicts, you must commit the changes to complete the merge.

    • Git rebase: During a rebase, if conflicts arise, you also need to resolve each conflict manually. However, the rebase process requires you to use git rebase --continue to proceed after fixing conflicts. This method ensures that your feature branch can be tested as if the changes have been made on top of the base branch (main).

    •  

 

SQUASH