JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Commit changes to a Git branch

Author:JIYIK Last Updated:2025/04/20 Views:

In this article, you'll learn how to save commits to a new or existing branch in Git.

This article explains how to move commits to:

  1. A new branch
  2. Existing branches

You’ll often find yourself committing the same staged changes to different branches. Git allows you to do this conveniently, as shown below.


Moving commits to a new branch in Git

In this section, you will learn how to move commits from your workspace branch to a new branch.

Create a new branch containing all commits. Use git branchthe command to initialize a new branch.

git branch <new-branch>

The above command will create a branch, a new-branch.

Use git resetthe command to reset the commit to the last update.

git reset --keep HEAD~N

We use --keepthe -d backup option to back up uncommitted changes.


Moving commits to an existing synced branch in Git

To better understand this feature, we will use a practical example. We submit to <wrong branch>instead of <right branch>.

Assuming the two are in sync, how do you bring the commits to <right branch>?

Use git checkoutthe command to navigate to an existing branch.

git checkout <right branch>

Use git mergethe command to move commits.

git merge <wrong branch>

To delete bad submissions, go to <wrong branch>.

git checkout <wrong branch>

Use git resetthe command to revert the commit.

git reset --keep HEAD~N

Moving commits to an existing branch in Git

Let's use a practical example to better understand this. If you wanted to move a specific commit without merging the branch, how would you do it?

In the following example, we will move a commit from <wrong branch>to <right branch>.

Switch to <right branch>.

git checkout <right branch>

Use git cherry-pickthe command and the commit's hash to move it, as shown below.

git cherry-pick <sha1-commit-hash>

Go back to and remove the commit 错误的分支using the command.git reset

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Ignore everything except certain files in Git

Publish Date:2025/04/20 Views:151 Category:OPERATING SYSTEM

This article outlines the steps to make Git ignore all but a few files in a Git repository. The .gitignore file is a useful Git utility that allows us to tell Git which files to track and which files not to track. If you want your .gitignor

Tagging an older commit in Git

Publish Date:2025/04/20 Views:115 Category:OPERATING SYSTEM

This article outlines the steps required to tag old commits in Git. We use git tags to mark specific points in our commit history as important. Typically, a git tag marks a stable release or an important milestone in a project. How do you t

List all tags in Git

Publish Date:2025/04/20 Views:120 Category:OPERATING SYSTEM

This article will teach us how to list all tags in Git. Git is a version control system that tracks changes in project directories using a Git repository. Changes made to files are tracked in a Git repository using commits. Using tags, we c

Recovering a reverted Git commit

Publish Date:2025/04/20 Views:197 Category:OPERATING SYSTEM

This article outlines the steps required to revert a reverted Git commit. By the end of this article, you will have the necessary knowledge to recover a reverted commit without rewriting your commit history. Recovering a reverted Git commit

Git merge repository

Publish Date:2025/04/20 Views:160 Category:OPERATING SYSTEM

When working on a project with multiple team members having separate repositories for each team, at some point in time we come across a situation where we want to combine these separate repositories into one master repository to deploy all

Git push to another branch with a different name

Publish Date:2025/04/20 Views:57 Category:OPERATING SYSTEM

git push It has a rich set of options that allow you to use the full power of Git. One of them is its source:destination refspecs parameters. We use these git push to go to a specific branch with a name of our choosing. Finally, we'll see s

Issues to note when installing Apache on Linux

Publish Date:2025/04/08 Views:78 Category:OPERATING SYSTEM

As the most commonly used web server, Apache can be used in most computer operating systems. As a free and open source Unix-like operating system, Linux and Apache are a golden pair. This article will introduce the installation and use of A

提交对 Git 分支的更改

Publish Date:2023/03/31 Views:178 Category:Git

在本教程中,我们将了解如何将提交保存到 Git 中的新分支或现有分支。你经常会发现自己将相同的暂存更改提交到不同的分支。Git 允许你方便地执行此操作,如下所示。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial