Git Study Notes

Git is a powerful distributed version control system for tracking and managing source code changes during software development. By tracking different file states, such as Untracked, Unmodify, Modified, and Staged, it helps developers manage code versions effectively. Git offers a rich set of commands for initializing repositories, committing code, pushing to remote repositories, switching branches, and managing tags, greatly simplifying collaborative development and code management.

Contents

Git

Git, a version control tool for collaborative development

File States

  • Untracked: Not tracked. This file is in the folder but has not been added to the Git repository, so it is not under version control. Use git add to change its state to Staged.

  • Unmodify: The file has been added to the repository and has not been modified; its snapshot in the repository is identical to the file in the folder. Files in this state can go in either of two directions: if modified, they become Modified. If removed from the repository using git rm, they become Untracked files.

  • Modified: The file has been modified, and no other operations have been performed. This file can also go in either of two directions: use git add to move it to the staging staged state, or use git checkout to discard the modifications and return it to the unmodify state. This git checkout retrieves the file from the repository and overwrites the current modifications.

  • Staged: Staging state. Run git commit to sync the changes to the repository. The file in the repository and the local file will then be identical again, and the file will be in the Unmodify state. Run git reset HEAD filename to unstage the changes; the file will be in the Modified state.

Common Commands

  • git init Initialize a Git repository

  • git log View the commit log

  • git remote View remote repository configuration

  • git remote add <remote repository name> <remote repository url> Add a remote repository

  • git config --global user.name "username" Set the Git username on this machine

  • git config --global user.email "email" Set the Git email address on this machine

  • git status View file status

  • git add <files> Add files to the staging area

  • git commit -m "message" Commit the files in the staging area to the local repository

  • git push <remote > <branch> Sync local commits to the remote repository

    • Optional parameter -u (--up-stream)Set the remote repository and branch used for this push as the default; subsequent pushes can omit the push destination
    • Optional parameter -f Force the push, ignoring conflicts
    • --tags Push tags
    • --delete origin tag <tag name> Delete a tag from the remote repository
  • git reset Unstage files

    • -- hard <提交的哈希ID> Restore files to the state of a particular commit
  • git pull Sync changes from the remote repository to the local repository

  • git checkout Switch branches

  • git branch <branch name> Clone the current branch to create a new branch

  • git tag List tags

    • -l <标签关键字> Search for a target tag
    • <tag name> -m "message" tag-name Create a tag
    • -d <tag name> Delete a tag
  • git show View details of the current commit