首页 > 代码库 > git 笔记
git 笔记
Step: Configure Git
Once it is installed, open terminal (aka Bash, aka Shell, aka Prompt). You can verify that it‘s really there by typing:
$ git --version
This will return the version of Git that you‘re running and look something like this:
git version 1.9.1
Next, configure Git so it knows who to associate your changes to:
Set your name:
$ git config --global user.name "<Your Name>"
Now set your email:
$ git config --global user.email <youremail@example.com>
----------------------------------------
Step: Create a Repository
You‘re going to create a new folder and initialize it as a Git repository.
To make things easier, name your folder what you‘d name the project. How about ‘hello-world‘.
You can type these commands one at a time into your terminal window.
To make a new folder:
$ mkdir hello-world
To go into that folder:
$ cd hello-world
To create a new Git instance for a project:
$ git init
That‘s it! It will just return you to a new line. If you want to be double-sure that it‘s a Git repository, type git status and if it doesn‘t return ‘fatal: Not a git repository...‘, you‘re golden!
Tips
Make a new folder (aka directory)
$ mkdir <FOLDERNAME>
Navigate into an existing folder (aka change directory)
$ cd <FOLDERNAME>
List the items in a folder
$ ls
Turn Git on for a folder
$ git init
----------------------------------------------
Step: Status, Add and Commit Changes
Next check the status of your repository to find out if there have been changes. Below in this terminal, you should still be within the ‘hello-world‘ you created. See if there are changes listed:
$ git status
Then add the file you just created to the files you‘d like to commit (aka save) to change.
$ git add readme.txt
Finally, commit those changes to the repository‘s history with a short description of the updates.
$ git commit -m "<your commit message>"
Step: Make More Changes
Now add another line to readme.txt and save.
In terminal, you can view the difference between the file now and how it was at your last commit.
$ git diff
Tips
Check status of changes to a repository
$ git status
View changes to files
$ git diff
Add a file‘s changes to be committed
$ git add <FILENAME>
To add all files changes
$ git add .
To commit (aka save) the changes you‘ve added with a short message describing the changes
$ git commit -m "<your commit message>"
----------------------------------------------
Tips
Add remote connections
$ git remote add <REMOTENAME>
Set a URL to a remote
$ git remote set-url <REMOTENAME>
Pull in changes
$ git pull <REMOTENAME> <BRANCHNAME>
View remote connections
$ git remote -v
Push changes
$ git push <REMOTENAME>
----------------------------------------------
$ git clone <URLFROMGITHUB>
$ git remote add upstream https://github.com/jlord/patchwork.git
Tips
Add remote connections
$ git remote add <REMOTENAME> <URL>
View remote connections
$ git remote -v
----------------------------------------------
Step: Create a branch
When you create a branch, Git copies everything from the current branch you‘re on and places it in the branch you‘ve requested.
Type git status to see what branch you‘re currently on (it should be ‘gh-pages‘).
Create a branch and name it "add-<username>", where ‘username‘ is your username. For instance, "add-jlord". Branches are case-sensitive so name your branch exactly the way your GitHub name appears.
$ git branch <BRANCHNAME>
Now you have a branch with a new name identical to ‘gh-pages‘.
To go into that branch and work on it, similar to using cd to change directory in terminal, you checkout a branch. Go onto your new branch:
$ git checkout <BRANCHNAME>
Step: Check-in
Go through the steps for checking in a project:
$ git status
$ git add <FILENAME>
$ git commit -m "<commit message>"
Now push your update to your fork on GitHub:
$ git push origin <BRANCHNAME>
Tips
You can create and switch to a branch in one line:
$ git checkout -b <BRANCHNAME>
Create a new branch:
$ git branch <BRANCHNAME>
Move onto a branch:
$ git checkout <BRANCHNAME>
List the branches:
$ git branch
Rename a branch you‘re currently on:
$ git branch -m <NEWBRANCHNAME>
Verify what branch you‘re working on
$ git status
----------------------------------------------
Tips
Pull in changes from a remote
$ git pull <REMOTENAME> <BRANCH>
Copy a repository to your computer
$ git clone <URL>
Add remote connections
$ git remote add <REMOTENAME> <URL>
View remote connections
$ git remote -v
----------------------------------------------
$ git pull <REMOTENAME> <BRANCHNAME>
TIPS
Check Git status
$ git status
Pull in changes from a remote branch
$ git pull
See changes to the remote before you pull in
$ git fetch --dry-run
----------------------------------------------
Step: Merge Locally
Your pull request has been merged! Now, since you know that you definitely want those updates in your forked version, and your branch is in good working order, merge it into the main branch on your forked repository, in this case, ‘gh-pages‘.
First, move into the branch you want to merge into — in this case, branch ‘gh-pages‘.
$ git checkout gh-pages
Now tell Git what branch you want to merge in — in this case, your feature branch that begins with "add-".
$ git merge <BRANCHNAME>
Tidy up by deleting your feature branch now that it has been merged.
$ git branch -D <BRANCHNAME>
You can also delete the branch from your fork on GitHub:
$ git push <REMOTENAME> --delete <BRANCHNAME>
Step: Pull from Upstream
And last but not least, if you pull in updates from the original (since it now shows you on the home page) you‘ll be up to date and have a version too, live at: yourusername.github.io/patchwork.
To pull from the original upstream:
$ git pull upstream gh-pages
Tips
Merge a branch into current branch
$ git merge <BRANCHNAME>
Change the branch you‘re working on
$ git checkout <BRANCHNAME>
Delete a local branch
$ git branch -D <BRANCHNAME>
Delete a remote branch
$ git push <REMOTENAME> --delete <BRANCHNAME>
Pull from a remote branch
$ git pull <REMOTENAME> <BRANCHNAME>
mkdir apc-git-test
cd apc-git-test
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://git.oschina.net/RoRBrother/apc-git-test.git
git push -u origin master
git 笔记