Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 528867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:01:54+00:00 2026-05-13T09:01:54+00:00

Here’s the situation. I’m working on the master branch. I create file1 and commit.

  • 0

Here’s the situation. I’m working on the master branch. I create file1 and commit. I create file2 and commit. Whoops. I may want to use file2, someday, but it’s definitely not something that should be put in the master branch. So that I don’t lose file2 I use

git checkout head~1
git branch new-branch
git checkout new-branch

so that I can continue developing. I add file3 to new-branch. If you’ve been paying attention, I’ve got two branches, master that contains “file1” and “file2” and new-branch that contains “file1” and “file3”.

Now is the time to get the changes I’ve made back into the master branch. What’s the best way to do this? I definitely want the head of the master branch to point at the files as they appear in new-branch, but I also don’t want to lose the work I’ve done in file2 by doing a reset, in case I want to use it.

Keep in mind this is a simplification. Instead of just three files, I’ve got a dozen files with tens of lines of code being changed all over the place all with multiple commits. I certainly hope the solution isn’t to do a file-by-file merge/checkout, because that would be a huge pain.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:01:54+00:00Added an answer on May 13, 2026 at 9:01 am

    I’m working on the master branch. I
    create file1 and commit.

    date >file1
    git add file1
    git commit -m 'added file1'
    

    I create file2 and commit.

    date >file2
    git add file2
    git commit -m 'added file2'
    

    Whoops. I may want to use file2,
    someday, but it’s definitely not
    something that should be put in the
    master branch.

    Oops. Very simple. Make a new branch from where you are:

    git checkout -b savingfile2
    

    This will make the file2 change the commit for savingfile2. Now go back and unwind one step on master

    git checkout master
    git reset --hard HEAD~1
    

    At this point, the commits leading up to master will reflect the addition of file1,
    and the additional commit between master and savingfile2 will be the addition of file2 to that.

    If you make more changes to master, and then want to bring file2 back eventually, you’ll want to rebase that side-branch onto the new master:

    date >file3
    git add file3
    git commit -m 'adding file3'
    date >file4
    git add file4
    git commit -m 'adding file4'
    

    And now we finally want file2:

    git checkout savingfile2
    git rebase master # might need to fix conflicts here
    git checkout master
    git merge savingfile2 # will be a fast-forward
    git branch -d savingfile2 # no need any more
    

    That should do it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 376k
  • Answers 376k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer answering my own question :) I found the best way… May 14, 2026 at 8:36 pm
  • Editorial Team
    Editorial Team added an answer First of all, make sure you know what you're doing… May 14, 2026 at 8:36 pm
  • Editorial Team
    Editorial Team added an answer From the LiveDocs - Basics of working with sound: [...]… May 14, 2026 at 8:36 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.