Thursday, October 19, 2017

15 most important Git command

15 most important Git command

If you are a web developer or software developer in general, you must be familiar with git as a tool for version control and github as place where you push your codes. Learning git is a must for every software developer, even if you are working alone and not on a team.

There are graphical interface application for git, but most of the time you are going to use the git command line, in fact there are some task that just easier if you use git command line, so learning git command line is a must have skill for every software developer.

But you don't need to learn all git commands in order to start working with it, in my opinion there are at least 15 git commands that every body should know. With these 15 git commands should be able to get you going with git and github.

So ladies and gentleman, these are 15 most important git commands:

1. git status

The git status will gives you information about the current branch you are on and if there are any changes to the files that you made and if there are any file that needs to be commit.
git status

2. git init

If you have existing files inside a folder that you want to put on a repo, you need to run git init, this will initialize git on that folder.
git init

3. git clone

The git clone command is where you want to copy existing repository on github in your local computer. This command takes one parameter which is the url of the repository on github.
git clone [repository-url]
Example:
git clone https://github.com/laravel/laravel.git

4. git remote

Git remote is the command that responsible for managing remote repository, you can show list of remote repo, add remote repo and delete remote repo. Basically you can have more than one remote repository on your local repo.

Add remote repo
git remote add [remote-name] [remote-url]
Example:
git remote add origin https://github.com/user/repo.git
git remote add repo1 https://github.com/user/something.git
git remote add repo2 https://github.com/user/what-ever-repo.git

Show list remote repo
git remote -v

Delete remote repo
git remote rm [remote-name]
Example:
git remote rm origin
git remote rm repo1
git remote rm repo2

5. git diff

Git diff is handy command that will display your changes that is not yet in staging, simply type in git diff and scroll down with arrow up and down on your keyboard to view the changes.
git diff


6 git add 

Git add is where you put the changes that you made into staging, this is a step before you do git commit.
git add [filename]
Example:
git add config.php
git add app.js
git add style.css
You can do this to add all files:
git add .


7. git commit

After you do git add, the next step if you really want to make the changes permanent then you can do git commit, this will commit your changes and put your name on the log. After you do a commit normally you push it to the remote repository like github.
git commit -m [name/description of your changes]
Example:
git commit -m "fixing bug on admin controller"
git commit -m "add new migration file"
git commit -m "clean up codes"

8. git log

The git log command is for displaying the list of commit made by all user, it's also display changes made through git merge. This command very useful to see the last merge and commit by all user.
git log

9. git push

Git push is the command that you use to push the changes that you made to remote repository, normally you do git push after you make a commit. If your push get rejected, that means your local branch is not up to date with the latest changes on the remote repository, you need to do git fetch and git merge to solve the issue.
git push [remote-name] [branch-name]
Example:
git push origin master
git push repo1 development
git push github feature

10. git fetch

Git fetch is the command that you run before you do git merge, this command will shows if there are any new changes on a branch on the remote repository.
git fetch

11. git merge

Git merge is the command for updating your local branch to the latest changes on remote repository, before you can push sometimes you need to merge first to the latest changes.
git merge [remote-name/branch-name]
Example:
git merge origin/development
git merge origin/master
git merge repo1/new-feature

12. git checkout

The git checkout can be use for several things, first you can use it to move from one branch to another, second you can cancel changes that you made that's not in stage yet, third you can use git checkout to create new local branch based on remote branch.

Moving to another branch
git checkout [branch-name]
Example:
git checkout master
git checkout development
git checkout new-features

Canceling changes/undo changes
git checkout [file-name]
Example:
git checkout config.php
git checkout app.js
Undo all changes:
git checkout .

Creating local branch based on remote branch
git checkout -b [local-branch-name] [remote-name/remote-branch-name]
Example:
git checkout -b master origin/master
git checkout -b mybranch origin/development
git checkout -b something repo1/feature


13. git branch

The git branch command will display your current branch clearly and list of other branches on your local machine.
git branch

14. git stash

The git stash is a bit more advanced command but it could be very useful sometimes, what this command does is temporarily hide any changes that you made that's not yet stage and then later when you want it back you can run git stash apply and your changes will shows up again.

Why this is useful? because there are times when you want to do git merge but you are currently working on something, git stash is perfect for that, just remember to run git stash apply to bring back your hidden changes.
git stash
git stash apply

15. git reset

Last but not least, we have git reset which very handy when you want to undo commit or putting files that are staged into unstage.

Undo last commit:
git reset HEAD~

Undo stage (undo git add):
git reset [file-name]
Example:
git reset config.php.sample
git reset server.js
git reset style.css

So that's 15 most important git command that every body should know, specially beginners. Learning all of these 15 command is enough to get you going and understand how git works. I believe the best way to learn is by trying it out and get your hands dirty.


No comments:

Post a Comment