Git 基本操作

Github windows

三种状态

Git中的文件有三种主要状态:committed, modified, staged

Committed means that the data is safely stored in your local database.
Modified means that you have changed the file but have not committed it to your database yet.
Staged means that you have marked a modified file in its current version to go into your next commit snapshot.

Git config

// 设置global范围的config
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com
git config --global core.editor emacs

// 查看所有的config
git config --list
// 查看具体的config
git config user.name

导出项目

// 导出项目
git clone https://github.com/libgit2/libgit2
// 重命名导出的文件夹名
git clone https://github.com/libgit2/libgit2 mylibgit  
// 查看状态
git status

Track

项目中的文件还可分为两种状态:tracked, untracked.
untracked的文件通常是新创建的文件,还未被git管理起来。
tracked文件都是快照中的文件,可以是:unmodified, modified, staged.

// 开始tracking untracked file(文件名是README),现在其状态的tracked,并且是staged
git add README

如果修改一个tracked的文件,就是说现在是tracked modified的状态(Changes not staged for commit)此时,文件是unstaged状态。要让文件变成staged,需要再次执行add命令:

git add CONTRIBUTING.md

To stage it, you run the git add command. git add is a multipurpose command – you use it to begin tracking new files, to stage files, and to do other things like marking merge conflicted files as resolved. It may be helpful to think of it more as “add this content to the next commit” rather than “add this file to the project”.

设置需要Git忽略的

在.gitignore文件中设置需要git忽略的文件或文件夹。

# no .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .txt files in the doc/ directory
doc/**/*.txt

查看不同

To see what you’ve changed but not yet staged, type git diff with no other arguments:

// 查看unstaged change
git diff

That command compares what is in your working directory with what is in your staging area. The result tells you the changes you’ve made that you haven’t yet staged.

If you want to see what you’ve staged that will go into your next commit, you can use git diff --staged. This command compares your staged changes to your last commit:

// 查看staged change
git diff --staged

It’s important to note that git diff by itself doesn’t show all changes made since your last commit – only changes that are still unstaged. This can be
confusing, because if you’ve staged all of your changes, git diff will give you no output.

提交

只会提交已经是staged状态的文件,所以如果要提交,事先运行git status查看文件状态,以及git add将文件变成staged状态。

git commit

执行上面命令会后,会出现文件编辑器,提示你输入提交message,你也可以直接将提交信息写在命令中:

// 只提交staged file
git commit -m "Story 182: Fix benchmarks for speed"

对于已经被tracked的文件,在提交时可以让git自动将其变成staged状态,然后提交:

git commit -a -m 'added new benchmarks'

删除

git rm PROJECTS.md

下次提交后,PROJECTS.md文件就不存在了。

如果你想保留文件在磁盘上,只是不想让Git再来tracked,就是变成untracked文件:

git rm --cached README

还可以指定目录以及表达式:

git rm log/\*.log

重命名文件:

git mv file_from file_to

Viewing the Commit History

git log

One of the more helpful options is -p, which shows the difference introduced in each commit. You can also use -2, which limits the output to only the last two entries:

git log -p -2

if you want to see some abbreviated stats for each commit, you can use the --stat option:

// 显示简写的提交历史
git log --stat

Another really useful option is --pretty. This option changes the log output to formats other than the default.

git log --pretty=oneline

The most interesting option is format, which allows you to specify your own log output format.

git log --pretty=format:"%h - %an, %ar : %s"

Undoing Things

If you want to try that commit again, you can run commit with the --amend option:

git commit --amend

if you commit and then realize you forgot to stage the changes in a file you wanted to add to this commit, you can do something like this:

git commit -m 'initial commit'
git add forgotten_file
git commit --amend

Unstaging a Staged File

如果你想让一个staged的文件变成unstaged

git reset HEAD CONTRIBUTING.md

Unmodifying a Modified File

git checkout -- CONTRIBUTING.md

远程服务器

You can also specify -v, which shows you the URLs that Git has stored for the shortname to be used when reading and writing to that remote:

git remote
git remote -v

To add a new remote Git repository as a shortname you can reference easily, run git remote add [shortname] [url]:

git remote add pb https://github.com/paulboone/ticgit
git remote -v

get data from your remote projects:

git fetch pb

When you have your project at a point that you want to share, you have to push it upstream. The command for this is simple: git push [remote-name] [branch-name].

git push origin master

If you want to see more information about a particular remote, you can use the git remote show [remote-name] command.

git remote show origin

Removing and Renaming Remotes

If you want to rename pb to paul, you can do so with git remote rename:

git remote rename pb paul

If you want to remove a remote:

git remote rm paul

Tagging

Listing the available tags in Git is straight forward. Just type git tag:

git tag

Git uses two main types of tags: lightweight and annotated.
A lightweight tag is very much like a branch that doesn’t change – it’s just a pointer to a specific commit.

Annotated tags, however, are stored as full objects in the Git database.They’re checksummed; contain the tagger name, e-mail, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG).
It’s generally recommended that you create annotated tags so you can have all this information; but if you want a temporary tag or for some reason don’t want to keep the other information, lightweight tags are available too.

Annotated Tags

创建tag:

git tag -a v1.4 -m 'my version 1.4'

显示某个tag的具体信息:

git show v1.4

Lightweight Tags

git tag v1.4-lw

提交tag

git push origin v1.5

将所有的tag提交

git push origin --tags

导出tag

If you want to put a version of your repository in your working directory that looks like a specific tag, you can create a new branch at a specific tag with git checkout -b [branchname] [tagname]:

git checkout -b version2 v2.0.0
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容