Day 10 Advance Git & GitHub

Day 10  Advance Git & GitHub

Git Branch:-

A branch is a version of the repository that diverges from the main working project. It is a feature available in most modern version control systems. A Git project can have more than one branch. These branches are a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug, you spawn a new branch to summarize your changes. So, it is complex to merge the unstable code with the main code base and also facilitates you to clean up your future history before merging with the main branch.

Git Master Branch:-

The master branch is a default branch in Git. It is instantiated when first commit made on the project. When you make the first commit, you're given a master branch to the starting commit point. When you start making a commit, then master branch pointer automatically moves forward. A repository can have only one master branch.

Master branch is the branch in which all the changes eventually get merged back. It can be called as an official working version of your project.

Operations on Branches :-

We can perform various operations on Git branches. The git branch command allows you to create, list, rename and delete branches. Many operations on branches are applied by git checkout and git merge command. So, the git branch is tightly integrated with the git checkout and git merge commands.

Create Branch:-

You can create a new branch with the help of the git branch command. This command will be used as:-

List Branch:-

You can List all of the available branches in your repository by using the following command.
Either we can use git branch - list or git branch command to list the available branches in the repository.

Delete Branch:-

You can delete the specified branch. It is a safe operation. In this command, Git prevents you from deleting the branch if it has unmerged changes. Below is the command to do this.
we can use git branch -d<branch name> command to delete the branch

Switch branch:-

Git allows you to switch between the branches without making a commit. You can switch between two branches with the git checkout command. To switch between the branches, below command is used:

Rename branch:-

We can rename the branch with the help of the git branch command. To rename a branch, use the below command:
syntax is : git branch -m <old branch name><new branch name>

Merge Branch

Git allows you to merge the other branch with the currently active branch. You can merge two branches with the help of git merge command. Below command is used to merge the branches:
Syntax is : git merge <branch name>

Git Revert & Git Reset

git revert:

  • Purpose: The git revert command is used to undo a commit by creating a new commit that undoes the changes made in the previous commit. It's a safe way to revert changes without rewriting the project's history.

  • How it works: When you run git revert, Git creates a new commit that contains the inverse changes of the specified commit. This allows you to maintain a clear history, especially in collaborative projects.

  • Usage:

      git revert <commit-hash>
    
  • Example:

      git revert abc123
    
  • Note: This is a good choice when you want to undo changes that have already been shared with others, as it doesn't alter the commit history.

git reset:

  • Purpose: The git reset command is more powerful and can be used to reset the current branch's HEAD to a specified state. It's often used to unstage changes, move the branch pointer, or even remove commits.

  • How it works: Depending on the specific mode used with git reset, it can remove commits, unstage changes, and move the branch pointer to a different commit.

  • Usage:

      git reset <options> <commit-hash>
    
  • Examples:

    • git reset --soft <commit-hash>: Keeps changes in your working directory and stages them. It moves the branch pointer to the specified commit.

    • git reset --mixed <commit-hash>: Keeps changes in your working directory but unstages them. It moves the branch pointer to the specified commit.

    • git reset --hard <commit-hash>: Discards changes in both the working directory and staging area. It moves the branch pointer to the specified commit.

Git Rebase & Git Merge

git rebase:

  • Purpose: The git rebase command is used to incorporate changes from one branch into another by moving or combining a sequence of commits. It provides a cleaner and more linear project history compared to traditional merging.

  • How it works: When you run git rebase, Git takes the commits from the source branch and replays them on top of the target branch. This results in a linear commit history without unnecessary merge commits.

  • Usage:

      git checkout <target-branch>
      git pull origin <target-branch>  # Fetch the latest changes
      git rebase <source-branch>
    

git merge:

  • Purpose: The git merge command is used to combine changes from one branch into another by creating a new commit that has both branch's changes. It results in a merge commit, indicating the point where the branches were integrated.

  • How it works: When you run git merge, Git creates a new commit that has two parent commits—the latest commit from the source branch and the latest commit from the target branch.

  • Usage:

      git checkout <target-branch>
      git pull origin <target-branch>  # Fetch the latest changes
      git merge <source-branch>