Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push changes to github without pull

Tags:

git

github

I developed a project that was been developed by another developers. I sent my code to the previous developers to make changes in their code but they said that they have never worked with git, so they added changes to the project and it is now the actual version but not under version control.

So, I already have commits in github and I need to push this new version.

What I did:

  1. git init
  2. git remote add origin
  3. git add .
  4. git push origin master

But there is an problem: I can't push new changes before updating the local repo. If I update my repo it will return all the files that the previous developers have deleted.

How do I push the current version without pulling data from git?

like image 700
Matrosov Oleksandr Avatar asked Oct 10 '13 18:10

Matrosov Oleksandr


People also ask

Can I push to GitHub without commit?

No, you must make a commit before you can push. What is being pushed is the commit (or commits).

Can you push before pulling git?

Always Pull Before a Push Doing so will ensure that your local copy is in sync with the remote repository. Remember, other people have been pushing to the remote copy, and if you push before syncing up, you could end up with multiple heads or merge conflicts when you push.

How do I push to a repo I don't own?

Updating a file on a repo you don't own. Once you click Propose file change, a pull request will be created and the owner of the repository will have a chance to review your change and either accept it, reject it, or discuss it with you.


1 Answers

You can do a forced push.

git push -f origin branch_name 

The forced push will erase all commit history of the remote repository's branch, and replace it to your branch.

Check the answers for doing forced pushes in "How do I properly force a Git push?". Forced push can have unintended consequences as mentioned in "Git: How to ignore fast forward and revert origin [branch] to earlier commit?", so check for the same.

The forced push will be an incorrect way to push stuff in your case, since you already have previous commits on GitHub, and this will erase the commit history for previous commits.

Hence, to preserve your commit history, you can do the following things:

Remove all the files from the git repository, and then add the new files here, and then commit the updated files:

git rm -rf . cp -r path/to/updated/code/* . git add . 

Doing a git status now will tell you which files the other developers modified, and a git diff will show what modifications are there.

If a file has remain unchanged, then git rm and git add will nullify the effect of each other.

The files which those developers deleted remain deleted since you ran the git rm for them but no git add.

Once you are satisfied that these indeed are the changes, you can commit using

git commit -m "Merged new code" 

Potential gotchas:

  1. Only the file mode has changed (755 <=> 644) - depending on what code the other developers sent you.
  2. You will lose your .gitignore file with git rm -rf . (and similarly .gitattributes and other such files). Reset HEAD for each such files using git reset HEAD .gitignore before the commit.
  3. Different line terminating characters (in case different development environments are being used), So check for them appropriately.
like image 91
Anshul Goyal Avatar answered Sep 29 '22 02:09

Anshul Goyal