@torresdyl
2018-04-14T18:42:29.000000Z
字数 8513
阅读 3423
git
Ref:
http://gitqwerty777.github.io/git-commands/
Some terms:
git add
;git commit
/git push
.This image is useful:
In local directory, use git bash to execute git init
.
Create a repo in github.com
or other git site, and copy the repo direction. Remember to copy the "https" version URL and delete .git
part.
Again in local directory, like /home/usr/MyLocalRepo
, use git bash to execute git add .
to add all files (only modified and new, don't delete) in this dir recursively. To add all files, use git add -A
. To delete some files, use git rm file_name
. Wildcards are supported, e.g., you can use rm *.class
to delete all .class
files.
If you want to add .gitignore
and README.MD
, use touch
to create: touch .gitignore
.
Default to be local master branch.
git commit -m "some comment"
git remote add origin <URL>
=
is needed:# set user name and email of git. "global" means for all users.
git config (--global) user.name "First Last"
git config (--global) user.email "user@example.com"
# using UI colors to make Git colorful
git config (--global) color.ui true
# config proxy
git config (--global) http.proxy http://proxyuser:proxypwd@proxy.server.com:8080
git config (--global) https.proxy http://proxyuser:proxypwd@proxy.server.com:8080
git config (--global) ftp.proxy http://proxyuser:proxypwd@proxy.server.com:8080
# stop using proxy
git config (--global) --unset http.proxy
# set "git log" date format to be like "2017-07-31 15:55:02"
git config log.date iso
# list all config values
git config -l
push
to remote repogit push origin master
git log
git remote -v show
We will get:
origin https://github.com/xxxxx (fetch)
origin https://github.com/xxxxx (push)
# simple pull
git pull origin master
# pull from remote branch B to local branch A
git pull origin/[branchB] [branchA]
# download without updating
git fetch origin
# overwrite local changes with newest from remote
git fetch --all
git reset --hard origin/master
(git reset --hard origin/<branch-name>)
(Answer from:
https://stackoverflow.com/questions/1125968/how-do-i-force-git-pull-to-overwrite-local-files)
Difference between reset --mixed
(default) and reset --hard
:
(https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard)
reset --mixed
: move head pointer, move index/stage to match head, so git status
shows clear. Files changes are there but you have to add them again;
reset --hard
: change stage area, head pointer, and all uncommited file changes are aborted.
# fetch all content in remote
git fetch --all
# compare current working dir with the fetched content
git diff origin/master
# "origin/master" means remote. We don't have to specify current working dir, it is by default.
# same as last one, but only showing file names, without details
git diff --name-only origin/master
It is like git fetch --dry-run
: comparing without changing. But fetching in dry-run makes no sense: we want to download, but not to change files.
You can also do:
git fetch -all
# avoid commit and fast-forward commit
git merge --no-commit --no-ff $BRANCH$
But this will touch the working dir. The safest way may be:
https://stackoverflow.com/questions/501407/is-there-a-git-merge-dry-run-option#answer-6283843
git fetch origin master
git merge-base FETCH_HEAD master # generate a <mergebase> hex id
git merge-tree <mergebase> master FETCH_HEAD
git log
date formatYou can use git log --date=[some-format]
, or with setting log.date
variable to do it once-and-for-all.
The permitted value of [some-format]
can be iso
, local
.
git pull
is neededhttps://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git#answer-3278427
is not correct! It behaves wrongly and acts like a normal git pull --dry-run
git pull
.
# update remote ref. use -v would be better and suffice.
git remote update
git remote -v update
#
git status -uno
git diff --stat --cached origin/master
Mind we have /
here. We will get:
manifest.json | 2 ++
1 file changed, 2 insertions(+)
Something like git diff origin master
. We have space here.
To get only numbers of files changed:
git diff -numstat origin master
Mind we have space here.
diff-tool
git init
git checkout -b X
touch .gitignore
vim .gitignore
git add --all
git commit -m "some text"
git remote add origin https://xxx.xxx
git push origin X
.gitignore
syntaxAf for a regular Maven project, I would do:
# don't include any .class file
**/*.class
# ignore any Eclipse related file
.classpath
.settings/**
.project
More details can be found at: https://git-scm.com/docs/gitignore
A blank line matches no files, so it can serve as a separator for readability.
A line starting with
#
serves as a comment. Put a backslash (\
) in front of the first hash for patterns that begin with a hash.Trailing spaces are ignored unless they are quoted with backslash (
\
).An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash (
\
) in front of the first "!" for patterns that begin with a literal "!", for example,\!important!.txt
.If the pattern ends with a slash(
/
), it is removed for the purpose of the following description, but it would only find a match with a directory. In other words,foo/
will match a directoryfoo
and paths underneath it, but will not match a regular file or a symbolic linkfoo
(this is consistent with the way how pathspec works in general in Git).If the pattern does not contain a slash (
/
), Git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the.gitignore
file (relative to the toplevel of the work tree if not from a.gitignore
file).Otherwise, Git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the
FNM_PATHNAME
flag: wildcards in the pattern will not match a/
in the pathname. For example,Documentation/*.html
matches "Documentation/git.html" but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning:
A leading
**
followed by a slash/
means match in all directories. For example,**/foo
matches file or directory "foo" anywhere, the same as pattern "foo".**/foo/bar
matches file or directory "bar" anywhere that is directly under directory "foo".A trailing
/**
matches everything inside. For example,abc/**
matches all files inside directory "abc", relative to the location of the .gitignore file, with infinite depth.A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.
Other consecutive asterisks are considered invalid.
If we encounter this problem when we pull:
fatal: refusing to merge unrelated histories
We can double check the files and if there is no file conflict, we can use:
git pull origin master --allow-unrelated-histories
For example, if we have all files in local, and a readme file in the repo, we can download this file to local, and merge history pull. Local files will not be overwritten. And then push.
https://stackoverflow.com/questions/13716658/how-to-delete-all-commit-history-in-github
git clone https://github.com/xxxxx/xxx
git checkout --orphan latest_branch
git add -A
git commit -am "commit message"
git branch -D master
git branch -m master
git push -f origin master
PS: this will not keep your old commit history around
Not possible, because Git does not follow system/soft links.
What we can do, is to:
- create hard link (Windows: mklink /j
; Linux: ln
)
- copy file to Git repo and link back: make app use link.
https://stackoverflow.com/questions/86402/how-can-i-get-git-to-follow-symlinks