The Complete GIT Workflow

Category : general

Date : April 20, 2014

They say Git is a distributed source code management system but they never say all amazing things you can do with it. For starters it gives your developers great flexibility when working on a common project whether working from home or when travelling.

One another great use-case is the version control. You can revert to a older version or even to your local changes say you want to go back to a version you had working a 4 hours back. We will split the whole Git process flow into four modules;

1. Initialising or Cloning a Project on Git;

To get started with an empty project or to clone an existing project from remote server.

1.1 Initialising a repository in your local machine for a fresh project or cloning a existing project into your local machine. Navigate to the folder in your cmd/terminal using cd /path/to/your/folder/

git init
or
git clone https://github.com/user/repo.git

1.2 Then Add all the files to a file tracking your repository with



git add .

when git add . fails use git add —all

1.3 Commit all the changes with the server config.


git commit -m “Your message"

Descriptive commit messages – Make sure your commit message describe the change you are making. This makes git log useful.

1.4 Push your changes/code to the remote repository.


git push origin master

:master is the branch name and by default you have master. We will talk about branching a bit later.

2. Adding a Remote URL to any exsiting git project;

For a project which is already in a shared repository and you are going to clone it.

2.1 Adding the remote git repository to our .git configuration.

git remote add origin https://github.com/user/repo.git

2.2 Then repeat the process like before, add all the files to a file tracking your repository


git add .

2.3 Commit all the changes with the server config.


git commit -m “Your message"

2.4 Push your changes/code to the remote repository.


git push origin master

3. Pulling Code/Changes from the remote;

When multiple member are working on a project you would have pull in versions into your local repository to develop or even merge with your version.

3.1 Before pulling the remote keep your local version up-to date committed. So add all the changes.

git add .

3.2 Commit all the changes with the server config.


git commit -m “Your message"

3.3 Pull the changes from the base and branch


git pull origin master
git pull —rebase

NOTE: —rebase – to get rid of unnecessary merge commit.

4 Branching and Merging;

When working on a modular application often branches are used to maintain separate modules/features and merged on completion

4.1 Create a separate branch for separate module/developer

git branch testBranch

4.2 Work on your features/module and push the change to the server


git push origin testBranch

4.3 When are ready to merge your changes to master branch then switch to master


git checkout master

4.4 Next step check if the master branch is upto date


git pull origin master

4.5 Next merge the master branch with your testBranch


git merge testBranch

4.6 Push all the merged changes to remote server.


git push origin master

TIP 1: git pull —rebase – to get rid of unnecessary merge commit messages while pulling from master. Merge commit messages are great when you are actually merging a branch to another.

TIP 2: Keep the branch you are working on up to date which means you should rebase with development (considering development is where current development is happening) often. This helps in avoiding merge conflicts.

TIP 3: Make the merge into master only when it is ready for production or passes the Quality check otherwise fix it in your side branch before merging it to the master.

TIP 4: Splitting commits – A single commit message should be related to one part of the story. Small commits are good, easy to merge, revert, log and shows continuous progress.

 

If you have any questions, comments or suggestions, feel free to join the discussion below!