I am writing this blog to put my understanding and regular practice for Git workflow.

Introduction


In this blog some information might look very simple to the the Git experts; but it might be helpful to the beginners. I will not go brief into the advantages of using git; but I will try to elaborate what should be the basic workflow for git into your project. Before playing with git, the basic idea of current working project or work space, index area, local repository and remote repository makes it easy to understand.



Workspace: It is a current working project on your IDE or any other editor. So initially each file and folders of your project will be in this state until you perform any git operation on it.

Index Area: It is a cache or temporary space in your personal machine. In other words it is a private area acquired by Git in your machine to temporary store your data. So question comes like why is it required? This memory makes Git able to provide offline storage option. How? I will explain it later. You should perform git add <path to file> command to add the specific files in index area.

Local Repository: It is again same temporary space in your local machine, where you will commit your changes. Now what is it and why is it required? Commit has same meaning as it has in database queries. You perform any operation and then you finalize it with commit. Think if you want to add some files and want to tag this changes as a specific operation in offline. You should perform git commit -m "<commit message>" to commit changes.

Remote Repository: As name suggests it is a remote storage for your file structure. So it could be on Github, Stash, Bitbucket or etc. Here you requires internet access to push data from your local machine to remote machine(server).


Workflow


1) Create project repository: 

You can create new project repository with git init command in your terminal or you can manually create a specific repository at remote location (GitHub) and clone it to the local git workspace.
                           git clone <url(ssh or http)>
It is good practice to put .gitignore file in your workspace, it contains the list of the file paths, which are not required to push to the remote repository.

2) Verify your working branch:

Git provides n number of branch creation on specific project. When you clone existing project all branches meta-data gets downloaded on your local machine. By default current working branch will be *master branch. Each project has master branch which creates by default.

a) How to check current working branch and list of all branches in your project?
                                 git branch
b) How to create new branch?
                        git branch <new-branch-name>
c) How to switch to specific branch?
                         git checkout <branch-name>
d) How to delete specific branch?
                         git branch -d <branch-name>
                         git branch -D <branch-name>
With second command Git will not prevent you to delete this branch, even though you have un merged changes on it.

Big projects will have so many branches. Main branch would be production branch or it may have any random name. It will also have branches for normal bug fixes. Personally I think creating new branch and working on it for any experiments is a very good practice, with this approach you can easily identify and add or revert commits.

3) Add your changes or new files to the Index area:

You can add your changes in regular time period not to lost it. In other words some time it happens that due to some problem your current project environment goes down or it gets crash, in this kind of scenario your recent work must be stored at somewhere before it simply disappears. So here index area plays good role by saving your data locally in cache. Since you are storing data locally, it does not require any internet connection. You can use following commands to stage your changes on index area.
                                 git add .
                                git add -all
                             git add <file-path>
a) How to un stage files from index area?
                         git reset HEAD <file-path>
b) How to discard file from staging to index area?
                         git checkout -- <file-path>

4) Commit your changes on local repository:

I like to commit my files on completion of any feature or task. It means you should tag changes with specific commit message after completing specific feature or task in project. A good commit message is required to make it easy for other developers and testers to know about your commit. You can revert local commit as well.

How to commit changes?
                        git commit -m "<commit-message>"
How to revert commit?
                              git reset HEAD~1
How to check commit logs?
                                  git log
How to set your head to specific commit?
                          git reset --hard fj5789sufj
                    git push -f origin fj5789sufj:<branch-name>

5) Push your commit to the remote repository:

After single commit or multiple commits it is good practice to push your local commits to the remote repository. It requires internet connection for action. Here it mayhap conflict with your commit while pushing to the remote branch.

a) How to push to remote repo?
                        git push origin [<branch-name>]
Sometime you lost the origin of the current repository. So while you push, git does not find the origin of the repository. You can set it with command:
                   git remote add origin <url(ssh or http)>
b) How to change or reset remote origin url:
                   git remote set-url origin <url(ssh or http)>
c) How to check current remote url:
                              git remote -v


Git Pull:


If on your branch more than one people works then other may add their commits in between. So when you push your local commits to the remote repository before merging your changes to the remote branch Git will tell you to get(pull) all the changes(commits) from the other developers to your local repository first with command:
                                git pull
Here it is possible that you could not able to do pull successfully because of your uncommitted changes. In other words in your local machine if you have not committed all the changes and if you try to pull from remote repository then Git will stop you from doing that. Here you have two options to get over with this problem

1) Commit all changes
2) Stash your changes : It is a good option if you are not ready to commit your files.

How to Stash current state?
                               git stash
How to remove latest stashed changes?
                             git stash pop

Merge and Rebase:


If you want to add all commits of your working branch to other working branch then you should merge or rebase all your commits of your current working branch to other branch.

I prefer Rebase over Merge because both makes your head on same stage, but with rebase you will not loose any commit log on the branch.

a) How to rebase your current branch against master branch:
                            git pull --rebase
           git pull --rebase origin <current-remote-branch-name>
After merge or rebase command you might find conflict error in code. Conflict happens because you and other person have tried to modified same line of the code in file. Git tries to resolve conflict related to new line and blank spaces but for other type of merge conflicts it puts specific message in the code with log of commit string and head state. You should manually check those conflicted files and after editing you should add it again to the index. Thus you can resolve those conflicts and push your merged code to the master or any other remote branch.

Revert and Reset:

 To revert (undo) specific commit.
git revert <commit | HEAD>


Comments

Post a Comment