git command 总结
该文章总结自Youtube视频:
Git Tutorial for Beginners: Command-Line Fundamentals BY Corey Schafer
link.
Git介绍
Git是一种分布式的文件管理系统,它的优点在于能够将本地的代码同步到远端,共同的开发者可以从远端服务器上获取并参与编辑你的代码,同时你们各自本地的代码又不会被改动,所以当任何一处的主机发生故障断电等一些列情况时,你们的代码不会丢失。
//check git version
$ git --version
//作用是:当你与其他开发者一起工作时,别人需要知道是谁在代码上做出改动
//set global configurations variables
$ git config --global user.name "Zitong"
$ git config --global user,email "zitong@gmail.com"
//to check the set values
$ git config --list
//need help
$ git help <verb>
or
$ git <verb> --help
git 有两种通用的情况:
- 你有本地代码想要通过git追踪
- remote有一个project,你想对它进行开发
Working Directory: 即本地untracked and modified files
Staging Area: 我们organize以及选择想要commit到remote repository的area
//首先cd到你本地的文件夹下
$ git init //initialize an empty git repository
$ ls -la //此时你会发现文件夹下出现了一个 .git 的文件,.git是一个directory,里面包含了所有与repository有关的文件
$ rm -f .git //如果你不想再使用git追踪该文件,你需要做的就只是去删除.git目录
$ git status //lists untracked files
$ touch .gitignore //创建一个.gitignore文件用于存放不想被track的文件
//打开.gitignore文件,复制如下几行:
.DS_Store //ignore后缀为.DS_Store的文件
.project //ignore后缀为.project的文件
*.pyc //ignore后缀我.pyc的文件
$ git add -A //把所有untracked的文件都add到staging area
or
$ git add <file>
$ git status
$ git reset <file> //remove file fro the staging area
$ git reset //remove everyting from the staging area
$ git commit -m "Initial commit" //add files to the repository
$ git status //working directory clean
$ git log //check the commit we just made
$ git clone <url> <where to clone> //cloning a remote repository
//eg: $ git clone http://www.hao123.com/xxx.git .
//注意这个结尾处的 . 代表克隆到当前地址下
//查看remote repository的信息
$ git remote -v
or
$ git branch -a
//Pushing changes
$ git diff //show the changes
$ git status
$ git add -A
$ git commit -m "modified function"
//then push
$ git pull origin master //pull changes since last time we pull
$ git push origin master //origin is the name of our remote repository
Common Workflow
$ git branch <branch_name> //create a branch for desired feature
$ git checkout <branch_name> //change to the branch
$ git status
$ git add -A
$ git commit -m "xxxxx"
//push branch to remote repository
$ git push -u origin <branch_name>
$ git branch -a
//merge a branch
$ git checkout master
$ git pull origin master //获取更新内容,别人有没有对该文件做过改动
$ git branch --merged //显示目前没有branch被merged到master上
$ git merged <branch_name> //把该<branch_name>merged到master上
$ git push origin master //push changes to the remote branch master
//delete a branch
$ git branch --merged //可以看到被merged到master的branch
$ git branch -d <branch_name> //该branch被deleted locally
$ git branch -a //该branch还存在于remote的repository
$ git push origin --delete <branch_name> //把该branch从remote上删除