<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jey Singh</title>
    <description>The latest articles on DEV Community by Jey Singh (@jeysingh).</description>
    <link>https://dev.to/jeysingh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4248516%2F88b16090-e36b-43fa-a149-64f75324f90c.png</url>
      <title>DEV Community: Jey Singh</title>
      <link>https://dev.to/jeysingh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jeysingh"/>
    <language>en</language>
    <item>
      <title>Fix "git push rejected (fetch first)" without losing work</title>
      <dc:creator>Jey Singh</dc:creator>
      <pubDate>Mon, 16 Nov 2026 21:25:06 +0000</pubDate>
      <link>https://dev.to/jeysingh/fix-git-push-rejected-fetch-first-without-losing-work-49ln</link>
      <guid>https://dev.to/jeysingh/fix-git-push-rejected-fetch-first-without-losing-work-49ln</guid>
      <description>&lt;p&gt;&lt;em&gt;Git refused your push because GitHub has commits you don't have yet. Nothing is broken and nothing is lost. Get their work first, then push again.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The error
&lt;/h2&gt;

&lt;p&gt;You run &lt;code&gt;git push&lt;/code&gt; and see something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt; ! &lt;span class="o"&gt;[&lt;/span&gt;rejected&lt;span class="o"&gt;]&lt;/span&gt;        main -&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; main &lt;span class="o"&gt;(&lt;/span&gt;fetch first&lt;span class="o"&gt;)&lt;/span&gt;
error: failed to push some refs to &lt;span class="s1"&gt;'github.com:you/shop-app.git'&lt;/span&gt;
hint: Updates were rejected because the remote contains work that you &lt;span class="k"&gt;do&lt;/span&gt; not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you already fetched their commits but have not merged them, Git says &lt;code&gt;(non-fast-forward)&lt;/code&gt; and “the tip of your current branch is behind its remote counterpart” instead. The cause and the fix are the same.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it means
&lt;/h2&gt;

&lt;p&gt;Someone pushed to the same branch after you last pulled. It might be a teammate, or you on another computer, or an edit you made in the GitHub website. GitHub will not let your push replace commits you have never seen, because that would delete their work. This is Git protecting the team, not a bug.&lt;/p&gt;

&lt;h2&gt;
  
  
  Check first (these commands change nothing)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git status                           &lt;span class="c"&gt;# which branch am I on? anything half done?&lt;/span&gt;
git fetch                            &lt;span class="c"&gt;# download new commits, leave my files alone&lt;/span&gt;
git log --oneline main..origin/main  &lt;span class="c"&gt;# commits on GitHub that I don't have&lt;/span&gt;
git log --oneline origin/main..main  &lt;span class="c"&gt;# my commits that GitHub doesn't have&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;main..origin/main&lt;/code&gt; reads as “commits in &lt;code&gt;origin/main&lt;/code&gt; that &lt;code&gt;main&lt;/code&gt; does not have yet”. Now you know exactly what you are about to combine.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix
&lt;/h2&gt;

&lt;p&gt;Bring their commits into your branch, then push:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git pull  &lt;span class="c"&gt;# fetch, then merge their work into yours&lt;/span&gt;
git push  &lt;span class="c"&gt;# now it goes through&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you prefer a straight line of history with no merge commit, use &lt;code&gt;git pull --rebase&lt;/code&gt; instead. It puts your commits on top of theirs. Many teams set this as the default with &lt;code&gt;git config --global pull.rebase true&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  If the pull stops with a conflict
&lt;/h3&gt;

&lt;p&gt;You and the other person changed the same lines. Git marks them in the file with &lt;code&gt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&lt;/code&gt;, &lt;code&gt;=======&lt;/code&gt; and &lt;code&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Run &lt;code&gt;git status&lt;/code&gt;. It lists the files with conflicts.&lt;/li&gt;
&lt;li&gt;Open each file, keep the right lines, and delete the three marker lines.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;git add&lt;/code&gt; on each fixed file.&lt;/li&gt;
&lt;li&gt;Finish with &lt;code&gt;git commit&lt;/code&gt; (after a merge) or &lt;code&gt;git rebase --continue&lt;/code&gt; (after a rebase).&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;git push&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Want to back out instead? &lt;code&gt;git merge --abort&lt;/code&gt; or &lt;code&gt;git rebase --abort&lt;/code&gt; puts everything back the way it was before the pull.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do not force push.&lt;/strong&gt; &lt;code&gt;git push --force&lt;/code&gt; makes the error go away by deleting the other person's commits from GitHub. On a shared branch that loses a teammate's work. Many teams block it with a branch rule.&lt;/p&gt;

&lt;h2&gt;
  
  
  Make it happen less often
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Pull before you start work each day, and before you push.&lt;/li&gt;
&lt;li&gt;Work on your own branch, not on &lt;code&gt;main&lt;/code&gt;. Then a push only touches your branch, and your work joins &lt;code&gt;main&lt;/code&gt; through a pull request.&lt;/li&gt;
&lt;li&gt;Push small and often. The fewer commits you hold back, the smaller any conflict is.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Other push errors that look similar
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;You see&lt;/th&gt;
&lt;th&gt;It means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Permission denied (publickey)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;GitHub doesn't have your SSH key. Check with &lt;code&gt;ssh -T git@github.com&lt;/code&gt;, or use the HTTPS address.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;Permission to OWNER/REPO.git denied to USER&lt;/code&gt; or &lt;code&gt;403&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;You are signed in as the wrong account, or that account can't write to the repo. Check with &lt;code&gt;gh auth status&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;remote: Repository not found.&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A typo in the URL, or a private repo your account can't see. Check with &lt;code&gt;git remote -v&lt;/code&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GH013: Repository rule violations&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A rule blocks direct pushes to that branch, or a secret was found. Push a branch and open a pull request.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;New to the undo side of Git? Read &lt;a href="https://www.jeysingh.com/learn/git-reset-vs-revert-vs-restore" rel="noopener noreferrer"&gt;git reset vs revert vs restore&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;This guide first appeared on &lt;a href="https://www.jeysingh.com/learn/git-push-rejected-fetch-first" rel="noopener noreferrer"&gt;jeysingh.com&lt;/a&gt;. It comes from my Git / GitHub Study Guide. There's a free sample at &lt;a href="https://www.jeysingh.com/git" rel="noopener noreferrer"&gt;jeysingh.com/git&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
