63

Apologies if this seems redundant as I know there are fair amount of questions regarding Merge vs Rebase, but there doesn't seem to be any that throw in 'Branch Default' as well.

You are given a case where you have multiple people working on something (i.e. an Android app in Android Studio) concurrently. What is the best option to update project/pull if someone pushes to the master branch and you want to pull in the new master such that it doesn't overwrite the work you are still working on and have yet to commit and push to master? Android Studio lists 'Merge' 'Rebase' and 'Branch Default' when clicking 'Update Project'. From what it sounds like, I would want to do 'Rebase' (followed by 'Merge'?), but I'm not entirely sure.

Kurt Wagner
  • 3,125
  • 11
  • 39
  • 69

6 Answers6

43

Stashing

The key here is that you have uncommitted work that you want to save. Before trying to merge anything in, you should stash your changes to save your uncommitted changes and clean your working directory.

Run git stash to stash your changes. You should then be able to pull the changes without any issues.

After you have successfully pulled, you can do a git stash apply to re-apply the changes you had made prior to the pull.

Merging and rebasing

Stashing your changes only works if you only have uncommitted changes. If at some point you committed but didn't push you will need to either rebase or merge.

This StackOverflow post has some great information on the differences.

In general, merging is easier, but some believe that it "pollutes" the git history with merge commits.

Rebasing requires additional work, but since you don't have a merge commit it will essentially make the merge invisible.

Again, in your case you shouldn't need to merge or rebase. Simply stash, pull, then apply the stash and it should all be good.

Community
  • 1
  • 1
Bryan Herbst
  • 62,910
  • 10
  • 119
  • 113
  • I want to make this as simple as possible (i.e. only need to have the Android Studio IDE open) Is there a solution that doesn't involve using Git Bash and only utilizes Android Studio? – Kurt Wagner Jun 24 '14 at 18:03
  • Nevermind, reread and seems like based on your answer I want to select 'Merge' when pulling if I want to just use AS – Kurt Wagner Jun 24 '14 at 18:15
  • I wrote a new line: //test, committed and pushed it, confirmed it was pushed to master, removed the //test line locally and tried 'Update Project' with Merge and Branch default and the line didn't return. Not sure why it didn't pull the //test line from master. :X – Kurt Wagner Jun 24 '14 at 18:23
  • 1
    IntelliJ (and Android Studio) [do have a stash option](http://www.jetbrains.com/idea/webhelp/stashing-and-unstashing-changes.html)- it is under VCS > Git > Stash changes. – Bryan Herbst Jun 24 '14 at 18:24
20

enter image description here

According to the IntelliJ IDEA documentation:

Update Type

  • Merge: choose this option to have the merge strategy applied. The result is identical with that of running git fetch ; git merge or git pull --no-rebase.
  • Rebase: choose this option to have the rebase strategy applied. The result is identical with that of running git fetch ; git rebase or git pull --rebase.
  • Branch Default: choose this option to have the default command for the branch applied. The default command is specified in the branch.<name> section of the .git/config configuration file.

Clean working tree before update

In this area, specify the method to save your changes while cleaning your working tree before update. The changes will be restored after the update is completed. The available options are:

  • Using Stash: choose this option to have the changes saved in a Git stash, so you can apply patches with stashed changed even outside IntelliJ IDEA, because they are generated by Git itself.
  • Using Shelve: choose this option to have the changes saved on a shelf. Shelving is a IntelliJ IDEA internal operation, patches generated from shelved changes are normally applied (unshelved) inside IntelliJ IDEA. Applying shelved changes outside IntelliJ IDEA is also possible but requires additional steps.
informatik01
  • 15,174
  • 9
  • 67
  • 100
Volodymyr
  • 5,789
  • 4
  • 52
  • 78
8

I couldn't find the answer to this question (i.e. the work-flow) in any of Google's documents... so here's my practical experience using Android Studio and Git completely from a UI.

(I vomit at the thought of switching between command line and IDE - it means the IDE is lacking!)

  1. Stash your changes with: Right Click Project -> Git -> Repository -> Stash Changes. Give it a name.
  2. Pull updates that your colleague did with: Right Click Project -> Git -> Repository -> Pull
  3. Merge back your code changes with: Right Click Project -> Git -> Repository -> UnStash Changes -> Apply Stash
  4. You will then see a "Files Merged with Conflicts" UI. This is where you select a file and selectively merge.

WARNING

The manual merge "Merge Revisions" UI is TERRIBLE. Once you try it, you'll see what I mean. Good luck trying to get "Synchronize Scrolling" to actually work. I sincerely hope this UI is addressed within the first few weeks of 2015.

admdrew
  • 3,600
  • 4
  • 22
  • 39
Someone Somewhere
  • 22,369
  • 11
  • 111
  • 155
4

According to documentation:

Merge: select this option to perform merge during the update. This is equivalent to running
git fetch and then git merge, or git pull --no-rebase.

Rebase: select this option to perform rebase during the update. This is equivalent to running
git fetch and then git rebase, or `git pull --rebase (all local commits will be put on top of the updated upstream head).

Branch Default: select this option if you want to apply different update strategies for different branches. You can specify the default update type for each branch in the branch.<name> section of the .git/config configuration file.

Merge vs Rebase

Read more here

yoAlex5
  • 13,571
  • 5
  • 105
  • 98
0

For those who are looking for rebase in Android Studio.

VCS > Git > Rebase.

enter image description here

enter image description here

Then click "Rebase", "Start Rebasing", "Merge". To simplify a process click "All" to resolve non-conflicting changes.

enter image description here

There may be conflicting changes, resolve them until you get a message:

enter image description here

If you encounter these conflicts, click left or right arrow to accept the most suitable:

enter image description here

Click a cross over unneccessary change to ingnore or an arrow to accept:

enter image description here

If you accept both changes, you can also edit the resulting code, for instance, add missing } in methods.

Do this until all conflicts get resolved:

enter image description here

Click "Apply". When all files inside one commit will be up-to-date, click "Continue rebasing" to move to the next commit.

CoolMind
  • 16,738
  • 10
  • 131
  • 165
-1

Update Git project with android studio terminal

  1. git add .
  2. git commit -m "anything"
  3. git push origin master

This is what I was wanting to find the answer from your post. So that's why I writing this answer.

Deepak Ror
  • 213
  • 2
  • 12