title: What does git push -u origin BRANCH do when you are not on BRANCH?
body: <p>When I first started using git, I was told by another team member to always push using</p>
  <pre><code>git push -u origin MYBRANCH
  </code></pre>
  <p>because it would prevent me accidentally pushing code to the wrong branch.  I didn't fully understand and today tried to understand what happens that means.</p>
  <p>I created a local repo and provide my exact commands so you could copy/paste this to reproduce what I've done.</p>
  <p>In this test I have a local (on disk) repo and two branches:</p>
  <pre><code>git branch
    main
  * feature/fixthis
  </code></pre>
  <p>I'm on the <code>feature/fixthis</code> branch and run:</p>
  <pre><code>git push -u origin main
    # NOTE: the current branch is feature/fixthis
  </code></pre>
  <p><strong>Did I just accidentally push my feature branch onto main?</strong></p>
  <h1>My Solution (figure it out myself using local test)</h1>
  <p>To test this out myself, I did the following.  The commands are written in such a way that you can copy/past to a UNIX bash shell to reproduce a local git repo and create the exact scenario I'm asking about.</p>
  <pre><code># Step 1:  Create local repo
  mkdir so-test; cd so-test; git init --bare local-git-repo.git
  
  # Step 2: git clone local (empty repo).
  git clone ./local-git-repo.git test-this
  
  # Step 3: Add a Readme.md file, commit and push the change 
  cd test-this; echo hi &gt; README.md; git add README.md; git commit -m &quot;initial commit&quot;; git push -u origin main
  
  # Step 4: Check the branch feature/fixit.
  git co -b feature/fixit
  
  # Step 5: Make change and accidentally push it to main.  
  echo In feature/fixit &gt;&gt; README.md; git add -u .; git commit -m &quot;change on feature/fixit&quot;; 
  
  # Step 6: Do the push from the feature/fixit branch to main
  git push -u origin main
  </code></pre>
  <p><strong>Did the command <code>git push -u origin main</code> accidentally update the main branch?</strong></p>
  <p>When I look at the logs I can't figure out what happened to the last push, it doesn't appear to have updated the origin/main branch because I don't see the origin/feature/fixit tag (on any commit).</p>
  <pre><code>git log
  commit 1bba12ad200080cb0a59aa0e2fb518a1e284b135 (HEAD -&gt; feature/fixit)
  Author: me@example.com
  Date:   Thu Apr 7 14:25:19 2022 +0000
  
      change on feature/fixit
  
  commit dcf8098bda7c937c80bc60ee974ac6eb9a895ced (origin/main, main)
  Author: me@example.com
  Date:   Thu Apr 7 14:20:06 2022 +0000
  
      initial commit
  
  </code></pre>
  <h3>Searching for answers</h3>
  <ul>
  <li><p>Search <a href="https://stackoverflow.com/search?q=%5Bgit%5D+git+push+-u+origin">[git] git push -u origin</a> - around  20,000 results... Hum... that's not going to work, let me add another question to the category. :-)</p>
  </li>
  <li><p><a href="https://stackoverflow.com/questions/5697750/what-exactly-does-the-u-do-git-push-u-origin-master-vs-git-push-origin-ma">What exactly does the &quot;u&quot; do? &quot;git push -u origin master&quot; vs &quot;git push origin master&quot;</a> - Good answer with lots of good technical stuff.  It might answer my question but if it does I didn't see it.</p>
  </li>
  </ul>
  <p>Many others</p>