Create A New Git Branch With Current Local Changes Saved By Example
Maybe your like
Git branch create with local changes example
Don’t worry, you can easily create a new Git branch without losing your local changes. Just use Git’s switch or checkout commands, and your new branch will be created with all of your current changes left intact. Just run the following command:
git@branchMINGW64 /c/example (master) git checkout -b new-git-branch git@branchMINGW64 /c/example (new-git-branch) lsThe new Git branch will be created the local changes you made on the other branch waiting there for you. Nothing has been deleted.
Save current changes in a new Git branch
It is worth mentioning that all of those local files you added to the original branch are still there. So if you created a bunch of files in the master branch before you created the new Git branch with your current changes, the master branch is still polluted with the files you added to the index before the switch to the new branch happened. If you want to clear your changes from the master branch, you can perform a git clean after the new branch is created.
The sequence to create a new Git branch with local changes in tact, but remove those changes from the original branch, the sequence of commands would look like this:
git@branchMINGW64 /c/example (master) git swtich -b new-git-branch git@branchMINGW64 /c/example (new-git-branch) git add . git@branchMINGW64 /c/example (new-git-branch) git commit -m "Add local changes to new git branch" git@branchMINGW64 /c/example (new-git-branch) git switch master git@branchMINGW64 /c/example (master) git clean -fNote that in the above example, we used Git switch, not checkout. The Git switch command came out in 2020, and is preferred over the git checkout command.
When this sequence of Git commands is completed, the new Git branch will have all your current changes stored locally, while the original branch where you inadvertently wrote some code will have its working tree reset to the state at the point of its last commit.
You can do a checkout and create a new branch with all local and current changes transferred over.
Cameron McKenzie is an AWS Certified AI Practitioner, Machine Learning Engineer, Solutions Architect and author of many popular books in the software development and Cloud Computing space. His growing YouTube channel training devs in Java, Spring, AI and ML has well over 30,000 subscribers.
| Git, GitHub & GitHub Copilot Certification Made Easy |
|---|
| Want to get certified on the most popular AI, ML & DevOps technologies of the day? These five resources will help you get GitHub certified in a hurry.
Get certified in the latest AI, ML and DevOps technologies. Advance your career today. |
Tag » How To Change Branch In Git Without Commit
-
Git: Switch Branch And Ignore Any Changes Without Committing
-
How To Change The Branch Without Committing So That I Can ... - Reddit
-
Git (revision Control): If You Do Not Commit And You Switch Branches ...
-
Move Existing, Uncommitted Work To A New Branch In Git | Baeldung
-
Git Switch Branch – How To Change The Branch In Git - FreeCodeCamp
-
Git - Git-checkout Documentation
-
Git: Switch Branch And Ignore Any Changes Without Committing
-
Switching Branch Without Commit In Working Directory - DQ Courses
-
How To Switch Branch On Git - Devconnected
-
Learn Git Collaboration - Nulab
-
Using Git Switch To Change Branches - Tempertemper
-
Stashing Changes - GitHub Docs
-
Git Stash: Save Local Changes Without Commit In Git - Fedingo
-
Git Stash - How To Save Your Changes Temporarily