user2568374
user2568374

Reputation: 1306

Git - Update current branch from master without committing current branch changes

I have looked at numerous questions on this same topic and they all have different answers and it is confusing.

Some say commit your changes first but I don't want to do this.

I am using Git Hub too, and do not understand how this works with the website commands such as create pull request , compare across forks, try changing your base, etc...

I thought just dragging my current branch to the right square and dragging the repo master branch to the left square and then clicking the Merge Branches button would work but then there is a "sync" button on the upper right that needs to be clicked after that, I guess, and then you need to do a pull request on the website....etc...etc.

Sheesh, in CVS I just clicked update and it brought down all the changes in the Head to my current and that was that.

There are three options I guess. Using git hub, using the website, and/or using the command line. How can this be simplified?

Upvotes: 4

Views: 23455

Answers (4)

RJFalconer
RJFalconer

Reputation: 11662

Your situation:

  • You're on a branch that may or may not be available upstream, which is branched from an old commit of master
  • You have no unstaged changes
  • You have uncommitted changes

"Some say commit your changes first but I don't want to do this"

There's no real reason not to; it's still your private working branch until you push it upstream.

So:

If you've not pushed yet:

  • git commit -m WorkInProgress (or git stash)
  • git rebase master
  • Resolve any conflicts
  • git stash pop if you stashed

If you have pushed already:

  • Ensure your current branch's commits are production quality
  • git stash
  • git pull (will not conflict, but will create merge commit)
  • git stash pop

Upvotes: 1

Makoto
Makoto

Reputation: 106430

It depends on what state your local repository is in relative to upstream.

  • If there are conflicts, then you are better off stashing your work before pulling your branch in. That can be accomplished thus:

    git add . && git stash save
    git pull
    git stash pop
    

    If you don't want to deal with merging, then you can rebase your branch instead, which doesn't require that you save off your work (but will prompt you for conflicts):

    git pull --rebase
    

    You'll have to deal with conflicts using your merge tool of choice.

  • If there are no conflicts, then you can simply pull the branch in.

    git pull
    

Github's role in all of this is simply to provide the remote repository in which you are pulling from/pushing to. There's no need to worry about it unless you don't have a remote set up; then I'd refer you to their wonderful documentation about getting a remote repository set up.

Upvotes: 6

Alim Özdemir
Alim Özdemir

Reputation: 2624

You could do Patches which most IDEs and some tools provide, or if using Intellij IDEA there is a similar construct to stashing which is shelving, these changes are not persisted in git, but rather in IDEAs project files.

But in the end, for your current situation git stash is the way to go.

In the longer run, it's probably better to just read a little bit more about git, since one of the differences between it and CVS is, you have to think two phases now. You don't interact directly with your remote github or whatever. Your local repo is like your former "Save" button and the remote repo sync's with your local. And even in CVS most systems complained if you forgot to save...

Upvotes: 0

Ezzored
Ezzored

Reputation: 925

You can stash your changes in the current branch with git stash. For more information, please see this: https://git-scm.com/book/en/v1/Git-Tools-Stashing

Upvotes: 1

Related Questions