自用笔记:本文属于自用笔记,不做详解,仅供参考。
笔记基本上整理自网站 Try Git,一套很不错的 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 <file>
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 <filename>
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.
1 | 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
1 | git remote add <remote name> <repository URL> |
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.
1 | git push -u <remote name> <branch name> |
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
1 | git push <remote name> <branch name> |
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 – <target>
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 <name>
- 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 <branch>
- Switch to certain branch.
git checkout -b new_branch
- Checkout and create a branch at the same time.
git rm <file>
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 <branch>
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.
Delete Branch
git branch -d <branch>
- 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
1 | git branch -r -d <remote name>/<branch name> |
1 | git branch -r -d origin/branch-name |