Git workflow

In my previous blog post, I wrote about why to use git ?

Now, let’s start working with it.

Since most of them prefer Github (proprietary) rather than Gitlab (Free). This will be in context with Github. There will not be much difference if you are using Gitlab, the workflow remains same.

I assume that you have created an account on Github and downloaded git in your system and configured it.
If not, follow this set up guide.

1. Fork the repository

First you need to fork the repository to your remote server. You can fork the repository by clicking on fork button (ϒ) which you see in the top right corner. Forking a repository is nothing but you have a copy of that repository on your account.

2. Clone the forked repository

If you want to make some changes to the forked repository, you need to clone it.
You can clone the repository by following command :

$ git clone {URL_of_the_repository}

In my case it will be :

$ git clone https://github.com/sagarhani/learn-git

In your case it will be :

$ git clone https://github.com/{your_username}/learn-git

3. Create a new branch

Now move inside the cloned directory by :

$ cd learn-git

It is always good practice to keep the master branch clean. So, create a new branch and work on that branch.
First, check currently which branch are you on, by doing :

$ git branch

It will show master.

You can create a new branch by doing :

$ git checkout -b my-feature

It automatically switch the current branch (master) to my-feature branch.

You can again verify it by doing :

$ git branch

This time it will show my-feature.

You can create any number of branches. You can switch to any branch at any point of time by :

$ git checkout {branch_name}

To switch to master branch :

$ git checkout master

4. Work on the feature

Now you can open your favourite text editor and work on the code. You can modify/add/delete files.

For example : Create a file hello.txt and add some text inside it.

5. Adding your work to staging area

Once you are done with working with code. You need to add them to staging area from your working directory.

Do :

$ git status

This will show all files which were modified and added or removed.

Now, you need to add these changes to staging area to tell git that you need to update these changes in you next commit.

You can add a file by :

$ git add {filename}

In this case it will be :

$ git add hello.txt

6. Commit your work

Git commit command commits the staged works snapshot to the project history. Committed snapshots can be thought of as “safe” versions of a project. Git will never change these work unless you explicitly ask it to change it.

$ git commit

Commit the staged snapshot. This will launch a text editor prompting you for a commit message. After you’ve entered a message, save the file and close the editor to create the actual commit.

$ git commit -m "{message}"

Commit the staged snapshot, but instead of launching a text editor, use <message> as the commit message.

Commit a snapshot of all changes in the working directory. This only includes modifications to tracked files (those that have been added with git add at some point in their history).

$ git commit -a

Combining all the above you can simply do :

$ git commit -am "Added hello.txt file"

7. Update your remote repo with master branch of the original repo.

While you’re working, many other developers around the world have been busy merging changes to the original repository. Most likely there won’t be overlap, but there may be some merge conflicts if two people have made changes to the same Instant Answer.

It’s best practice to start each work session by updating your fork with the latest changes. This is one major reason we kept master free of changes. We can use it as an ongoing reflection of the original repository’s master branch.

To do this, you’ll need to add a ‘remote‘ that points to the original, main repository. We’ll call it upstream.

In this case the command will be :

$ git remote add upstream https://github.com/sagarhani/learn-git

Update your master branch. Move to master branch to by git checkout master

Pull the updates form the upstream

$ git pull upstream master

This will pull the changes from the latest master branch into yours, so you’re up-to-date.

Next, to update your working branches off of master, merge or rebase with the master branch.

If you encounter any merge conflicts, don’t worry, GitHub has a great guide for how to resolve them. 😀

8. Make a pull request

If you want to submit your changes, you need to make a pull request on the original (upstream) repository.

A pull request is the process of submitting changes to a collaborative project. It’s your way of asking that the changes made on your fork be incorporated into the original repository.

The first step to making a pull request is updating the clone of your repository that lives up on GitHub.com. Go to the branch you’re working on, and type:

$ git push origin my-feature

This updates the equivalent branch on GitHub.com. Next, visit your repository on github – https://github.com/{your_username}/learn-git. Find the branch you just pushed. Finally, click the Pull Request button.

9. Update your pull request

Once you have created a pull request, you pull request page will be the place where the feedback/discussion takes place. Usually the repo owner comments on the page if any changes is required.  You can continue to make ongoing changes to your pull request. Simply keep making commits – and push them your fork on Github – and the pull request will update automatically. Once it is satisfied by the repo owner he/she will merge it. 🙂

Bingo! You made some contribution to that project. 😀

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s