GIT for newbies

Why and how to use git..?

I wondered why we use git when I was newbie ruby on rails developer. We could have exchanged code using a pendrive or mails. After 1 and a half years on rails, I can’t imagine code without GIT.

GIT is used for Version Control – meaning, which file has been modified, added or removed. Suppose a team of 30 people are working on a product, how do we work simultaneously without being dependant on each other and what are the changes made and by whom. Code becomes complex and huge as all 30 work everyday, so you know what’s the solution now – GIT

We use following GIT commands for version control.

git init – Creates an empty Git repository – basically a .git directory with subdirectories for objects. An initial HEAD file that references the HEAD of the master branch is also created.

git status – Shows the file modified, added or deleted to exisiting codebase.

git clone https://your_git_url – Copies the code from the url to your local machine.

git branch – Displays the current branch that you have been working on.

git checkout -b your_new_branch_name – Creates a new branch from the branch you were previously in.

git checkout your_existing_branch – Switches into another branch from previous branch.

git remote add your_alias https://your_url – Creates alias for the url to the GIT repository.

git remote -v – Gets all corresponding aliases w.r.t url where your code is linked to GIT repository.

git add . – Adds all files staged to be committed.

git commit -m your_message_here – Commits the piece of code which has been staged previously.

git push origin your_branch_name – Updates your_branch with the respective commits to the GIT repository.

git pull origin your_branch_name – Updates your_branch locally with the respective commits from the GIT repository.

Note: Never commit or push the code to the Target Branch, ie. say master branch or develop branch.

We shall learn more GIT in upcoming posts.

Leave a Reply

Your email address will not be published. Required fields are marked *