0

I'm using git and github for my personal project and my git knowledge is not advanced. I've been previously using branches to which I commit my changes which I then merge with remote master.

But the last time I committed to Github I was trying to be fancy and tried to rebase my branch, something I had never done before. I can't remember what the exact commands were but they were something like this:

git checkout master
git pull
git checkout myBranch
git pull
git rebase -i master
git checkout master
git merge myBranch

However, I must have done something wrong because the commands resulted in that I lost my changes and I had to redo everything. I also lost all my local branches and now they're all marked as remote and I only have a local master branch (see picture).

local branches lost

So my question is. I just want to push my current changes to my remote master without losing all my changes like last time. What is the safest way to do that? Would a simple:

git pull
git add -A
git commit -m "msg"
git push

be enough?

edit:

A screenshot of git log --oneline --graph enter image description here

Rick
  • 199
  • 1
  • 2
  • 12
  • Can you add the output of `git log --oneline --graph` in the question so that your situation becomes more clear? – Jdeep Jun 23 '21 at 18:04
  • I've edited my answer. I'm really sorry how messy this is. I haven't been totally loyal with git strategies in this project. All I want is to make sure is that I push my latest changes correctly to remote master without deleting anything. – Rick Jun 23 '21 at 19:04
  • In which commit did you rebase it? – Jdeep Jun 23 '21 at 19:45
  • It was in commit 0b3ca86. In the following commit I remade my changes. It seems that I’m currently switched to head master origin but I have no clue how that could’ve happened as I never work directly on master. – Rick Jun 23 '21 at 19:53
  • If you are currently in `d57ae24` , then `git reset --soft HEAD~5` would take you back to `03e4fce` without deleting the commits that came after it (All those commits will be in the staging area. You can do a `git status` to check) . – Jdeep Jun 23 '21 at 19:56
  • But what if I want to keep everything as is and simply push my latest changes? Would doing a ```git reset``` bring back my local branches? – Rick Jun 23 '21 at 20:00
  • 1. No problem in pushing your latest changes. 2. No., `git reset ` only restores commits. For restoring branch, [see this](https://stackoverflow.com/q/3640764/12982627) – Jdeep Jun 23 '21 at 20:02
  • So I could go for a simple add, commit and push commands to push on the master this time and then switch to a new branch for next changes? – Rick Jun 23 '21 at 20:03
  • 1
    Yes. After `git reset`, you have to `add` , `commit` and `push`if you want the lost changes – Jdeep Jun 23 '21 at 20:05
  • Okay I see. But if I have already re-done my changes that were lost by hand, then I should be able to simply jump to ```add```, ```commit``` and ```push``` commands if I'm not mistaken. – Rick Jun 23 '21 at 20:14
  • The real mystery, in my opinion, is what deleted all the (local) branch names. That's not all that easy to do by accident. – torek Jun 23 '21 at 22:12
  • @Rick, ya if you have redone it , no problem. You can push to remote repository. Pro tip: Remember almost everything that you have committed in git is recoverable if deleted. – Jdeep Jun 24 '21 at 02:15
  • I'll keep that in mind @Jdeep thanks. I pushed and kept my changes and am now working on a new branch. – Rick Jun 24 '21 at 07:45

1 Answers1

1

When in doubt, make a branch. If you're not sure how to rebase, make a new branch and rebase that: if it works out, great; if it doesn't, you can go back to the original and try again with another new branch.

However, I must have done something wrong because the commands resulted in that I lost my changes and I had to redo everything.

It's hard to know what happened when you don't remember what you did. Rebasing changes your commit history; depending on specifically what you did, you might have reordered commits so that that hid your changes, or removed some commits entirely. It's a powerful tool, so take some time getting to know it before you use it on anything important, and always use it with care.

I also lost all my local branches and now they're all marked as remote and I only have a local master branch

Rebasing lets you change the commit history in a particular branch; the impact on the other branches was likely due to some other thing that you did.

So my question is. I just want to push my current changes to my remote master without losing all my changes like last time.

If the remote is called "origin" then you'd normally just do:

git push origin

If your branch isn't already tracking a remote branch (unlikely with your master branch) git will tell you how to use the --set-upstream option so that a it knows what branch to push to. If the branch you're pushing to has changed since you last pulled, you might get conflicts that you'll need to resolve. Rebasing before pushing is one way to deal with that; git essentially takes all your changes and puts them aside, pulls down the new commits, and then "replays" your changes on top of the new commits. But you may still run into merge conflicts that you'll need to resolve manually... that's just what happens when you and someone else change the same parts of the same files and git can't tell whose version to use.

Caleb
  • 124,013
  • 19
  • 183
  • 272