Merge pull request from command line

When you get a pull request and you want to merge that then there are couple of options you can do for this. You can use GitHub web interface or you can use command line to merge the changes.

The first step is to checkout the pull request branch in your local machine and review the changes. If you are ok with the changes and want to merge it, then you can squash the changes in the pull request branch with proper message and then rebase in to master branch.

Today I am doing a demo with these steps. I am using GitHub and Windows Terminal with PowerShell Core.

First, I am creating a new repository in GitHub.

Next, I am cloning this newly created repository in my local machine.

git clone https://github.com/Arnab-Developer/pr-merge-command-line.git

Moving into the repository folder.

cd .\pr-merge-command-line\

I am adding a new text file and commit as initial commit into master branch and push to GitHub.

Creating a pull request

I am creating a new branch ‘b1’.

git branch b1
git checkout b1

Now in the text file, I am adding some changes and creating three commits in ‘b1’ branch and push to GitHub.

At this moment my git log looks like this.

git log --oneline --graph

Now I am going to GitHub and create a new pull request from branch ‘b1’ to ‘master’.

Merging the pull request

Now the pull request has been raised and I have reviewed it and it is ready to merge in master. I want to make my git history clean so I am going to squash the commits in b1 branch and rebase that on master.

Squash the commits of b1

git checkout b1
git rebase -i cc6b9c0

This will open my default git text editor which is notepad in my case. I can see that there are three commits showing as ‘pick’.

pick 25a4708 commit 1 in branch 1
pick 965c50f commit 2 in branch 1
pick 017ab08 commit 3 in branch 1

I am changing this and doing the first as pick and second and third as squash so that they can squash into first commit.

pick 25a4708 commit 1 in branch 1
squash 965c50f commit 2 in branch 1
squash 017ab08 commit 3 in branch 1

It also giving me option to change the commit message which is in this case I am giving the below one.

commit for branch b1

I am pushing the changes to GitHub.

git push --force

The git log looks like this.

Merging the pull request

I am doing a rebase on master from b1.

git rebase master
git checkout master
git rebase b1
git push

Now if I go to GitHub, I can see that my pull request has been merged.

Now I can delete the b1 branch.

git branch b1 -d
git push origin -d b1

At this moment my git log looks like this.

You can check the repository in my GitHub https://github.com/Arnab-Developer/pr-merge-command-line.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s