update my repository on github

FeRcHo picture FeRcHo · Jan 19, 2018 · Viewed 33.4k times · Source

I just went up to github the project that was working locally on my computer. everything went well and I could upload all my files .. now I would like to know how I can update my repository if I make some changes to my project locally try the following commands:

git add.
git pull origin master

I think I'm forgetting something, or I'm using the commands incorrectly, what would be the correct way?

Answer

user1561783 picture user1561783 · Jan 20, 2018

Your work flow for ensuring that your changes are added correctly to your remote github repo in "normal" cases should follow these stages ..

(1) git status

Will always tell you what is uncommitted and what needs to be "added" (staged) for committing to your local repo. In fact git will hint to you what it thinks is the next step in your work flow

(2) git add <your_file_path_you_can_use_glob_patternsLike_asteriks>

(3) git commit -m "A Nice Subject line for many file commits <enterFromKeyboardGivesYouAnotherLine>
(a) Continue typing more comments which are detailed if necessary <anotherEnterFromKeyboard>
(b) Some more details and do not forget to put closing quote mark"

Works in Windows if use the Windows git-bash.exe It uses mingW64 emulator to simulate linux environment. It's very good at it.

You need to commit whatever changes yout want to keep - locally before you can "push" your changes to your github repo remotely ie only after you have told git where your remote git repo is .....

(4) git remote add myGitHubOrBitBucketRepo https://github.com/YourGitAppRepo.git

Usually the default name for your remote repo on github is given as "origin". But I have been using specific alias branch names which in your case is "myGitHubOrBitBucketRepo"

(5) git push -u myGitHubOrBitBucketRepo HEAD:master

This command will push your committed changes (aka snap shots in git speak) to YourGitAppRepo.git on github.com onto the master branch and if master on your remote repo is not ahead of your local branch and it is just a couple of commits behind - github.com will accept this push

The -u is the same as --track which means that your local branch positioned @ HEAD will be tracking the master branch at your remote alias myGitHubOrBitBucketRep

In steps 4 & 5 you will have to use a userId and passWord to interact with your remote repo on GitHub.com

(6) git status

From now on git status will actually tell you whether you are behind or ahead of your remote github repo because of the --track (ing) option you had done in your push

Another useful command to use from this point onwards will be

git branch -vv --all

An example

$ git branch -vv --all
* CurrAsOf18Jan2018             50d1fc6 [remotes/bitbucketFrmWin/master: behind 5] Pedantic but done - gitNotes.txt
  remotes/bitbucketFrmWin/master        58470cd [CurrAsOf18Jan2018] This is really crazy - Spent more than a week - Singleton still gives

Over here bitbucketFrmWin is my alias for my remote bitbucket repo AsOf16Jan2018 is a branch that I am no longer interested master is my current main branch which I push my changes to from my local repo.

The --all option will also display your local & your "remotes"

Of note is the following * CurrAsOf18Jan2018 50d1fc6 [remotes/bitbucketFrmWin/master: behind 5]

The asterik * that is the HEAD of my local branch or at which commit on that branch I am on. Usually it is always at the tip or the HEAD i.e. why it is called the "head"

CurrAsOf18Jan2018 is my local main branch and importantly it is saying that my local is already ahead of my remote branch by 5 commits - it is out of date so I need to update my remote with a "git push"

For now that is just one side of this story. If your remote repo goes ahead then another work-flow would be

git fetch --all && git merge --ff-only <theCommitYouWantYouToCatchUpWith>

That is altogether another post.

And here is a succinct image which I found courtesy Oliver Steele that displays another version of the basic git workflow life-cycle of versioning Another Git WorkFlow cycle courtesy Oliver Steele @ osteele.com

Hope this helps.