git config --global user.name "John Doe"
git config --global user.email [email protected]
or only set this information locally to a specific repository
git config user.name "John Doe"
git config user.email [email protected]
If same records exist in both global and local settings, local ones are prioritized.
git config --global core.editor "vim"
or set “export GIT_EDITOR=vim” in “~/.bashrc/”.
git status
git add [files]
A short-hand
git add .
will stage all files under current directory
git commit -m 'commit message'
or just
git commit
followed by inputing the commit message interactively.
git add
:git commit -a -m 'commit message'
git mv file_from file_to
git diff
git diff --cached
or
git diff --staged
git log
git log -p -2
git log --stat
--pretty
flag formats the git log
output. For example, to format the git log
output in form of one line for each commitgit log --pretty=oneline
--graph
flaggit log --pretty=format:"%h, %s" --graph
The meaning of %h
and %s
can be found below:
Option Description of Output
%H Commit hash
%h Abbreviated commit hash
%T Tree hash
%t Abbreviated tree hash
%P Parent hashes
%p Abbreviated parent hashes
%an Author name
%ae Author e-mail
%ad Author date (format respects the –date= option)
%ar Author date, relative
%cn Committer name
%ce Committer email
%cd Committer date
%cr Committer date, relative
%s Subject
git log --since=2.weeks
and some useful option flags:
Option Description
-(n) Show only the last n commits
--since, --after Limit the commits to those made after the specified date.
--until, --before Limit the commits to those made before the specified date.
--author Only show commits in which the author entry matches the specified string.
--committer Only show commits in which the committer entry matches the specified string.
git log --graph --decorate --all
git log
isgitk
git commit --amend
That is convenient: it allows you to commit frequency, and in the end it can just be commit if using this amend command after each commit. An example will be
git commit -a -m 'update'
git add forgotten_file
git commit --amend -m 'update2'
git add another_forgotten_file
git commit --amend 'update3'
And in the end there will just be one commit in the log with commit message “update3”.
git commit --amend --reset-author 'update4'
git reset HEAD [files]
which will leave the file as “modified”. To even discard the changes made since last commit, that is, unmodify modified files:
git checkout -- [files]
But “git checkout” will not remove the untracked files and directories. Use “git clean” to remove the untracked ones, do
git clean -f # remove untracked files
git clean -df # remove untracked directories
See the documentation for “git-clean” for details.
First check out a commit from the history:
git checkout [commit] .
Note that the period “.” in the end means apply the changes to the current stage.
If only need to check out a specific file of a specific commit, do:
git checkout [commit] [file-path]
And then commit the changes and make a new commit:
git add .
git commit -m "message"
git rm [files]
git rm --cached [files]
which will leave files as “untracked”.
git remote
git remote add [remote_nickname] [remote_url]
such as
git remote add origin [email protected]:vadiode/git_notes.git
git remote show [remote_nickname]
or with flag -v
git remote show [remote_nickname] -v
git remote rename [old_remote_name] [new_remote_name]
Edit the remote url
git remote set-url origin new_url
Git remote with a custom SSH port (other than 22): edit ~/.ssh/config like this
Host example.com
Port 1234
git remote rm [remote_name]
git push [remote_name] [local_branch_name]:[remote_branch_name]
If the local branch and remote branch have the same names, this command can be shortened as:
git push [remote_name] [branch_name]
Tags can be either lightweight (just a tag name), or annotated (with tagging message). If it is annotated, it can be even signed with GPG.
A newly created tag will not be pushed to remote by default. A git push
is needed if you want to do that.
Show existing tags
git tag
git tag -l 'v1.4.*'
git show [tag_name]
such as
git show v1.4.0
git tag v1.4.1-lw
git tag -a v1.4.1 -m 'version 1.4.1, finished in 12/11/2011'
-s
flaggit tag -s -a v1.4.1 -m 'version 1.4.1, finished in 12/11/2011, with signature'
git tag -a [tag_name] [commit_hash]
such as
git tag -a v1.4.1 9fceb02
git push [remote_name] [tag_name]
such as
git push origin v1.4.1
git push origin --tags
git diff --name-only SHA1 SHA2
Omitting “–name-only” flag will make it show the diff of file content.