Skip to content

Instantly share code, notes, and snippets.

@LukeberryPi
Last active July 14, 2025 15:34
Show Gist options
  • Save LukeberryPi/ec6018e975f8b5fac2dbce596af280d5 to your computer and use it in GitHub Desktop.
Save LukeberryPi/ec6018e975f8b5fac2dbce596af280d5 to your computer and use it in GitHub Desktop.
why my git configuration was wrong

Notification: Preset: opus reason low creativity


LukeberryPi: this is my git workfow. but I always see this error. what am I doing wrong?

git checkout main
git checkout -b feature/...
git add -p
git commit -m "..."
git push origin HEAD (which errors like the image)

at which point I run git pull (which rebases) i complete the rebase, solve the conflicts

and run git push --force-with-lease origin HEAD


Raycast AI: The issue is that the feature branch already exists on the remote repository. This happens because either:

  1. You previously pushed this branch and are reusing the branch name
  2. Someone else created a branch with the same name
  3. Your local main branch is outdated when you create the feature branch

To fix your workflow, add these steps:

git checkout main
git pull origin main  # ← Add this to update main first
git checkout -b feature/...
git add -p
git commit -m "..."
git push origin HEAD

If you still get the error, it means the branch already exists remotely. You have two options:

Option 1: Use a different branch name

git checkout -b feature/different-name

Option 2: Check if the remote branch exists first

git fetch origin
git branch -r | grep feature/your-branch-name

If it exists and you want to continue working on it:

git checkout main
git pull origin main
git checkout -b feature/... origin/feature/...  # Track the remote branch
git add -p
git commit -m "..."
git push origin HEAD

The key is ensuring your main branch is up-to-date and checking whether the feature branch already exists remotely before creating it locally.


LukeberryPi: i'm the only one working on this branch so it can't be that. also how can it have remote changes if i'm the only one working on it? i think it's some problem with not assigning an upstream branch correctly


Raycast AI: You're absolutely right! The issue is with upstream branch tracking. When you first push a new branch with git push origin HEAD, it doesn't automatically set up tracking between your local and remote branches.

Here's what's happening:

  1. You create a new branch locally
  2. You push it to remote (creates the branch there)
  3. Your local branch isn't tracking the remote branch
  4. Next time you push, Git doesn't know they're related and complains about divergent histories

To fix this, use one of these approaches:

Option 1: Set upstream on first push

git checkout main
git checkout -b feature/...
git add -p
git commit -m "..."
git push -u origin HEAD  # ← The -u flag sets upstream tracking

Option 2: Set upstream explicitly

git branch --set-upstream-to=origin/feature/... feature/...

Option 3: Always push with tracking

git push --set-upstream origin HEAD

After setting the upstream once, future pushes will work with just git push (no need for origin HEAD).

To check if your current branch has upstream tracking:

git branch -vv

If you see something like [origin/feature/...] commit message, it's tracking correctly. If not, you'll just see the commit info without the [origin/...] part.

This explains why you need to force push after pulling - without proper tracking, Git treats the remote as a separate branch with different history.


LukeberryPi: what is the difference between

git push -u origin HEAD

and

git push -u origin


Raycast AI: The difference is subtle but important:

git push -u origin HEAD

  • Explicitly pushes the current branch (whatever HEAD points to) to a branch with the same name on origin
  • Always predictable - HEAD always means "current branch"
  • Works regardless of your Git configuration

git push -u origin

  • Uses Git's push.default configuration to decide what to push
  • With modern Git (2.0+), the default is simple, which pushes the current branch to a branch with the same name
  • Can behave differently based on your Git config

In practice, they usually do the same thing, but HEAD is more explicit and safer because:

# These are equivalent with default settings
git push -u origin HEAD        # Explicit: "push current branch"
git push -u origin            # Implicit: "push based on config"

# But if someone changed push.default to 'matching'
git config push.default matching
git push -u origin            # Might push ALL branches!
git push -u origin HEAD       # Still only pushes current branch

Recommendation: Use git push -u origin HEAD for clarity and consistency. It's self-documenting - anyone reading your command knows exactly what branch is being pushed.

You can check your current push.default setting with:

git config push.default
@LukeberryPi
Copy link
Author

the image mentioned above contains this error:

error: failed to push some refs to https://github.com/<CENSORED>
hint: Updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote changes (e.g. git pull) before pushing again.
hint: See the Note about fast-forwards, in git push -help, for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment