To initialize a Git repository: git init
the repository status :git status
track new file: git add filename
To store our staged changes we run the commit command with a message describing what we've changed. Let's do that now by typing: git commit -m "Add cute octocat story"
we can add all the new files using a wildcard with git add. Don't forget the quotes! git add '*.txt'
browse git history: git log
remote on github : git remote add origin git@github.com:AnYuan/try_git.git
The push command tells Git where to put our commits when we're ready, and boy we're ready. So let's push our local changes to our origin repo (on GitHub).The name of our remote is origin and the default local branch name is master. The -u tells Git to remember the parameters, so that next time we can simply run git push and Git will know what to do. Go ahead and push it!
We've invited other people to our github project who have pulled your changes, made their own commits, and pushed them.We can check for changes on our GitHub repository and pull down any new changes by running: git pull origin master
Let's take a look at what is different from our last commit by using the git diff command.In this case we want the diff of our most recent commit, which we can refer to using the HEAD pointer.
: git diff HEAD
Another great use for diff is looking at changes within files that have already been staged. Remember, staged files are files we have told git that are ready to be committed.
:git add octofamily/octodog.txt
Good, now go ahead and run git diff with the --staged option to see the changes you just staged. You should see that octodog.txt was created.
: git diff --staged
Resetting the stage
So now that octodog is part of the family, octocat is all depressed. Since we love octocat more than octodog, we'll turn his frown around by removing octodog.txt.You can unstage files by using the git reset command. Go ahead and remove octofamily/octodog.txt.
:git reset octofamily/octodog.txt
Undo git reset did a 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 how things were before octodog came around and ruined the party.Files can be changed back to how they were at the last commit by using the command: git checkout -- <target>. Go ahead and get rid of all the changes since the last commit for octocat.txt
:git checkout --octocat.txt
Branching Out 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.We want to remove all these pesky octocats, so let's create a branch called clean_up, where we'll do all the work
:git branch clean_up
Switching Branches Great! Now if you type git branch you'll see two local branches: a main branch named master and your new branch namedclean_up.You can switch branches using the git checkout <branch>command. Try it now to switch to the clean_up branch
:git checkout clean_up
Removing All the things Ok, so you're in the clean_up branch. You can finally remove all those pesky octocats by using the git rm command which will not only remove the actual files from disk, but will also stage the removal of the files for us.
You're going to want to use a wildcard again to get all the octocats in one sweep, go ahead and run
:git rm '*.txt'
Preparing to Merge Alrighty, the moment has come when you have to merge your changes from the clean_up branch into the master branch. Take a deep breath, it's not that scary.
We're already on the master branch, so we just need to tell Git to merge the clean_up branch into it
:git merge clean_up
Remove branch Congratulations! You just accomplished your first successful bugfix and merge. All that's left to do is clean up after yourself. Since you're done with the clean_up branch you don't need it anymore.
You can use git branch -d <branch name> to delete a branch. Go ahead and delete the clean_up branch now
:git branch -d clean_up
The final push :git push