Sanjay Salunkhe
Sanjay Salunkhe

Reputation: 2735

git:use git commit or git stash?

I was trying to pull data from repository with git pull command.this command gave me an error

error: Your local changes to the following files would be overwritten by merge:

        public_html/sites/file
        public_html/sites/file1.txt
        public_html/sites/file2.txt
Please, commit your changes or stash them before you can merge.
Aborting

In order to solve this issue i use below two commands.

   Step1
    1)git add .
    2)git commit -m "my first commit"

and then i did

   step2
    git pull origin master

Now everthing works fine, but the problem is that i had mistakenly commited some files in step1(by using command "git add ." and git commit -m "my first commit")".

Now If supposed i issued "git push origin master" command then those unwanted files (commited in step1) will be pushed to the repository.

I dont want these unwanted files(commited in step 1) to be pushed to repository.

Upvotes: 0

Views: 150

Answers (2)

Ahmed Siouani
Ahmed Siouani

Reputation: 13891

You've to reset your commit in order to add only the files you wanted to push,

git reset --soft HEAD^ 

you've then to unstage the files you added accidentally,

git rm --cached list_of_files_you_added_accidentally

and commit again,

git commit -m "your commit message"

Or, unstage all the files and add only what you wanted to push,

git add file_1 file_2 ...

Upvotes: 2

wacko
wacko

Reputation: 3424

If the only commit you've made is 'my first commit', and you want to exclude some files from it, you can do:

git reset --hard           # undo the pull
git reset --mixed HEAD~    # undo the commit, unstash all the files
git add file1, dir2 ...    # add just the files you want

git commit -m "some msg"   # then commit and push as normal...
git pull origin master
git push

Upvotes: 2

Related Questions