I'm trying to save my changes, using Git, before actually doing a push. This is so I don't lose my work if my system crashes.
I looked into git stash, which seems nice, but it appears that's just local, so that doesn't help.
I read that I should create a new branch and push that. Well, that's what I did, below, and when I went back to the master branch, my working files were no longer showing up.
I have two questions:
What did I do wrong?
Is there a better way to save changes before you're ready to push?
My attempt:
// In master, where I have work in progress, create and move to a new branch
git checkout -b branch1
// Stage, then push, the changes in the new branch
git add .
git commit -m "committing changes from new local branch"
git push
fatal: The current branch branch1 has no upstream branch.
// Create remote branch and push new local branch to it (changes now on server)
git push --set-upstream origin branch1
// Go back to master to work
git checkout master
// Uh oh, changes aren’t showing up back in master? Where did they go?
git status
On branch master Your branch is up-to-date with 'origin/master'. nothing to commit, working tree clean
You didn't do anything wrong. Your changes, which were in the Git stage, were committed on branch1 and pushed to the server. Hence, after you did that commit, the stage was cleaned.
To get back the changes from branch1 into master, you can try merging branch1 into master, e.g.
git checkout master
git merge branch1
I find it odd that you are working directly on the master branch. More typically, you would already be working on some feature branch branch1 and then be looking to merge the entire feature back into master at a later point. I mention this scenario, because it would allow a much smoother operation. You could make a temporary commit on branch1, then push. When you return to work, you can keep working, and then amend the commit via:
git commit --amend
Now, you would be on your feature branch with a single commit containing all the work you had intended to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With