Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+8 votes
2 views
in DevOps and Agile by (19.7k points)

I am a 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?

2 Answers

+13 votes
by (62.9k points)

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

  1. Make your change.
  2. git add - this stages the changes in the file for committing
  3. git commit - this will commit your staged file locally and now being tracked by git.
  4. git push - this will push your committed changes in a file to a remote repo

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 are seeing (your branch is ahead by one commit) means your native 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 a place where you work, you should ask internally how this should be handled.

0 votes
by (2.8k points)

Step 1- Firstly, your have to find the commit which you want to revert, to undo everything, that you can do by:

Git log

Step 2- You have to undo the commit but keep the changes in your working directory.

git reset <commit_hash> --soft    # Use --soft to keep changes staged or --mixed to keep them unstaged

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...