Git

Git fast-forwarding | Explained

On Git, developers can perform several operations, and merging is one of them. More specifically, they can perform the fast-forward merge operation when it is required to merge the two branches without having extra commits. In fast-forward merge operation, Git moves the current branch HEAD pointer to the target local branch HEAD pointer.

This guide will talk about:

Let’s start!

What is Git Fast-forwarding?

In Git’s fast-forwarding operation, the source branch pointer is moved to the desired branch pointer without creating an extra merge commit.

How to Perform Fast-forwarding Operation?

To perform a Git fast-forwarding, first, navigate to the Git directory and initialize the Git local repository. Create and modify the file. Then, update the repository by committing. Again, modify the file and commit the added changes. Next, create a new local branch and switch to it. After that, update the previously created file into the new branch. Lastly, execute the “$ git merge <branch-name>” command to perform the fast-forwarding operation.

Now, it’s time to perform the fast-forwarding operation!

Step 1: Navigate to Git Local Repository

To move to the desired local repository, run the “cd” command:

$ cd "C:\Users\nazma\Git\Test_4"

Step 2: Initialize Repository

Initialize the Git local repository by executing the “git init” command:

$ git init

Step 3: Create or Modify File

To create or modify update a file, use the following command:

$ echo My_file>file1.txt

Step 4: Track file

Run the “git add .” command to track a newly created file:

$ git add .

Step 5: Update Repository

Run the “git commit” command with the “-m” option to update the repository and add the commit message:

$ git commit -m "first commit"

Step 6: Modify File

Update the previously created file using the “echo” command:

$ echo My_file>>file1.txt

Step 7: Add File

Execute the “git add .” command to add the changes to the Git staging area:

$ git add .

Step 8: Commit Changes

Next, commit all the added changes to the repository by executing the “git commit” command:

$ git commit -m "Second commit"

Step 9: Create Local Branch

Now, run the “git branch” command to create a new local branch:

$ git branch beta

Step 10: Switch Local Branch

Now, switch to the newly created branch by utilizing the following command:

$ git switch beta

Step 11: Update File

Run the “echo” command to update the file of another branch in the current branch:

$ echo first_file>>file1.txt

Step 12: Track File

To track all added changes to the staging area, execute the “git add .” command:

$ git add .

Step 13: Update Repository

Now, update the local repository with the newly added changes:

$ git commit -m "third commit"

Step 14: Switch to Previous Branch

Run the “git switch” command to switch back to the previous local branch:

$ git switch master

Step 15: Merge Branch

Lastly, execute the “git merge” command to merge the previous branch with the current branch:

$ git merge beta

As you can see, the source branch pointer is moved to the specified branch pointer without creating an extra merge commit:

We have offered the method to perform Git fast-forwarding.

Conclusion

To perform a Git fast-forwarding, first, navigate to the Git directory and initialize the Git local repository. Create and modify the file. Then, update the repository by committing. Again, modify the file, commit the added changes to the repository, and update it. Next, create a new local branch and switch to it. After that, update the previously created file into the new branch. Lastly, execute the “$ git merge <branch-name>” command to perform the fast-forwarding operation. This study explained the method to perform Git fast-forwarding.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.