Blog Post

Moving One File Across Git Branches: #SQLNewBlogger

,

I was working on some branching and merging with a customer and they wanted to move a file from one branch to another without taking the entire commit. I had to dig in a bit and see how to cherry pick a file, and not a commit. This post looks at how this can work.

I’ll do this in the git CLI. I’m sure it works in many clients, but when I do something strange or new, I like looking at the CLI. Mostly because when I make a mistake, the clients send me to the CLI often anyway, so I get comfortable there.

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers. You can see all posts on Git as well.

A Simple Setup

First, grab a repo. In this case, I had a branching/merging repo I use with customers to show some things. I’ll use the repo at: https://github.com/way0utwest/BranchMerge

For me, I have a main branch, as well as a dev and qa branches. There are likely some feature ones as well.

To start with, let’s check main. Everything is up to date (I pulled already):

2024- 08_ 0059

Let’s also check QA. Same thing.

2024- 08_ 0060

I’ll now make some changes on QA, adding a file and changing two others. Once I do this, here is my status, pre-commit.

2024- 08_ 0061

If I don’t commit, these files don’t exist, and I could change branches and add them there. However, once I commit, I might want to move them in a certain way. I’ll commit and my status is clean.

2024- 08_ 0062

Now, I could switch to main and do this to move one file:

git checkout qa -- README.md

However, I’m just doing that without review. So, I wouldn’t do that and you shouldn’t either. Instead, let’s create a PR.

Using a Branch for Peer Review

A PR is a pull request, but it also means we ask for some peer review. In my case, I’ll use this code to create a new branch and then pull in two changes: my modified readme and one of the other changes.

git checkout -b r9-qa

git checkout qa -- README.md

git checkout qa -- "V6__create proc gettwo.sql"

Ignore my typos, but I’ve run 4 commands: checkout 3 times and status once.

2024- 08_ 0063

Now I’ll commit and push these changes to my r9-qa branch.

2024- 08_ 0064

Once I do that, Github detects a push and asks me to create PR. I do, and I see the PR with these changes.

2024- 08_ 0065

Now I can proceed with my flow, and my cherry picked changes are captured.

There is a git cherrypick command, but often I find I need random files from multiple commits, while ignoring others in the commit. This works well for database releases.

A few references:

SQL New Blogger

This post took my about 20 minutes to write. However the learning and experimentation took over an hour as I read various links and dug into the docs and various posts.

This is a very useful skill, and one you can discuss in an interview. Write a similar post and you’ll be prepared for this type of question in an interview.

Original post (opens in new tab)
View comments in original post (opens in new tab)

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating