git - Your branch is ahead of 'origin/master' by 1 commit

360,934

Solution 1

You cannot push anything that hasn't been committed yet. The order of operations is:

  1. Make your change.
  2. git add - this stages your changes for committing
  3. git commit - this commits your staged changes locally
  4. git push - this pushes your committed changes to a remote

If you push without committing, nothing gets pushed. If you commit without adding, nothing gets committed. If you add without committing, nothing at all happens, git merely remembers that the changes you added should be considered for the following commit.

The message you're seeing (your branch is ahead by 1 commit) means that your local repository has one commit that hasn't been pushed yet.

In other words: add and commit are local operations, push, pull and fetch are operations that interact with a remote.

Since there seems to be an official source control workflow in place where you work, you should ask internally how this should be handled.

Solution 2

git reset HEAD^ --soft (Save your changes, back to last commit)

git reset HEAD^ --hard (Discard changes, back to last commit)

Solution 3

If you just want to throw away the changes and revert to the last commit (the one you wanted to share):

git reset --hard HEAD~

You may want to check to make absolutely sure you want this (git log), because you'll loose all changes.

A safer alternative is to run

git reset --soft HEAD~ # reset to the last commit
git stash              # stash all the changes in the working tree 
git push               # push changes 
git stash pop          # get your changes back 

Solution 4

I resolved this by just running a simple:

git pull

Nothing more. Now it's showing:

# On branch master
nothing to commit, working directory clean

Solution 5

git reset HEAD^

then the modified files should show up.

You could move the modified files into a new branch

use,

git checkout -b newbranch

git checkout commit -m "files modified"

git push origin newbranch

git checkout master

then you should be on a clean branch, and your changes should be stored in newbranch. You could later just merge this change into the master branch

Share:
360,934
sam
Author by

sam

Updated on April 23, 2022

Comments

  • sam
    sam about 2 years

    I am newbie in git and I am working on git.

    I added some files in git :

    git add <file1>
    git add <file2>
    

    then I wanted to push that for review, but mistakenly I did

    git commit
    

    so the files which I have changed don't go for reviews.
    Now if I enter the command :

    git status
    

    it says

    # On branch master
    # Your branch is ahead of 'origin/master' by 1 commit.
    #
    nothing to commit (working directory clean)
    

    I want to revert that commit and I want to push those files for review rather than commit. Can anyone let me know how I can do that?

    • ДМИТРИЙ МАЛИКОВ
      ДМИТРИЙ МАЛИКОВ about 12 years
      If you want to undo your commit - take a look at the similar question.
    • David Soroko
      David Soroko about 12 years
      I don't know what you're using for code reviews. The simple answer to the simple question of how to un-commit is git reset HEAD^
    • sam
      sam about 12 years
      i did reset HEAD. but then when I try to push, then it says me D file1.py when I tried to psuh then again it says me that your branch is ahead of origin/master by 1 commit
    • Jake Greene
      Jake Greene about 12 years
      What do you mean when you say "I want to push those files for review"? Do you want other people to see your commit?
    • sam
      sam about 12 years
      my code will be first reviewed and then it will be commited. so first I want to push the code for review but mistakenly i have commited
    • Shep
      Shep about 12 years
      this is confusing terminology: you have to commit your code before you push it. What do you mean it will be committed after it's reviewed? Presumably someone else will merge it into an official repo?
    • antinome
      antinome over 11 years
      (I'm sure there are many valid workflows, so what I'm about to say may not be relevant to the way your team operates...) The workflow I'm familiar with is to commit your changes in a "feature" or "dev" branch and push that to the central repository. The "feature" branch can be merged into master after the code review.
  • sam
    sam about 12 years
    aha ok. so it means after commit, I have to push it
  • dbr
    dbr about 12 years
    Good explanation - the question'er probably doesn't want to actually revert the commit, but rather, have people review the commit before pushing it to the origin repository. I'd also recommend reading a few of the tutorial/intros to git on git-scm.com/documentation
  • Anil Arya
    Anil Arya about 10 years
    It can cause problem if there will more than one created branch which is not required.
  • Gucho Ca
    Gucho Ca about 8 years
    you went back to you remote state and lost your last work :D
  • Tom
    Tom over 7 years
    When my git status says "Your branch is ahead of 'origin/develop' by 1 commit." how exactly do I view that files were changed? git diff doesn't seem to do anything
  • Adrian P.
    Adrian P. almost 6 years
    Sometimes is somebody else that worked there. Make a pull before your push
  • Mugen
    Mugen almost 5 years
    What is the significance of the HEAD with a carrot '^' ? I've seen it being written without the carrot too.
  • Kevin K
    Kevin K almost 3 years
    I think the carrot reverts by one commit (so if you have to back up 3 commits, then you could run the command 3 times). Similarly, you could run git reset HEAD~ --hard to revert all commits to the HEAD
  • Eskay Amadeus
    Eskay Amadeus over 2 years
    It's caret, not carrot.
  • Yellowjacket
    Yellowjacket over 2 years
    See detailed discussion in stackoverflow.com/questions/2221658/…