Git
How to Pull a Specific Commit from GitHub?
In collaborative development, it’s common to sync your local project with a remote repository on GitHub. Sometimes, though, you may not want to pull the latest changes or the entire history. Instead, you might need a specific commit for testing, debugging, or integrating into your work.
In this blog, we’ll walk through how to pull a specific commit from GitHub to your local repository.
Why Would You Pull a Specific Commit?
There are a few scenarios where pulling a specific commit might be useful:
- Selective Testing: You might want to test specific changes without merging the entire branch.
- Hotfixing: Pulling specific commits can help you quickly apply a fix to your branch.
- Selective Integration: Incorporating only some changes from a colleague’s branch without introducing other recent changes.
Key Terminology
- Commit: A snapshot of your project at a given time, containing changes to files in the repository.
- SHA: A unique hash identifier assigned to each commit.
- Cherry-Picking: A Git command that allows you to apply a specific commit from one branch to another.
Step 1: Identify the Commit SHA
To pull a specific commit, you’ll need its unique SHA identifier. Here’s how to find it:
- Go to your GitHub repository and navigate to the Commits section (usually found under the repository name).
- Browse the commits to locate the one you want.
- Copy the full SHA hash (40 characters) or a shortened version (usually the first 7 characters).
Alternatively, if you already have access to the branch locally, you can use:
git log
This command lists the commits in your branch, along with their SHAs, author names, and messages.
Step 2: Fetch the Latest Changes from GitHub
Before you cherry-pick a commit, it’s a good practice to fetch the latest changes from the remote repository to ensure that you’re working with up-to-date data.
- Open a terminal or Git Bash.
- Run the following command to fetch updates:
git fetch origin
This command brings all new commits, branches, and tags from the remote repository to your local repository without altering your working branch.
Step 3: Cherry-Pick the Specific Commit
Once you have the SHA and have fetched the latest changes, you’re ready to cherry-pick the commit. This process will apply the changes from the selected commit onto your current branch.
- Make sure you’re on the branch where you want to apply the commit:
git checkout target-branch
Replace target-branch
with the name of your branch.
- Use the
git cherry-pick
command with the SHA of the commit:
git cherry-pick <commit-SHA>
Replace <commit-SHA>
with the SHA of the commit you copied earlier. For example:
git cherry-pick abc1234
This command will apply the changes from the specific commit to your current branch.
Note: If there are conflicts between the selected commit and your branch, Git will prompt you to resolve them before completing the cherry-pick. After resolving conflicts, use
git add <file>
to stage the changes, thengit cherry-pick --continue
to finalize the cherry-pick process.
Step 4: Verify the Cherry-Picked Commit
To confirm that the commit has been successfully applied to your branch:
- Check your commit history using:
git log
- You should see the commit at the top of your branch’s history if it was applied successfully.
Step 5: Push Changes to GitHub (Optional)
If you’re working on a shared branch and want to share the cherry-picked commit with others, push your changes to GitHub:
git push origin target-branch
Replace target-branch
with the name of the branch you’re working on.
Alternative: Pulling Specific Commits from a Remote Branch (without Cherry-Picking)
If the commit you need belongs to a different branch, you can switch to that branch temporarily to pull only the desired commit. This approach is helpful when you want to explore the changes in isolation without altering your main branch. Here’s how:
- Checkout the branch containing the commit:
git checkout branch-with-commit
- Use
git log
orgit log --oneline
to identify the SHA of the commit. - Create a new temporary branch at that specific commit:
git checkout -b temp-branch <commit-SHA>
- Now, you can view or test the commit’s changes without modifying your main branch.
- When finished, switch back to your main branch and delete the temporary branch if no longer needed:
git checkout main
git branch -d temp-branch
Summary
Pulling a specific commit from GitHub is straightforward with Git’s cherry-pick command. Here’s a quick recap:
- Identify the Commit SHA: Get the unique identifier for the commit you want.
- Fetch the Latest Changes: Ensure your local repository is up-to-date with the remote repository.
- Cherry-Pick the Commit: Apply the changes from the specific commit to your current branch.
- Push (Optional): Push your branch to GitHub if you want to share the changes.
By following these steps, you’ll be able to selectively pull specific commits from GitHub, making your workflow more efficient and allowing greater control over your project’s history.