Pulling and rebasing your code – Source Code Management with Git and GitOps

Pulling code involves downloading up-to-date code from the remote to your local repository. Rebasing means applying your changes on top of the latest remote commit. It is a best practice to pull and rebase your changes on top of what already exists in the remote repository.

Let’s do so using the following command:

$ git pull –rebase

remote: Enumerating objects: 5, done.

remote: Counting objects: 100% (5/5), done.

remote: Compressing objects: 100% (2/2), done.

remote: Total 3 (delta 0), reused 0 (delta 0),pack-reused 0

Unpacking objects: 100% (3/3), 652 bytes | 130.00 KiB/s, done.

From github.com:<your-github-username>/first-git-repo

d11c139..f5b7620  master         -> origin/master

Auto-merging file1

CONFLICT (content): Merge conflict in file1

error: could not apply e411e91… Added fourth line Resolve all conflicts manually, mark them as resolved with

“git add/rm <conflicted_files>”, then run “git rebase –continue”.

You can instead skip this commit: run “git rebase –skip”.

To abort and get back to the state before “git rebase”, run “git rebase –abort”.

Could not apply e411e91… Added fourth line

Now, we have another issue: we are unable to rebase our commit as we’ve tried to modify a file that has been modified by someone else. Git wants us to check the file and make appropriate changes so that the changes are applied correctly. This situation is known as amerge conflict. Git also provides us with the file that contains the conflict. Let’s open the file with a text editor and make the appropriate changes.

The current file looks like this:

This is first line

This is second line

<<<<<<< HEAD

This is third line

=======

This is fourth line

>>>>>>> e411e91 (Added fourth line)

The portion depicted by HEAD is the line in the remote repository and shows the recent changes made remotely. The e411e91 commit shows the changes that we made locally. Let’s change the file to the following and save it:

This is first line

This is second line

This is third line

This is fourth line

Now, let’s add the file to the staging area and continue the rebase using the following commands:

$ git add file1

$ git rebase –continue

[detached HEAD 17a0242] Added fourth line

1 file changed, 1 insertion(+)

Successfully rebased and updated refs/heads/master.

Now that we’ve rebased the changes, let’s look at the status of the Git repo by running the following command:

$ git status

On branch master

Your branch is ahead of ‘origin/master’ by 1 commit.

(use “git push” to publish your local commits)

nothing to commit, working tree clean

As we can see, we’ve added a single commit that we need to push to the remote repository. Let’s do that now using the following command:

$ git push

Enumerating objects: 5, done.

Counting objects: 100% (5/5), done.

Delta compression using up to 12 threads

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 297 bytes | 148.00 KiB/s, done.

Total 3 (delta 0), reused 0 (delta 0), pack-reused 0

To github.com:<your-github-username>/first-git-repo.git f5b7620..17a0242 master -> master

And this time, the push worked successfully.

In most cases, you would normally need to take a copy of the main code and make changes to it to test new features. You might also want someone to review your changes before they are merged into the main code base. Git allows you to manage that by using Git branches. We’ll look at Git branches in the next section.

About the Author

Leave a Reply

Your email address will not be published. Required fields are marked *

You may also like these