Git Summary and Workflow

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

There were some previous tutorials on how to do specific things using GIT.  In this tutorials, we give a summary of a typical workflow on how two person can collaborate on a project.  And it will give us a chance to review some GIT commands.

We are going to role play two person. One being the CEO (Chief Executive Officer) of a company and the owner of the website shown here…

sample website

sample website

In previous tutorials, we saw how the site was created on a GitHub account, and how the CEO was able to edit and deploy to the site.

Now the CEO wants the CIO (Chief Information Officer) to help with the edits of the site.  So CEO give CIO access as contributor to the GitHub account here…

github account

github account

1.  git clone

The first thing the CIO does when he gets access is to clone the entire website on his local machine so that he can work on it.  CIO creates an empty directory called website where the local repository will be cloned to…

git bash command line 1

git bash command line 1

He is on Windows and uses Git Bash command line.

Then he types git clone with the “HTTPS clone URL” shown in the GitHub account page (shown above) …

git clone command

git clone command

This created a new folder named “learnwebtutorials.github.io” in the website folder.  And he cd’s into that folder and finds all the files of the website in there.  Doing git status shows working directory clean.

2.  git branch 

CIO wants to add an “contact us feature” to the website.  He creates a new branch called “contact_feature” and switches to that branch in which he will develop this feature …

git branch contact_feature
git checkout contact_feature

As a shortcut, he could have done …

git checkout -b contact_feature

3.  git add

He makes an edit to the index.html page and adds it to his staging index…

git add -A

This is equivalent to doing …

git add .
git add -u

The first command stages modified and new files.  The second command stages removed files.  So the -A stages “all” (modified, new, and deleted) files.

4. git commit

CIO commits the changes to local repository.

git commit -m “added contact feature”

Then he remember that CEO told him to always use present tense in commit comments.  So he amends his last commit using…

git commit -m “adds contact feature”

Doing a git log, he see that there is only one commit with corrected commit comment…

git log

git log

5. git push

CIO wants to push this “contact_feature” branch to the remote GitHub repository for the CEO to review.  He confirms that origin is the GitHub repository …

git remote show origin

git remote show origin

He does a “git fetch” and then “git push” …

git fetch
git push origin contact_feature

The last command says to push to origin the “contact_feature” branch.

And the GitHub accounts shows that new branch placed there by the CIO…

branch pushed

branch pushed

CIO sends email to CEO saying he completed contact feature in the branch “contact_feature”.

6.  git fetch

Upon receiving the email, the CEO does a …

git fetch

and sees there is a new branch …

see remote branches

 

7.  git checkout -b

To download this new branch onto the CEO machine, he …

git checkout -b

git checkout -b

This creates the branch “contact_feature” sourcing from “origin/contact_feature” and checkout (switches) to this new branch.

8.  git diff

CEO does a git diff between master branch and contact_feature branch and see that CIO has remove the “bye” and added the “contact us”…

git diff

git diff

9.  git merge

CEO accepts the change and switches to master branch…

git checkout master

and merges the change from the contact_feature branch to master branch …

git merge contact_feature

The command “git branch –merge” shows what branches have been merged into the current branch …

see what branches have been merged

see what branches have been merged

10.  git push

CEO is now ready to push to origin the master branch (which contains the new contact feature) to the repository.

git fetch
git push origin master

The CEO knows that it is better to specify “git push origin master” in full rather than just “git push”.  Because “git push” can have two different behaviors depending on local git config:

matching: means that it pushes all local branches to remote of the same name

simple: means that it pushes the current local branch to the remote of the same name.

Starting in Git 2.0, the default will switch from “matching” to “simple”.

Once the push command has been performed, the website has been updated …

website updated

website updated

11. Delete the branch

Now CEO wants to delete the branch “contact_feature” …

git branch -d contact_feature
git branch

This deleted the branch from the local machine.  But the remote repository still has branch.  To delete from remote …

git push origin :contact_feature

Think of this as pushing to origin nothing (the nothing in front of the colon) to the remote branch named “contact_feature” (after the colon).

An  equivalent more intuitive syntax is …

git push origin –delete contact_feature

12.  git pull

With …

git checkout master
git fetch
git status

the CIO sees that his master branch is outdated…

master branch outdated

He updates his master branch by …

git fetch
git merge origin/master

Doing “git pull” would be shortcut for those two commands.

13. git remote prune origin

When the CIO does a …

git ls-remote

git ls-remote

he sees that the branch “contact_feature” is no longer on the remote repository.  Or even more informative is …

git remote show origin command

git remote show origin command

It shows that the branch “contact_feature” is stale.

Because he still has the “contact_feature” on his local repository…

local branches

local branches

If he wants to delete the stale branch “contact_feature” on his local repository, he first do a dry-run…

git remote prune

git remote prune

which would show that it would prune origin/contact_feature.

Running …

git remote prune origin

would perform the actual prune.  Now when he shows the remote branches, contact_feature is not there …

show remote branches

show remote branches

But when he shows his local branches, it is still there…

show local branches

show local branches

To remove his local branch “contact_feature”, he does …

git branch -d contact_feature


Related Posts

Tags

Share This