Git - Save Work in Progress - No changes after branching
I am trying to save changes using Git before pushing. This means that I will not lose my job if my system goes down.
I've looked through git stash
which seems nice, but it seems like it's just local, so it doesn't help.
I read that I have to create a new branch and push it. Well what I did is at the bottom and when I went back to the master branch my work files were no longer showing.
I have two questions:
What have I done wrong?
Is there a better way to save changes before you're ready to push?
My attempt:
// In master, where I have work, create and navigate to a new branch
git checkout -b branch1
// Stage, then push, changes to new branch
git add .
git commit -m "committing changes from new local branch"
git push
fatal: current branch1 does not have an upstream.
// Create a remote branch and push the new local branch to it (now the changes are on the server)
git push --set-upstream origin branch1
// Back to the wizard to work
git checkout master
// Well, the changes aren't showing up again in master? Where did they go?
git status
On branch master, your branch has been updated with "origin / master". do nothing, the working tree is clean
source to share
You haven't done anything wrong. Your changes that were in the Git phase have been committed to branch1
and redirected to the server. Hence, after you have done this, the stage has been cleared.
To revert changes from branch1
to master
, you can try merging branch1
into master
, for example.
git checkout master
git merge branch1
I found it odd that you are working directly on a branch master
. Typically, you will already be working on a feature branch branch1
and then again want to merge the entire feature back into master
. I mention this scenario because it will make the operation smoother. You can make a temporary commit to branch1
and then push. When you get back to work, you can keep working and then change the commit with:
git commit --amend
You will now be in your feature branch with a single commit containing all the work you intended to do.
source to share
master
is the default branch name. You created a new branch based on master
and committed your changes there. This branch now contains all the commits that were in master
, plus the new commit. Then you pushed the new branch to the remote. Everything is fine so far.
Then you switch to master
. What for? It does not contain what you were working on. Your new branch contains what you worked on. Keep working on this thread. When you are done with this part of your work, merge it back into master
.
Also: stop thinking about using Git as a "save". It's a way of recording and communicating changes to your codebase over time. This is not a fantastic store of value. This is a common beginner's mistake.
source to share