YouTip LogoYouTip

Git Basic Operations

# Git Basic Operations The work of Git is to create and save snapshots of your project and compare them with subsequent snapshots. This chapter will introduce the commands for creating and committing your project snapshots. The commonly used Git commands are the following 6: **git clone**, **git push**, **git add**, **git commit**, **git checkout**, **git pull**. We will explain them in detail later. !(#) **Explanation:** * workspace: Working Directory * staging area: Staging Area / Cache * local repository: Repository or Local Repository * remote repository: Remote Repository A simple operation step: $ git init $ git add . $ git commit * git init - Initialize a repository. * git add . - Add files to the staging area. * git commit - Add the content of the staging area to the repository. ### Create Repository Commands The following table lists the commands for creating a Git repository: | Command | Description | | --- | --- | | `git init` | Initialize a repository | | `git clone` | Copy a remote repository, i.e., download a project. | * * * ## Commit and Modify The work of Git is to create and save snapshots of your project and compare them with subsequent snapshots. The following table lists the commands for creating and committing your project snapshots: | Command | Description | | --- | --- | | `git add` | Add files to the staging area | | `git status` | View the current state of the repository, showing changed files. | | `git diff` | Compare differences between files, i.e., the difference between the staging area and the working directory. | | `git difftool` | Use an external diff tool to view and compare file changes. | | `git range-diff` | Compare differences between two commit ranges. | | `git commit` | Commit the staging area to the local repository. | | `git reset` | Revert to a previous version. | | `git rm` | Remove files from the staging area and working directory. | | `git mv` | Move or rename files in the working directory. | | `git notes` | Add notes. | | `git checkout` | Switch branches. | | `git switch (Introduced in Git 2.23)` | Switch branches more clearly. | | `git restore (Introduced in Git 2.23)` | Restore or revert file changes. | | `git show` | Display detailed information about Git objects. | ### Commit Log | Command | Description | | --- | --- | | `git log` | View historical commit records | | ``` git blame ``` | View the historical modification records of a specified file in list form | | `git shortlog` | Generate a concise commit log summary | | `git describe` | Generate a readable string based on Git's tag system to describe the current commit | ### Remote Operations | Command | Description | | --- | --- | | `git remote` | Remote repository operations | | ``` git fetch ``` | Fetch code from the remote repository | | ``` git pull ``` | Download remote code and merge | | ``` git push ``` | Upload local code and merge | | ``` git submodule ``` | Manage projects containing other Git repositories | * * * ## Git File States The state of files in Git is divided into three categories: Working Directory, Staging Area, and Local Repository. Understanding these concepts and how they interact is key to mastering Git. ### Working Directory The working directory is the project files you see on your local computer. It is where you actually operate on files, including viewing, editing, deleting, and creating files. All changes to files first occur in the working directory. Files in the working directory may have the following states: * **Untracked**: Newly created files that have not been recorded by Git. * **Modified**: Files that have been tracked by Git have undergone changes, but these changes have not yet been committed to Git's records. ### Staging Area The staging area, also known as the Index, is a temporary storage area used to save changes that are about to be committed to the local repository. You can selectively add changes from the working directory to the staging area, allowing you to commit changes to multiple files at once without committing all file changes. * Use the `git add ` command to add files from the working directory to the staging area. * Use the `git add .` command to add all changes in the current directory to the staging area. git add # Add specified file to staging area git add . # Add all changes to staging area ### Local Repository The local repository is a database hidden in the `.git` directory that stores the entire commit history of the project. Each time you commit changes, Git saves the content of the staging area to the local repository. Use the `git commit -m "commit message"` command to commit changes from the staging area to the local repository. git commit -m "commit message" # Commit changes from staging area to local repository ### File State Transition Flow **Untracked**: Newly created files are initially untracked. They exist in the working directory but are not tracked by Git. touch newfile.txt # Create a new file git status # Check status, shows newfile.txt is untracked **Tracked**: After adding untracked files to the staging area using the `git add` command, the files become tracked. git add newfile.txt # Add file to staging area git status # Check status, shows newfile.txt is in staging area **Modified**: After making changes to tracked files, these changes will show as modified status, but these changes have not yet been added to the staging area. echo "Hello, World!" > newfile.txt # Modify file git status # Check status, shows newfile.txt is modified **Staged**: After using the `git add` command to add modified files to the staging area, the files enter the staged state, waiting to be committed. git add newfile.txt # Add file to staging area git status # Check status, shows newfile.txt is staged **Committed**: After using the `git commit` command to commit changes from the staging area to the local repository, these changes are recorded, and the file state returns to tracked. git commit -m "Added newfile.txt" # Commit changes git status # Check status, working directory is clean
← Python Multiply ListPython Count Occurrences Eleme β†’