Himanshu
Himanshu

Reputation: 33

git push not submitting local changes to remote repository

I have followed following steps.

$git log --oneline

commit5
commit4
commit3
commit2
commit1

$git checkout commit3

$git log --oneline
commit3
commit2
commit1

Now i made changes to few file.

$git add .
$git commit -m "commit6"
$git log --oneline
commit6
commit3
commit2
commit1

Now, i try to push these changes to remote repository.

$git push -f origin master


This command is not pushing my commit6 to the remote server.
I am getting the message everything up-to-date.

How to push commit6 to remote repository. I want remote repository state similar to my local state.

Thanks in advance.

Upvotes: 0

Views: 145

Answers (2)

torek
torek

Reputation: 490078

$ git checkout commit3

I will assume here that you used a raw hash ID to check out, as in, e.g.:

$ git checkout fe3fec53a6
Note: switching to 'fe3fec53a6'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at fe3fec53a6 Merge branch 'bc/rev-list-without-commit-line'

Please read all of the text that git checkout printed.

Now i made changes to few file.

$ git add .
$ git commit -m "commit6"
$ git log --oneline
commit6
commit3
commit2
commit1

You have now created, in effect, a new unnamed branch. This new branch ends at your new commit6.

Your existing master branch remains unchanged. It continues to end at the existing commit5 that you saw in the original output.

How to push commit6 to remote repository. I want remote repository state similar to my local state.

Depending on the remote repository in question, this may be anywhere from very difficult to impossible. In particular, your local state uses a detached HEAD. You may not be able to put the other repository into detached-HEAD state.

You can send the new commit to the remote repository, but you will likely wish to use a branch name to do this. When you are in detached HEAD mode, you are not on any branch. You made your commit6 on no branch at all.

You could choose to discard commits 4 and 5 and instead use only commit 6, locally, as the tip commit of your (local) master branch. You can attempt to set the remote repository's master, over at origin, to this same state as well. The remote repository will be within its rights to refuse to discard commits 4 and 5, especially if you do not own the remote repository. If you do own the remote repository, you can set things up so that you can discard the commits there—but you have not told us whether this is the case.

You could choose to add a new commit to master locally, so that commit7 (the new one) comes immediately after commit5, and send this new commit and its files to the remote repository. This is more likely to succeed than the request to have the remote repository discard commits 4 and 5, since Git is built to add new commits without discarding existing ones.

You need to decide which of these actions is the most appropriate (or whether there's some alternative that's better than any of these). Should you wish to force the remote repository to discard commits, you will, in general, need some kind of "forced push". There are literally hundreds of existing StackOverflow questions about this.

Upvotes: 1

Huy Phạm
Huy Phạm

Reputation: 1023

If you made changes to a few fies, the first thing you want to do is not

$git commit -m "commit6"

But to add files to stage with git add file_name Then you can create a commit and push it to remote repo Make sure you have linked your offline repo with your remote first with git remote

Upvotes: 0

Related Questions