- GIT INSTALLATION
For GNU/Linux distributions Git should be available in the standard system repository. For example in Debian/Ubuntu please type in the terminal:1
$ sudo apt-get install git
If you want or need to install Git from source, you can get it from https://git-scm.com/downloads.
An excellent Git course can be found in the great Pro Git book by Scott Chacon and Ben Straub. The book is available online for free at https://git-scm.com/book.
- GIT CONFIGURATION
1 | $ git config --global user.name "Your Name" |
Set the name that will be attached to your commits and tags.
1 | $ git config --global user.email "you@example.com" |
Set the e-mail address that will be attached to your commits and tags.
1 | $ git config --global color.ui auto |
Enable some colorization of Git output.
- STARTING A PROJECT
1 | $ git init [project name] |
Create new local repository. If [project name] is provided, Git will createa new directory named [project name] and will initialize a repository inside it.If [project name] is not provided, then a new repository is initialized in currentdirectory.
1 | $ git clone [project url] |
Downloads a project with entire history from the remote repository.
1 | $ git remote set-url subrepo/common ssh://git@code.gongyuanhezi.cn:8001/iot/common.git |
Change remote url
- IGNORING FILES
1 | $ cat .gitignore |
Thanks to this file Git will ignore all files in logs directory (excludingthe .gitkeep file), whole tmp directory and all files *.swp. Described fileignoring will work for the directory (and children directories) where .gitignorefile is placed.
- DAY-TO-DAY WORK
1
$ git status
See the status of your work. New, staged, modified files. Current branch.
1 | $ git diff [file] |
Show changes between working directory and staging area.
1 | $ git diff --staged [file] |
Shows changes in the staging area that haven’t been commited.
1 | $ git checkout -- [file] |
Discard changes in working directory. This operation is unrecoverable.
1 | $ git add [file] |
Add a file to the staging area. Use . instead of full file path, to add allchanges files from current directory down into directory tree.
1 | $ git reset [file] |
Get file back from staging area to working directory.
1 | $ git commit [-m "message here"] |
Create new commit from changes added to the staging area. Commit musthave a message! You can provide it by -m. Otherways $EDITOR will be opened.
1 | $ git rm [file] |
Remove file from working directory and add deletion to staging area.
1 | $ git stash |
Put your current changes into stash.
1 | $ git stash pop |
Apply stored stash content into working directory, and clear stash.1
$ git stash drop
Clear stash without applying it into working directory.
- GIT BRANCHING MODEL
1 | $ git branch [-a] |
List all local branches in repository. With -a: show all branches (with remote).
1 | $ git branch [name] |
Create new branch, referencing the current HEAD.
1 | $ git checkout [-b] [name] |
Switch working directory to the specified branch. With -b: Git will create thespecified branch if it does not exist.
1 | $ git merge [from name] |
Join specified [from name] branch into your current branch (the one you areon currenlty).
1 | $ git branch -d [name] |
Remove selected branch, if it is already merged into any other. -D instead of-d forces deletion.
- REVIEW YOUR WORK
1 | $ git log [-n count] |
List commit history of current branch. -n count limits list to last n commits.
1 | $ git log --oneline --graph --decorate |
An overview with references labels and history graph. One commit per line.
1 | $ git log ref.. |
List commits that are present on current branch and not merged into ref.A ref can be e.g. a branch name or a tag name.
1 | $ git log ..ref |
List commit, that are present on ref and not merged into current branch.
1 | $ git reflog |
List operations (like checkouts, commits etc.) made on local repository.
- TAGGING KNOWN COMMITS
1 | $ git tag |
List all tags.1
$ git tag [name][commit sha]
Create a tag object named name for current commit.Add commit sha to tag a specific commit instead of current one.
1 | $ git tag -a [name] [commit sha] |
Create a tag object named name for current commit.
1 | git tag -d [name] |
Remove a tag from a local repository.
1 | $ git push origin master && git push --tags |
Push commit to origin master and push all tags
- REVERTING CHANGES
1 | $ git reset [--hard] [target reference] |
Switch current branch to the target reference, and leaves a difference as anuncommited changes. When –hard is used, all changes are discarded.
1 | $ git revert [commit sha] |
- SYNCHRONIZING REPOSITORIES
1 | $ git fetch [remote] |
Fetch changes from the remote, but not update tracking branches.
1 | $ git fetch --prune [remote] |
Remove remote refs, that were removed from the remote repository.
1 | $ git pull [remote] |
Fetch changes from the remote and merge current branch with its upstream.
1 | $ git push [--tags] [remote] |
Push local changes to the remote. Use –tags to push tags.
1 | $ git push -u [remote] [branch] |
Push local branch to remote repository. Set its copy as an upstream.
- git subrepo
1 | $ git subrepo status |
Show subrepo status
1 | git subrepo init common -r ssh://git@code.gongyuanhezi.cn:8001/iot/common.git |
Init remote repo common
as subrepo
1 | git subrepo pull common |
1 | git subrepo push common |
command | explain |
---|---|
commit | an object |
branch | a reference to a commit; can have a tracked upstream |
tag | a reference (standard) or an object (annotated) |
HEAD | a place where your working directory is now |