git simple tutorial

转载自https://try.github.io

  • Initializing
    1
    git init

To initialize a Git repository locally, and local directory now has an empty repository in /.git/. The repository is a hidden directory where Git operates.

  • Checking the Status
    1
    git status

    To see what the current state of our project

  • Adding Changes
    1
    git add filename

    To add filename to the staging area

  • Adding All Changes
    1
    git add '*.txt'

    To add all the new files using a wildcard with git add.

  • Committing
    1
    git commit -m "Add cute octocat story"

    The files in the Staging Area are not in our repository yet. We could add or remove files from the stage before we store them in the repository.
    To store staged changes, run the commit command with a message describing what we’ve changed.

  • Committing All Changes
    1
    git commit -m 'Add all the txt files'
  • History
    1
    git log

    To browse all the commits to see what we changed. Git’s log remembers all the changes we’ve committed so far, in the order we committed them.

  • Remote Repositories
    1
    git remote add origin https://github.com/try-git/try_git.git

    To push our local repo to the GitHub server. We need first to create a new empty GitHub repository and then add a remote repository.
    This command takes a remote name and a repository URL, which in your case is https://github.com/try-git/try_git.git.

  • Pushing Remotely
    1
    git push -u origin master

    To push our local changes to our origin repo (on GitHub).
    The push command tells Git where to put our commits when we’re ready.
    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.

  • Pulling Remotely
    1
    git pull origin master

    To pull down any new changes on our GitHub repository.

  • Differences
    1
    git diff HEAD

    To 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.

  • Staged Differences
    1
    git add filename

    To look at changes within files that have already been staged. Remember, staged files are files we have told git that are ready to be committed.

  • Staged Differences (cont’d)
    1
    git diff --staged

    To see the changes you just staged, run git diff with the —staged option.

  • Resetting the Stage
    1
    git reset filename

    To remove(unstage) a file, use the git reset command. Notice that the file is still there. It’s just not staged anymore.

  • Undo
    1
    git checkout -- filename

    To change things back to how they were at the last commit by using the command: git checkout — .

  • Branching Out
    1
    git branch clean_up

    To create a branch called clean_up.
    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.

  • Switching Branches
    1
    git checkout clean_up

    To switch branches using the git checkout command.
    Now if you type git branch you’ll see two local branches: a main branch named master and your new branch named clean_up.

  • Removing All The Things
    1
    git rm '*.txt'

    To remove all the txt file in the clean_up branch, use 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.

  • Commiting Branch Changes
    1
    git commit -m "Remove all the txt file"

    To commit changes.

  • Switching Back to master
    1
    git checkout master
  • Preparing to Merge
    1
    git merge clean_up

    To merge your changes from the clean_up branch into the master branch.

  • Keeping Things Clean
    1
    git branch -d clean_up

    To delete a branch, use git branch -d command.

  • The Final Push
    1
    git push

    To push everything you’ve been working on to your remote repository.