PTK
PTK

Reputation: 53

How to commit only the files that are different to existing github remote repository?

Not sure how to make this question more precise so i'll explain.

I've got my remote repository on github with few commits. I did download last commit and made some changes in few files in my local project but i deleted git repository in my project.

When i want to hook up my local project to existing github repo using :

git init
git remote add origin [email protected]:User/UserRepo.git 

my local files won't compare with last commit but it wants to push every file i've got in project (files i didn't changed that are already on my last remote commit), not just the few files I changed.

How to compare my local changes with last remote commit and only commit changes that are different or new from last commit?

Upvotes: 0

Views: 438

Answers (2)

IMSoP
IMSoP

Reputation: 97928

The simple answer is that git will always compare your working copy against the commit you currently have checked out, which will normally be the tip of some branch. So to compare against some other state, you need to check out that state, and apply your changes on top of it.

The situation you describe is slightly unusual, because you've run git init (creating a completely new repository) where git clone would be more natural (starting the repository based on the remote one).

The good thing is, git doesn't really care about that - although often used with a central server, it's designed to be entirely distributed, so linking unrelated repositories together is no problem.

You need to do a few things:

  1. Fetch the contents of the remote repository with git fetch origin (git clone would have done this by default, but git remote add does not).
  2. Move your local files out of the way - the easiest way is to probably just move all of them to a completely different folder on your local machine, since git currently thinks they're all brand new.
  3. Check out the branch you want to continue working on; this is probably called master, or main, or develop, but those are just conventions.
  • If you run git checkout master and don't already have a branch called master, git will treat it as short for git checkout -t origin/master, meaning "create a local branch master associated for push and pull with the branch of the same name on the origin remote".
  • If you already have a local branch with the name of the branch you want (e.g. it was created for you by git init), the easiest approach will be to delete it - check out any other branch, then run git branch -D branch_name.
  1. Now move your files back into the working directory, and they will be compared against the branch you checked out.

Upvotes: 1

LeGEC
LeGEC

Reputation: 52026

Here is a another way to do it :

if you want to commit your files on top of a branch named mybranch :

  1. create a local branch, with no history, named mybranch :

    git branch mybranch
    git checkout mybranch
    
  2. run git fetch, to update your view of origin/mybranch

  3. run git reset origin/mybranch
    important : do not use the --hard option, this would throw away your local modifications

You are now back to a state where :

  • the active commit is the head commit of mybranch
  • your local changes are compared to this commit

You may want to add git branch -u origin/mybranch to have your local branch track the remote branch.

Upvotes: 2

Related Questions