16 examples for Git common usage

ubuntu-logoWe will show a list of common usage examples for git, with the help of these examples, you can turn down most of the issues you faced on Git.

1. git pull — checkout the latest code to local
a). git pull origin master — checkout remote origin to local master branch.

2. git push  — commit local code to remote server.

3. git log  — check log for last commit.
a). git log -p  — check commits logs and changes
b). git log -p -4  — check commits logs and changes for the recent 4 commits.
c). git log frameworks/  — check commits logs for directory frameworks/
d). git log –stat  — check commit logs and a summary of files changes.
e). git log –stat packages/apps/ — same as above but for specified directory.

4. git status  — check code changes since last commit.

5. git diff — check the difference between working directory and local repository.
a). git diff packages/apps/Contacts/AndoridManifest.xml  — same as above but for specified file.
b). git diff b46cae3b9a377bcd9cfc249a5778fce647feb961 dc94bf29aab18168e79d6496a89e47dba7a1d494 — same as above but for two revisions.

6. git commit — commit changes to local repo.
a). git commit file1 file2 -m “log message”   — same as above 提交file1,file2到本地仓库
b). git commit -a -m “log message”  — commit all changes.

7. git add file1 file2  — add new file to repo.

8. git rm — remove files from git repo
a). git rm file3  — remove specified file from git repo.
b). git rm –cached FileName — remove file from git repo but not delete the file.

9. git branch  — list local branches.
a). git branch -a — list all the local and remote branches.
b). git branch new_branch  — create a new branch new_branch
c). git branch -D old_branch  — force delete the branch old_branch
d). git branch -r — list remote branches.

10. git checkout — checkout files in different branches.
a). git checkout branch1 — checkout files in branch1, which means switch to branch1.
b). git checkout origin/cmcc -b cmcc — checkout remote branch origin/cmcc to local branch cmcc
c). git checkout /packages/apps/Contacts/AndroidManifest.xml — checkout AndroidManifest.xml from its last commit. Unsaved change will lose.
d). git checkout /package/app/Contacts/ — same as above but for directory
11. git reset — reset current working directory to a specified revision.
a). git reset [–fixed] 3204f2f0bdfd6ada86c587b8927cdc5a15c586c1
b). git reset [–soft] 3204f2f0bdfd6ada86c587b8927cdc5a15c586c1
c). git reset [–hard] 3204f2f0bdfd6ada86c587b8927cdc5a15c586c1  — force to rollback to specified revision, unsaved changes will lose.
d). git reset –hard origin/master — force to sync with master branch on server side.

12. git merge
a). git merge branch1 — merge changes from branch1 to current branch.
13. git patch
a). git format-patch -1 — create a patch from last commit.
b). git fromat-patch -1 6525c43add803bfee37cf5c1cb573ed8aecf529e — create a patch from a specified revision
c). git format-patch -2 6525c43add803bfee37cf5c1cb573ed8aecf529e — same as above but for different patch type

14. git am xxx.patch — commit patch
a). git am –abort recover git status so that we can commit patch again.

15. git stash
git stash — hidden the changes of working directory
git stash pop — recover the changes.

16. git what changed
git whatchanged packages/apps/Contacts/  — check what have changed in directory packages/apps/Contacts/

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.