title: Git 基础命令与相关术语 date: 2015-11-23 03:01:30 categories: - 自用笔记 - 术业专攻 tags: - Git permalink: git-command note: 1 ---   笔记基本上整理自网站 [Try Git](https://try.github.io),一套很不错的 Git 在线入门教程。 ## git init To initialize a Git repository here ### Directory - A folder used for storing multiple files. ### Repository - A directory where Git has been initialized to start version controlling your files. ## git status See what the current state of the project is. ### staged - Files are ready to be committed. ### unstaged - Files with changes that have not been prepared to be committed. ### untracked - Files aren't tracked by Git yet. This usually indicates a newly created file. ### deleted - File has been deleted and is waiting to be removed from Git. ## git add `` Add files to the staging area. ### Staging Area - A place where we can group files together before we "commit" them to Git. Commit ### add all - You can also type `git add -A` . where the dot stands for the current directory, so everything in and beneath it is added. The `-A` ensures even file deletions are included. ## git reset You can use `git reset ` to remove a file or files from the staging area. ## Commit A "commit" is a snapshot of our repository. This way if we ever need to look back at the changes we've made (or if someone else does), we will see a nice timeline of all changes. ### git commit -m "msg" - To store our staged changes we run the commit command with a message describing what we've changed. ### '-a' option - Auto removes deleted files with the commit. ``` git commit -am "msg" ``` ## git log a journal that remembers all the changes we've committed so far ### git log --summary - see more information for each commit. You can see where new files were added for the first time or where files were deleted. It's a good overview of what's going on in the project. ## git remote ``` git remote add git remote add origin https://github.com/try-git/try_git.git ``` Git doesn't care what you name your remotes, but it's typical to name your main one `origin`. It's also a good idea for your main repository to be on a remote server like GitHub in case your machine is lost at sea during a transatlantic boat cruise or crushed by three monkey statues during an earthquake. ## git push Tells Git where to put our commits when we're ready. ``` git push -u git push -u origin master ``` The `-u` tells Git to remember the parameters, so that next time we can simply run `git push`. ## git stash Sometimes when you go to pull you may have changes you don't want to commit just yet. One option you have, other than commiting, is to stash the changes. Use the command `git stash` to stash your changes, and `git stash apply` to re-apply your changes after your pull. ## git pull ``` git push git pull origin master ``` Check for changes on remote repository and pull down any new changes. ## git diff A good overview of changes we have made and lets us add files or directories one at a time and commit them separately. ### git diff HEAD - Show what is different from our last commit. `HEAD` points to your most recent commit by default. ### git diff --staged - Look at changes within files that have already been staged. ## git checkout -- `` Changed files back to how they were at the last commit, namely get rid of all the changes since the last commit. `--`: promise the command line that there are no more options after the '--', avoid switching to the branch of the same name. ## git branch List local branches of now repository. ### git branch `` - Create a new branch. > When developers are working on a feature or bug they'll often create a copy (aka. branch) of their code they can make separate commits to. Then when they're done they can merge this branch back into their main master branch. ### git checkout `` - Switch to certain branch. ### git checkout -b new_branch - Checkout and create a branch at the same time. ## git rm `` Not only remove the actual files from disk, but will also stage the removal of the files for us. ### git rm -r folder - Recursively remove all folders and files from the given directory. ## git merge `` Merge your changes from the given branch into current branch. ### Merge Conflicts - Merge Conflicts can occur when changes are made to a file at the same time. Reading more on [how conflicts are presented](http://git-scm.com/docs/git-merge#_how_conflicts_are_presented). ## Delete Branch ### git branch -d `` - Delete a local branch. `-d` won't let you delete something that hasn't been merged. ### `-f` and `-D` - Force delete the branch that hasn't been merged. > Add the `-f`(--force) option or use `-D` which combines `-d -f` together into one command. ### Delete Remote Branch ``` git branch -r -d / git push : ``` ``` git branch -r -d origin/branch-name git push origin :branch-name ```