0

if I have a > b > c versions. I want go back to b and add some changes So what I have now is a > b > b+newChanges on master branch, I hope that makes sense.

Amr Aly
  • 3,871
  • 2
  • 16
  • 33
  • What about **c**? Do you want the set of changes introduced from b to c to no longer be part of the master branch? Do you want to save it on a different branch? Also, do you want to still have b itself as a revision, so that your master branch history includes a, then b, then b+newChanges? Or do you want to omit b, so that your master branch history goes directly from a to b+newChanges, effectively combining the changes from a to b with the newChanges in a single change set? – David Z Dec 28 '16 at 02:32
  • I want the master branch version to be b+newchanges – Amr Aly Dec 28 '16 at 02:44
  • Yes, but what do you want to be the revision _before_ that? Should it be b, or a? In other words, do you want to have the ability to revert to b or not? – David Z Dec 28 '16 at 02:45
  • It would be fine if b is the previous version like a > b > b+changes – Amr Aly Dec 28 '16 at 02:48
  • OK, I would advise updating your question to say that. – David Z Dec 28 '16 at 02:54

4 Answers4

1

If you haven't push the commit to the remote, try this:

  1. checkout a new branch from the b commit
  2. make changes and commit to the new branch
  3. git rebase from new branch to previous branch

commands are below:

git checkout <b commit hash> -b <new branch name>
#make changes 
git add .
git commit
git rebase <new branch name> <previous branch name>

If you have already push the a > b > c to remote, you shouldn't change the commit history, just make changes and commit it.

jayxhj
  • 2,829
  • 1
  • 19
  • 24
1

In git, a branch is basically just a friendly name for a revision - keeping in mind that a git revision also includes information about its history. The main advantage of branches over revision hashes is that you can change which revision a branch refers to; for example, when you commit some new code, git updates the currently checked-out branch to refer to the brand new revision you just created.

So in your case, you have (at least) three revisions, a, b, and c. Revision a is the parent of b, which in turn is the parent of c, and the master branch refers to revision c. What you need to do is

  1. Ensure that you have your master branch checked out.

    git checkout master
    
  2. (Optional) If you want to keep access to c, you should create another branch that will refer to c. You can do this by running the following command:

    git branch newbranch
    

    and that will create a branch called newbranch which refers to revision c.

  3. Reset the master branch so that it refers to revision b. To do this, with master checked out, run

    git reset --hard HEAD^
    

    The notation HEAD^ means "the revision prior to HEAD", and HEAD is a shorthand for the currently checked-out revision. (Kind of like a special branch name.) In your case, HEAD is c, but then after running this command, HEAD will be b. If you skipped step 2, you have now lost access to revision c.1

  4. Make your desired new changes.

  5. Commit your new changes into a new revision.

    git commit -a
    

    to commit all changed files that were already being tracked by git, or

    git add [files]
    git commit
    

    to explicitly specify what files to commit. You have now created a new revision, d, whose parent is b and which includes the new changes.


1 Well, not really, but that's advanced usage.

David Z
  • 128,184
  • 27
  • 255
  • 279
  • So can i checkout to the newbranch without adding the current version to statging area i mean to say: git add . – Amr Aly Dec 28 '16 at 03:18
  • I'm not quite sure what you mean, but if you have any changes that have not yet been committed when you run `git branch newbranch`, those changes will not be included in **newbranch**. – David Z Dec 28 '16 at 03:20
0

Try this,

git reset HEAD~1
# make changes
git commit -a --amend 

This will combine all your changes from b and c and the new changes into one new b commit. If you want to loose you changes from c use reset --hard.

akuhn
  • 27,477
  • 2
  • 76
  • 91
  • It worked but i still have no access to master branch it says ((dc8c622...)) insted of master sorry if that stupid question but i am new to git – Amr Aly Dec 28 '16 at 02:40
  • 1
    Is that a detached head? Try the 2nd answer here, http://stackoverflow.com/questions/5772192 – akuhn Dec 28 '16 at 02:49
0

I have found the answer right here stackoverflow.com/questions/5772192 what i did looks like this

git reset HEAD~1

added some changes and added them to stage area

git branch -f master temp

git checkout master

Community
  • 1
  • 1
Amr Aly
  • 3,871
  • 2
  • 16
  • 33