Saturday, August 1, 2015

Basic Git Commands

What is Git
Git is a revision control system. It keeps track of changes.

1. show all branch
git branch
view raw git.sh hosted with ❤ by GitHub
This git command list down all your local git branch and also highlight the branch name you are currently in.

2. create new branch
git branch newbranchname
view raw git.sh hosted with ❤ by GitHub
This git command create a new branch named 'newBranchName'.

3. switch to another branch
git checkout branchname
view raw git.sh hosted with ❤ by GitHub
This git command switch to 'newBranchName' branch.

4. create a new branch and switch to it
git checkout -b newbranchname
view raw git.sh hosted with ❤ by GitHub

5. delete a branch
git branch -d branchname
view raw git.sh hosted with ❤ by GitHub

6. rename the current branch
git branch -m newname
view raw git.sh hosted with ❤ by GitHub

7. rename the branch which is not your current branch
git branch -m oldname newname
view raw git.sh hosted with ❤ by GitHub

8. status
git status
view raw git.sh hosted with ❤ by GitHub
This git command list the files you have changed and those you still need to add or commit.

9. add single file
git add filename
view raw git.sh hosted with ❤ by GitHub
add one file to staging

10. add all files
git add *
view raw git.sh hosted with ❤ by GitHub
add all files to staging

11. commit changes to head (but not yet to the remote repository)
git commit -am 'message'
view raw git.sh hosted with ❤ by GitHub
12. send changes to the branch of your remote repository
git push origin branchname
view raw git.sh hosted with ❤ by GitHub
13. update from the remote repository
git pull origin master
view raw git.sh hosted with ❤ by GitHub
fetch and merge changes from the remote server(master branch) to your working branch

14. get list of commit id
git log
view raw git.sh hosted with ❤ by GitHub

15. show difference (changes) between two commit id
git diff sourceCommitId targetCommitId
view raw git.sh hosted with ❤ by GitHub

16. show difference (changes) between two branch
git diff sourcebranch targetbranch
view raw git.sh hosted with ❤ by GitHub

17. changes local working directory code to specific commit
git reset --hard commitid
view raw git.sh hosted with ❤ by GitHub

18. show GUI
git gui
view raw git.sh hosted with ❤ by GitHub

19. undo git add for single file
git reset filename
view raw git.sh hosted with ❤ by GitHub

20. undo git add for all files
git reset
view raw git.sh hosted with ❤ by GitHub

21. undo git commit
git reset --soft HEAD^
view raw git.sh hosted with ❤ by GitHub

22. view your remote branches
git branch -r
view raw git.sh hosted with ❤ by GitHub