Git Rebase and Branch Switching

Rebase

Rebase is the process of changing the commit history of a branch by applying commits from another branch. Instead of using merge to combine changes, rebase allows you to insert new commits into the current branch's commit history without creating merge commits.

For example, let's say you have two branches: feature-branch and main. You are working on feature-branch and want to apply the latest commits from main onto your current branch. You can use rebase to achieve this:

git checkout feature-branch  
git rebase main  

When you run this command, Git will take the commits from main and apply them onto feature-branch. This means that all the commits on feature-branch will appear after the commits from main. The result is a cleaner and more readable commit history on feature-branch.

However, when using rebase, it's important to note that changing commit history can impact publicly shared branches. Therefore, if you have already pushed commits from your current branch to a remote repository, it's generally advised not to use rebase on that branch to avoid conflicts and a messy commit history.

 

Branch Switching

Branch switching in Git refers to the process of moving from one branch to another. When you switch branches, Git moves the HEAD pointer to the new branch, allowing you to work on that branch and make changes without affecting other branches.

For example, let's say you have branches feature-branch and main. To switch to feature-branch, you would use the following command:

git checkout feature-branch

After switching branches, you can make changes in the working directory. All commit, add, and checkout commands will apply to the current branch.

For example, if you add a new file and commit it on feature-branch, only that branch will contain the commit, while main remains unaffected. This allows you to develop separate features, fix bugs, or work on different versions of the code independently. You can switch between branches whenever needed to work on each branch separately.