Git使用快速入门(15分钟)--网站模拟使用git
本模拟过程可以在网站上按着指导一步一步操作,网址在下面。记录下这个过程,是为了以后查看方便,不用从头操作这个过程。
Git模拟使用网址:https://try.github.io/levels/1/challenges/1
下面是正文,开始模拟使用git。
1.1 Got 15minutes and want to learn Git?
Git allowsgroups of people to work on the same documents (often code) at the same time,and without stepping on each other's toes. It's a distributed version controlsystem.
Our terminalprompt below is currently in directory we decided to name "octobox".To initialize a Git repository here, type the following command:
git init
1.2 Checkingthe Status
Good job! As Gitjust told us, our "octobox" directory now has an empty repository in /.git/.The repository is a hidden directory where Git operates.
To save yourprogress as you go through this tutorial -- and earn a badge when yousuccessfully complete it -- head over tocreate a freeCode School account. We'll wait for you here.
Next up, let'stype the git status command to see what the current state of ourproject is:
git status
1.3 Adding& Committing
I created a filecalled octocat.txt in the octobox repository for you (as you can seein the browser below).
You should runthe git status command again to see how the repository status haschanged:
git status
1.4 AddingChanges
Good, it lookslike our Git repository is working properly. Notice how Git says octocat.txt is"untracked"? That means Git sees that octocat.txt is a newfile.
To tell Git tostart tracking changes made to octocat.txt, we first need to add it to thestaging area by using git add.
git add octocat.txt
1.5 Checkingfor Changes
Good job! Git isnow tracking our octocat.txt file. Let's run git status againto see where we stand:
git status
1.6 Committing
Notice how Gitsays changes to be committed? The files listed here are in the StagingArea, and they are not in our repository yet. We could add or remove files fromthe stage before we store them in the repository.
To store ourstaged changes we run the commit command with a message describingwhat we've changed. Let's do that now by typing:
git commit -m "Add cute octocat story"
1.7 AddingAll Changes
Great! You alsocan use wildcards if you want to add many files of the same type. Notice thatI've added a bunch of .txt files into your directory below.
I put some in adirectory named "octofamily" and some others ended up in the root ofour "octobox" directory. Luckily, we can add all the new files usinga wildcard with git add. Don't forget the quotes!
git add '*.txt'
1.8 CommittingAll Changes
Okay, you'veadded all the text files to the staging area. Feel free to run git status tosee what you're about to commit.
If it looksgood, go ahead and run:
git commit -m 'Add all the octocat txt files'
1.9 History
So we've made afew commits. Now let's browse them to see what we changed.
Fortunately forus, there's git log. Think of Git's log as a journal that remembers allthe changes we've committed so far, in the order we committed them. Try runningit now:
git log
1.10 RemoteRepositories
Great job! We'vegone ahead and created a new empty GitHub repository for you to use with TryGit athttps://github.com/try-git/try_git.git. To push our local repo tothe GitHub server we'll need to add a remote repository.
This commandtakes a remote name and a repository URL, which inyour case is https://github.com/try-git/try_git.git.
Go ahead and run gitremote add with the options below:
git remote add origin https://github.com/try-git/try_git.git
1.11 PushingRemotely
The push commandtells Git where to put our commits when we're ready, and boy we're ready. Solet's push our local changes to our origin repo (on GitHub).
The name of ourremote is origin and the default local branch name is master.The -u tells Git to remember the parameters, so that next time we cansimply run git push and Git will know what to do. Go ahead and pushit!
git push -u origin master
1.12 PullingRemotely
Let's pretendsome time has passed. We've invited other people to our github project who havepulled your changes, made their own commits, and pushed them.
We can check forchanges on our GitHub repository and pull down any new changes by running:
git pull origin master
1.13 Differences
Uh oh, lookslike there has been some additions and changes to the octocat family. Let'stake a look at what is different from our last commit by using the gitdiff command.
In this case wewant the diff of our most recent commit, which we can refer to using the HEAD pointer.
git diff HEAD
1.14 StagedDifferences
Another greatuse for diff is looking at changes within files that have alreadybeen staged. Remember, staged files are files we have told git that are readyto be committed.
Let's use gitadd to stage octofamily/octodog.txt, which I just added to the familyfor you.
git add octofamily/octodog.txt
1.15 StagedDifferences (cont'd)
Good, now goahead and run git diff with the --staged option to see thechanges you just staged. You should see thatoctodog.txt was created.
git diff --staged
1.16 Resettingthe Stage
So now thatoctodog is part of the family, octocat is all depressed. Since we love octocatmore than octodog, we'll turn his frown around by removing octodog.txt.
You can unstagefiles by using the git reset command. Go ahead and remove octofamily/octodog.txt.
git reset octofamily/octodog.txt
1.17 Undo
git reset dida great job of unstaging octodog.txt, but you'll notice that he's still there.He's just not staged anymore. It would be great if we could go back to howthings were before octodog came around and ruined the party.
Files can bechanged back to how they were at the last commit by using the command: gitcheckout -- <target>. Go ahead and get rid of all the changes since thelast commit for octocat.txt
git checkout -- octocat.txt
1.18 BranchingOut
When developersare 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 theycan merge this branch back into their main master branch.
We want toremove all these pesky octocats, so let's create a branch called clean_up,where we'll do all the work:
git branch clean_up
1.19 SwitchingBranches
Great! Now ifyou type git branch you'll see two local branches: a main branchnamed master and your new branch namedclean_up.
You can switchbranches using the git checkout <branch> command. Try it now toswitch to the clean_up branch:
git checkout clean_up
1.20 RemovingAll The Things
Ok, so you're inthe clean_up branch. You can finally remove all those pesky octocatsby using the git rm command which will not only remove the actualfiles from disk, but will also stage the removal of the files for us.
You're going towant to use a wildcard again to get all the octocats in one sweep, go ahead andrun:
git rm '*.txt'
1.21 CommitingBranch Changes
Now that you'veremoved all the cats you'll need to commit your changes.
Feel free to run gitstatus to check the changes you're about to commit.
git commit -m "Remove all the cats"
1.22 SwitchingBack to master
Great, you'realmost finished with the cat... er the bug fix, you just need to switch back tothe master branch so you can copy (ormerge) your changes from the clean_up branchback into the master branch.
Go ahead andcheckout the master branch:
git checkout master
1.23 Preparingto Merge
Alrighty, themoment has come when you have to merge your changes from the clean_up branchinto the master branch. Take a deep breath, it's not that scary.
We're already onthe master branch, so we just need to tell Git to merge the clean_up branchinto it:
git merge clean_up
1.24 KeepingThings Clean
Congratulations!You just accomplished your first successful bugfix and merge. All that's leftto do is clean up after yourself. Since you're done with the clean_up branchyou don't need it anymore.
You can use gitbranch -d <branch name> to delete a branch. Go ahead and delete the clean_up branchnow:
git branch -d clean_up
1.25 The FinalPush
Here we are, atthe last step. I'm proud that you've made it this far, and it's been greatlearning Git with you. All that's left for you to do now is to push everythingyou've been working on to your remote repository, and you're done!
git push
到此为止,教程结束。
下面是操作过程中的命令记录,执行命令的情况。
上述所有操作的终端记录:
Press enter to submit commands
> git init
Initialized empty Git repository in /.git/
Success!
$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
Success!
$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will becommitted)
#
# octocat.txt
nothing added to commit but untracked files present (use "git add" totrack)
Success!
$ git add octocat.txt
Nice job, you've added octocat.txt to the Staging Area
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file:octocat.txt
#
Success!
$ git commit -m "Add cute octocat story"
[master (root-commit) 20b5ccd] Add cute octocat story
1 file changed, 1 insertion(+)
create mode 100644 octocat.txt
Success!
$ git add '*.txt'
Success!
$ git commit -m 'Add all the octocat txt files'
[master 3852b4d] Add all the octocat txt files
4 files changed, 4 insertions(+)
create mode 100644 blue_octocat.txt
create mode 100644 octofamily/baby_octocat.txt
create mode 100644 octofamily/momma_octocat.txt
create mode 100644 red_octocat.txt
Success!
$ git log
commit3852b4db1634463d0bb4d267edb7b3f9cd02ace1
Author: Try Git <try_git@github.com>
Date: Sat Oct 10 08:30:00 2020 -0500
Add all the octocat txt files
commitb652edfd888cd3d5e7fcb857d0dabc5a0fcb5e28
Author: Try Git <try_git@github.com>
Date: Sat Oct 10 08:30:00 2020 -0500
Added cute octocat story
Success!
$ git remote add origin https://github.com/try-git/try_git.git
Success!
$ git push -u origin master
Branch master set up to track remote branch master from origin.
Success!
$ git pull origin master
Updating 3852b4d..3e70b0f
Fast-forward
yellow_octocat.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 yellow_octocat.txt
Success!
$ git diff HEAD
diff --git a/octocat.txt b/octocat.txt
index 7d8d808..e725ef6 100644
--- a/octocat.txt
+++ b/octocat.txt
@@ -1 +1 @@
-A Tale of TwoOctocats
+[mA Tale of TwoOctocats and an Octodog
Success!
$ git add octofamily/octodog.txt
Success!
$ git diff --staged
diff --git a/octofamily/octodog.txt b/octofamily/octodog.txt
new file mode 100644
index 0000000..cfbc74a
--- /dev/null
+++ b/octofamily/octodog.txt
@@ -0,0 +1 @@
+[mwoof
Success!
$ git reset octofamily/octodog.txt
Success!
$ git checkout -- octocat.txt
Success!
$ git branch clean_up
Success!
$ git checkout clean_up
Switched to branch 'clean_up'
Success!
$ git rm '*.txt'
rm 'blue_octocat.txt'
rm 'octocat.txt'
rm 'octofamily/baby_octocat.txt'
rm 'octofamily/momma_octocat.txt'
rm 'red_octocat.txt'
Success!
$ git status
# On branch clean_up
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted:blue_octocat.txt
# deleted:octocat.txt
# deleted:octofamily/baby_octocat.txt
# deleted:octofamily/momma_octocat.txt
# deleted:red_octocat.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will becommitted)
#
# octofamily/
Did not use git commit
$ git commit -m "Remove all the cats"
[clean_up 63540fe] Remove all the cats
5 files changed, 5 deletions(-)
delete mode 100644 blue_octocat.txt
delete mode 100644 octocat.txt
delete mode 100644 octofamily/baby_octocat.txt
delete mode 100644 octofamily/momma_octocat.txt
delete mode 100644 red_octocat.txt
Success!
$ git checkout master
Switched to branch 'master'
Success!
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will becommitted)
#
# octofamily/.gitkeep
nothing added to commit but untracked files present (use "git add" totrack)
Use 'git merge' to merge the clean_up branch with master
$ git merge clean_up
Updating 3852b4d..ec6888b
Fast-forward
blue_octocat.txt | 1 -
octocat.txt | 1 -
octofamily/baby_octocat.txt | 1 -
octofamily/momma_octocat.txt | 1 -
red_octocat.txt | 1 -
5 files changed, 5 deletions(-)
delete mode 100644 blue_octocat.txt
delete mode 100644 octocat.txt
delete mode 100644 octofamily/baby_octocat.txt
delete mode 100644 octofamily/momma_octocat.txt
delete mode 100644 red_octocat.txt
Success!
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will becommitted)
#
# octofamily/
nothing added to commit but untracked files present (use "git add" totrack)
Use 'git branch -d' to delete the clean_up branch
$ git branch -d clean_up
Deleted branch clean_up (was ec6888b).
Success!
$ git push
To https://github.com/try-git/try_git.git
3e70b0f..99c2fd7 master -> master
Success!
>
Advice
Learning more about Git
We only scratched the surface of Git inthis course. There is so much more you can do with it. Check out the Git documentation fora full list of functions.
The Pro Git book,by Scott Chacon, is an excellent resource to teach you the inner workings ofGit.
help.github andGitHub Training are also great for anything related to Git in general and usingGit with GitHub.