Posted by Arjun Arjun on Saturday 14th May 2016, 3 minute read

Getting started with Git

Commands Git

In this post I would like to provide basic usage steps and a list of some basic Git commands to get you going with Git. First let me give you basic information about git.

Git is a version control system that is widely used for software development and other version control tasks. It is a distributed revision control system with an emphasis on speed, data integrity, and support for distributed, non-linear workflows.

Git workflow - Your local repository consists of three "trees" maintained by git. the first one is your Working directory which holds the actual files. The second one is the index which acts as a staging area and finally the HEAD which points to the last commit you have made.

Getting started with git

Tell Git who you are - Configure the author name and email address to be used with your commits.

$ git config --global user.name "Arjun PHP"
$ git config --global user.email [email protected]

To use colorful git output

$ git config color.ui true

Create a new local repository - Create a new directory, open it and issue following command from your terminal to create a new git local repository.

$ git init

Checkout a repository - To create a working copy of a local repository issue following command form terminal.

$ git clone /path/to/your/repository

For a remote server, your command will be

$ git clone [email protected]:/path/to/repository

add - To add one or more files to index/staging, issue following commands from your terminal

$ git add fileName # filename should be valid file path
(or)
$ git add . # you can use dot to add all files

commit - To commit changes to local head(but not yet to the remote repository).

$ git commit -m "your commit message"

Commit any files you have added with git add, and also commit any files you've changed since then with -a flag.

$ git commit -am "your commit message"
(or)
$ git commit -a -m "your commit message" # -a is to add , -m is to commit 

Now the files are committed to the HEAD, but not in your remote repository yet.

push changes - To push changes to the remote repository, issue the following command from your terminal.

$ git push #changes will go to the current branch
(or) 
$ git push origin master #push changes to the remote master branch.  Change master to whatever branch you want to push your changes to. 

Status - To check the list of files you have changed and those you still need to add or commit, issue the following command from your terminal.

$ git status

connecting to a remote repository - If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with following command.

$ git remote add origin 

You can list all currently configured remote repositories with following command -

$ git remote -v

Get Update from the remote repository
To fetch and merge changes from the remote server to your working directory, issue the following command from your terminal.

$ git pull

To merge a different branch into your active branch, issue the following command from your terminal.

$ git merge branchname

To view all the merge conflicts, issue the following command from your terminal.

$ git diff

To view the conflicts against the base file, issue the following command from your terminal.

$ git diff --base 

To preview changes, before merging, issue the following command from your terminal.

$ git diff  

After you have manually resolved any conflicts, you need to add the changed files to git state using following command

$ git add fileName
(OR)
$ git add .

Branches

Branches are used to develop features isolated from each other. The master branch is the "default" branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion.

To create a new branch and switch to it, issue the following command from your terminal.

$ git checkout -b branchName

To switch from one branch to another, issue the following command from your terminal.

$ git checkout branchname

To get list all the branches in your repository, and you are current branch issue the following command from your terminal.

$ git branch

To delete the feature branch, issue the following command from your terminal.

$ git branch -d branchname

To push the branch to your remote repository, issue the following command from your terminal.

$ git push origin branchname

To push all branches to your remote repository, issue the following command from your terminal.

$ git push --all origin

To delete a branch on your remote repository, issue the following command from your terminal.

$ git push origin :branchname

Tagging

Tagging is know concept, we general tag each software release. You can create a new tag named 1.0.0 by issuing following command from your terminal.

$ git tag 1.0.0 commitID

You can get the commit id by issuing following command from your terminal

$ git log

Issue the following command from your terminal to push all tags to remote repository

$ git push --tags origin
...
Arjun

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.