Monday, November 2, 2015

List of most important git commands


There are so many git commands but we don't need to know everything, specially for beginners, you just need to know the basic.

So here's list of git commands that you probably use very often.

git status
The git status command will give you information about your current branch as well as any change to the file on that branch.

git status

git branch
If you type git branch, it will show you list of branches on your local directory and shows you what branch you are currently on.

git branch
git branch -a

git checkout [name_of_branch]
The git checkout command followed by name of the branch will move you to that particular branch. So git checkout is for moving around to another branch.

git checkout development
git checkout master

git remote -v
To show remote repository on your local computer, you can use git remote -v command to do that.

git remote -v

git add .
If you have files that's being edited or you have new files, and then you want to make a commit, first you need to add those files to stage, by using git add command.

git add .
The "git add ." will add all files that's being edited and new files, you can also add individual files by typing the name of the file instead of "." after git add.

git add sample.php

git commit -am
After putting to stage, you can create new commit using git commit -am followed by the name of the commit. The name of the commit should describe the changes that you are made on that commit.

git commit -am "change variable name"
git commit -am "add complicated query on user controller"

git push
After creating a commit, you can push the commit to remote repository on github by using git push command.

git push origin master
git push origin development

git pull
Sometimes, when you push your new commit to github, you get rejected, that's because your local branch is not up to date, you need to update your local branch to get the latest version, simply run git pull.

git pull

git stash
When you need to update your local branch with the latest update (git pull), but you are in the middle of something (editing a file), use git stash to temporary hide your work so that you can run git pull without having to make a commit first.

git stash
And then use git stash apply to restore your work, after running git pull or moving to another branch.

git stash apply

git diff
When you are editing something and you are not yet run git add command (not yet staged), you can view your editing with the previous one by running git diff command to see the difference.

git diff

For beginners i think that's all you need to know, to have better understanding about git and github, i strongly suggest that you put it on practice, try creating new repo, editing files, add new files, make commit, pushing to github and so on.

No comments:

Post a Comment