Git is a really important tool in the developer world. I sometimes ponder upon the thought that if there was no git how hard would it would be keep track of our codes and it's security.
Surely we have got an magic wand with us to just fling and boom your code is on the internet safe from any flood or fire that may try to destroy your precious code.
So today I'm writing some of the most used commands of git. These will help me remember them and any other beginner who stumbles upon it.
In simple terms it converts a directory into an empty repository. Only after running this command we can do other commands such as commit and add.
git init [project name]
This command adds all the files into a staging area so that you can commit it later at once. It can be done by a number of ways, you can either add entire directories or a single file or all un-staged files.
git add <temp.txt>
Commit is just like a track keeper. It keeps the records of changes that are made in the local repository. That is why you might notice that you need to provide a message along with the commit. It helps a third person to easily understand what changes you have made and where you have made it.
git commit –m “Message to go with the commit here”
It's pretty much self explanatory but what it does is return the current state of the repository.
Branching is probably the most important part of git. It helps collaborative development and is used most after commit. Git branch helps us adding a new branch, determine which branch we are in, and even delete a branch.
git branch <branch_name>
git branch -d <branch_name>
Listing all branches in the remote or local repository:
git branch -a
Once you create a branch you can go ahead and make changes that do not affect the main branch. But once you're done you need to merge both branches. And that's where git merge comes in. It combines two branches and any changes done in one of them.
More on branching in another blog.
git merge <branch-name>
Git clone is used for creating a local repository for an existing remote repository. It downloads and copies the remote repository to your computer. It's equivalent to git init for a remote repository. You use init for creating a remote repository and clone to create a local repository.
For remote server:
git clone username@host:/path/to/repository
For copying a local repository:
git clone /path/to/repository
Pulling something from the remote repository requires the git pull command. It pulls the latest version of the remote repository to your local repository.
And finally git push. Its essentially the opposite of git pull. It pushes your local repository to your remote repository.
git push origin <master>
I've listed some of the most used git commands, there are still some advanced commands that you should definitely check out.