Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (27.5k points)

I have worked on a local branch and also pushed the changes to remote. I want to revert the changes on that branch and do something else on it, but I don't want to lose the work completely. I was thinking of something like creating a new branch locally and copy the old branch there, then I can revert the changes and continue working on the old branch. Is there a better way maybe? Or how do I do this?

2 Answers

0 votes
by (19.4k points)

$ git checkout old_branch

$ git branch new_branch

This command will give you a new branch "new_branch" with the same state as "old_branch".

This command can be combined to the following:

$ git checkout -b new_branch old_branch

0 votes
by (1.2k points)

This is achievable through the creation of a new branch that starts from the current state in which you do not have to lose your work. Here is how to create it:

Create a New Branch:

Code

 git checkout -b new-branch


 

Switch back to your old branch:

Code

 git checkout old-branch

Revert the changes on the old branch:

If you have made a last commit in your code and want to undo that commit.

The code will be:

 Code

 git reset --hard HEAD~1

That means you could hold all of your new branch changes there, reverting later when you may need to again start work on the old branch.

Related questions

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...