[关闭]
@torresdyl 2018-04-14T18:42:29.000000Z 字数 8513 阅读 3423

[GIT]常用命令

git


Ref:

http://gitqwerty777.github.io/git-commands/

Some terms:

This image is useful:

I. Init and create repo

1. Init dir

In local directory, use git bash to execute git init.

2. Create remote

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.

3. Stage local changes

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.

4. Commit changes to local repo.

Default to be local master branch.

git commit -m "some comment"

5. Set remote to be the URL of our remote repo.

git remote add origin <URL>

6. Some configs if first time using git in this machine, no = 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

7. At last, push to remote repo

git push origin master

II. Check log

git log

III. Check remote

git remote -v show

We will get:

origin  https://github.com/xxxxx (fetch)
origin  https://github.com/xxxxx (push)

IV. Pull and force pull/overwrite local changes

# 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.

V. Fetch and don't change/merge before checking

# 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

VI. Change git log date format

You 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.

VII. Check if remote is changed and a git pull is needed

https://stackoverflow.com/questions/3258243/check-if-pull-needed-in-git#answer-3278427
git pull --dry-run is not correct! It behaves wrongly and acts like a normal git pull.

# update remote ref. use -v would be better and suffice.
git remote update
git remote -v update

# 
git status -uno 

VIII. Check what will be pushed before pushing

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.

IX. Configure diff-tool

https://stackoverflow.com/questions/10713925/how-can-i-make-winmerge-my-git-mergetool/30699239#30699239

X. Init, push, clone from a branch, not HEAD

1. Create a local repo for branch X

git init
git checkout -b X

2. Add content and commit to local branch

touch .gitignore
vim .gitignore
git add --all
git commit -m "some text"

3. Add remote

git remote add origin https://xxx.xxx

4. Push to remote branch

git push origin X

XI. .gitignore syntax

Af 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 directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (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.

XII Force pull merge history (or pull)

If we encounter this problem when we pull:

  1. fatal: refusing to merge unrelated histories

We can double check the files and if there is no file conflict, we can use:

  1. 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.

XIII Clear commit history

https://stackoverflow.com/questions/13716658/how-to-delete-all-commit-history-in-github

  1. Use an empty dir to clone:
  1. git clone https://github.com/xxxxx/xxx
  1. Checkout
  1. git checkout --orphan latest_branch
  1. Add all the files
  1. git add -A
  1. Commit the changes
  1. git commit -am "commit message"
  1. Delete the branch
  1. git branch -D master
  1. Rename the current branch to master
  1. git branch -m master
  1. Finally, force update your repository
  1. 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

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注