@@ -24,6 +24,7 @@ static int progress = -1;
2424static const char * * refspec ;
2525static int refspec_nr ;
2626static int refspec_alloc ;
27+ static int default_matching_used ;
2728
2829static void add_refspec (const char * ref )
2930{
@@ -111,6 +112,9 @@ static void setup_default_push_refspecs(struct remote *remote)
111112{
112113 switch (push_default ) {
113114 default :
115+ case PUSH_DEFAULT_UNSPECIFIED :
116+ default_matching_used = 1 ;
117+ /* fallthru */
114118 case PUSH_DEFAULT_MATCHING :
115119 add_refspec (":" );
116120 break ;
@@ -130,6 +134,45 @@ static void setup_default_push_refspecs(struct remote *remote)
130134 }
131135}
132136
137+ static const char message_advice_pull_before_push [] =
138+ N_ ("Updates were rejected because the tip of your current branch is behind\n"
139+ "its remote counterpart. Merge the remote changes (e.g. 'git pull')\n"
140+ "before pushing again.\n"
141+ "See the 'Note about fast-forwards' in 'git push --help' for details." );
142+
143+ static const char message_advice_use_upstream [] =
144+ N_ ("Updates were rejected because a pushed branch tip is behind its remote\n"
145+ "counterpart. If you did not intend to push that branch, you may want to\n"
146+ "specify branches to push or set the 'push.default' configuration\n"
147+ "variable to 'current' or 'upstream' to push only the current branch." );
148+
149+ static const char message_advice_checkout_pull_push [] =
150+ N_ ("Updates were rejected because a pushed branch tip is behind its remote\n"
151+ "counterpart. Check out this branch and merge the remote changes\n"
152+ "(e.g. 'git pull') before pushing again.\n"
153+ "See the 'Note about fast-forwards' in 'git push --help' for details." );
154+
155+ static void advise_pull_before_push (void )
156+ {
157+ if (!advice_push_non_ff_current || !advice_push_nonfastforward )
158+ return ;
159+ advise (_ (message_advice_pull_before_push ));
160+ }
161+
162+ static void advise_use_upstream (void )
163+ {
164+ if (!advice_push_non_ff_default || !advice_push_nonfastforward )
165+ return ;
166+ advise (_ (message_advice_use_upstream ));
167+ }
168+
169+ static void advise_checkout_pull_push (void )
170+ {
171+ if (!advice_push_non_ff_matching || !advice_push_nonfastforward )
172+ return ;
173+ advise (_ (message_advice_checkout_pull_push ));
174+ }
175+
133176static int push_with_options (struct transport * transport , int flags )
134177{
135178 int err ;
@@ -151,14 +194,21 @@ static int push_with_options(struct transport *transport, int flags)
151194 error (_ ("failed to push some refs to '%s'" ), transport -> url );
152195
153196 err |= transport_disconnect (transport );
154-
155197 if (!err )
156198 return 0 ;
157199
158- if (nonfastforward && advice_push_nonfastforward ) {
159- fprintf (stderr , _ ("To prevent you from losing history, non-fast-forward updates were rejected\n"
160- "Merge the remote changes (e.g. 'git pull') before pushing again. See the\n"
161- "'Note about fast-forwards' section of 'git push --help' for details.\n" ));
200+ switch (nonfastforward ) {
201+ default :
202+ break ;
203+ case NON_FF_HEAD :
204+ advise_pull_before_push ();
205+ break ;
206+ case NON_FF_OTHER :
207+ if (default_matching_used )
208+ advise_use_upstream ();
209+ else
210+ advise_checkout_pull_push ();
211+ break ;
162212 }
163213
164214 return 1 ;
0 commit comments