158

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?

Shep
  • 7,990
  • 8
  • 49
  • 71
sam
  • 18,509
  • 24
  • 83
  • 116
  • If you want to undo your commit - take a look at the [similar question](http://stackoverflow.com/questions/927358/git-undo-last-commit). – ДМИТРИЙ МАЛИКОВ Apr 16 '12 at 06:03
  • 2
    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^` – Anonymoose Apr 16 '12 at 06:03
  • 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 – sam Apr 16 '12 at 06:05
  • What do you mean when you say "I want to push those files for review"? Do you want other people to see your commit? – Jake Greene Apr 16 '12 at 06:05
  • 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 – sam Apr 16 '12 at 06:06
  • 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? – Shep Apr 16 '12 at 06:14
  • (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. – antinome Dec 17 '12 at 18:05

7 Answers7

163

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.

tdammers
  • 20,353
  • 1
  • 39
  • 56
  • 6
    aha ok. so it means after commit, I have to push it – sam Apr 16 '12 at 06:08
  • 5
    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 http://git-scm.com/documentation – dbr Apr 16 '12 at 06:09
  • 2
    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 – Tom Sep 08 '16 at 23:01
  • Sometimes is somebody else that worked there. Make a pull before your push – Adrian P. Jul 10 '18 at 19:06
137

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

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

Juanito Fatas
  • 9,419
  • 9
  • 46
  • 70
  • 8
    What is the significance of the HEAD with a carrot '^' ? I've seen it being written without the carrot too. – Mugen May 22 '19 at 04:23
  • 1
    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 – Kevin K Aug 05 '21 at 16:32
  • 8
    It's caret, not carrot. – Eskay Amadeus Dec 22 '21 at 12:28
  • See detailed discussion in https://stackoverflow.com/questions/2221658/whats-the-difference-between-head-and-head-in-git – Yellowjacket Jan 24 '22 at 18:51
45

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 
Shep
  • 7,990
  • 8
  • 49
  • 71
25

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
karlingen
  • 13,800
  • 5
  • 43
  • 74
18

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

Karan Singh
  • 1,114
  • 1
  • 13
  • 30
OZI
  • 424
  • 3
  • 8
4
git reset HEAD <file1> <file2> ...

remove the specified files from the next commit

Bnjmn
  • 1,973
  • 1
  • 20
  • 34
0

connect to

remote origin/master

not

local master

and make a pull request.

Create a new branch base on remote master.

Ehsan Ehsani
  • 412
  • 3
  • 7