Reputation: 53
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
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:
git fetch origin
(git clone
would have done this by default, but git remote add
does not).master
, or main
, or develop
, but those are just conventions.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".git init
), the easiest approach will be to delete it - check out any other branch, then run git branch -D branch_name
.Upvotes: 1
Reputation: 52026
Here is a another way to do it :
if you want to commit your files on top of a branch named mybranch
:
create a local branch, with no history, named mybranch
:
git branch mybranch
git checkout mybranch
run git fetch
, to update your view of origin/mybranch
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 :
mybranch
You may want to add git branch -u origin/mybranch
to have your local branch track the remote branch.
Upvotes: 2