### Git Remote Management Examples Source: https://github.com/git/git-reference/blob/gh-pages/zh/remotes/index.html Demonstrates the usage of `git remote` commands to list and add remote repositories, showing the aliases and their corresponding URLs. ```git git remote origin ``` ```git git remote -v origin git@github.com:github/git-reference.git (fetch) origin git@github.com:github/git-reference.git (push) ``` ```git git remote add github git@github.com:schacon/hw.git git remote -v github git@github.com:schacon/hw.git (fetch) github git@github.com:schacon/hw.git (push) ``` -------------------------------- ### Git Project Creation Commands Source: https://github.com/git/git-reference/blob/gh-pages/_layouts/zh_reference.html This section covers the fundamental Git commands for initializing new projects and cloning existing ones. It provides an overview of how to start a new repository or obtain a copy of an existing one. ```APIDOC init Initialize an empty Git repository or reinitialize an existing one. clone Clone a repository into a new directory. ``` -------------------------------- ### Git Commit Example Source: https://github.com/git/git-reference/blob/gh-pages/zh/basic/index.html Demonstrates a typical Git commit process, including staging changes and committing them with a message. It also shows the output of 'git status' when no changes are staged. ```bash no changes added to commit (use "git add" and/or "git commit -a") **$ git commit -am 'changes to hello file'** \[master 78b2670\] changes to hello file 1 files changed, 2 insertions(+), 1 deletions(-) ``` -------------------------------- ### Initialize a New Git Repository Source: https://github.com/git/git-reference/blob/gh-pages/creating/index.html Initializes an existing directory as a new Git repository. This creates a hidden `.git` subdirectory where Git stores all project data and history. It's a local operation that can be performed on any directory. ```git git init ``` -------------------------------- ### Clone an Existing Git Repository Source: https://github.com/git/git-reference/blob/gh-pages/creating/index.html Creates a local copy of an existing Git repository from a remote URL. This allows you to view, modify, or contribute to the project. By default, it creates a directory with the same name as the repository. ```git git clone git://github.com/schacon/simplegit.git ``` -------------------------------- ### Viewing Git Commit History Source: https://github.com/git/git-reference/blob/gh-pages/zh/branching/index.html Shows how to use the `git log` command to view the commit history of a project. It includes examples of the basic `git log`, a concise `--oneline` view, and a `--graph` view to visualize branching and merging. ```git git log # commit 8d585ea6faf99facd39b55d6f6a3b3f481ad0d3d # Merge: 3cbb6aa 3ac015d # Author: Scott Chacon # Date: Fri Jun 4 12:59:47 2010 +0200 # # Merge branch 'fix_readme' # # Conflicts: # README # # commit 3cbb6aae5c0cbd711c098e113ae436801371c95e # Author: Scott Chacon # Date: Fri Jun 4 12:58:53 2010 +0200 # # fixed readme title differently # ... git log --oneline # 8d585ea Merge branch 'fix_readme' # 3cbb6aa fixed readme title differently # 3ac015d fixed readme title # 558151a Merge branch 'change_class' # ... git log --oneline --graph # * 8d585ea Merge branch 'fix_readme' # |\ # | * 3ac015d fixed readme title # * | 3cbb6aa fixed readme title differently # |/ # * 558151a Merge branch 'change_class' # |\ # | * 3467b0a changed the class name # * | b7ae93b added from ruby # |/ # * 17f4acf first commit ``` -------------------------------- ### Git Status Unstage Hint Source: https://github.com/git/git-reference/blob/gh-pages/zh/basic/index.html Illustrates the helpful message provided by 'git status' when files are staged, guiding the user on how to unstage them. ```bash **$ git status** # On branch master # Changes to be committed: # (use "git reset HEAD ..." to unstage) # # modified: README # modified: hello.rb # ``` -------------------------------- ### Git Branching and Merging Example Source: https://github.com/git/git-reference/blob/gh-pages/branching/index.html This snippet illustrates a common Git workflow involving creating a new branch, making changes (renaming a class), committing those changes, switching back to the master branch, renaming a file and modifying its content, committing again, and finally merging the feature branch back into master. It highlights Git's ability to automatically handle file renames during merges. ```git git branch # Expected output: * master cat hello.rb # Expected output: class HelloWorld # def self.hello # puts "Hello World" # end # end # HelloWorld.hello git checkout -b change_class # Expected output: Switched to a new branch 'change_class' vim hello.rb # (Modify 'HelloWorld' to 'HiWorld') head -1 hello.rb # Expected output: class HiWorld git commit -am 'changed the class name' # Expected output: [change_class 3467b0a] changed the class name # 1 files changed, 2 insertions(+), 4 deletions(-) git checkout master # Expected output: Switched to branch 'master' git mv hello.rb ruby.rb vim ruby.rb # (Modify 'puts "Hello World"' to 'puts "Hello World from Ruby"') git diff # Expected output: diff --git a/ruby.rb b/ruby.rb # index 2aabb6e..bf64b17 100644 # --- a/ruby.rb # +++ b/ruby.rb # @@ -1,7 +1,7 @@ # class HelloWorld # # def self.hello # - puts "Hello World" # + puts "Hello World from Ruby" # end # # end git commit -am 'added from ruby' # Expected output: [master b7ae93b] added from ruby # 1 files changed, 1 insertions(+), 1 deletions(-) # rename hello.rb => ruby.rb (65%) git branch # Expected output: change_class # * master git merge change_class # Expected output: Renaming hello.rb => ruby.rb # Auto-merging ruby.rb # Merge made by recursive. # ruby.rb | 6 ++---- # 1 files changed, 2 insertions(+), 4 deletions(-) cat ruby.rb # Expected output: class HiWorld # def self.hello # puts "Hello World from Ruby" # end # end # # HiWorld.hello ``` -------------------------------- ### Git Alias for Unstaging Source: https://github.com/git/git-reference/blob/gh-pages/basic/index.html You can create a Git alias for `git reset HEAD` to simplify the unstaging process. For example, aliasing it to `unstage` allows you to run `git unstage `. ```git git config --global alias.unstage "reset HEAD" ``` -------------------------------- ### 初始化新的 Git 仓库 Source: https://github.com/git/git-reference/blob/gh-pages/zh/creating/index.html 使用 `git init` 命令在当前目录初始化一个新的 Git 仓库。这会在项目根目录下创建一个 `.git` 子目录,用于存储所有版本控制相关的数据。此操作是完全本地化的。 ```bash cd your_project_directory git init ``` -------------------------------- ### Git Project Initialization and Cloning Source: https://github.com/git/git-reference/blob/gh-pages/_layouts/reference.html Commands for creating new Git repositories and duplicating existing ones. 'init' creates a new repository, while 'clone' copies a remote repository to your local machine. ```git git init git clone ``` -------------------------------- ### Basic Project Snapshotting Source: https://github.com/git/git-reference/blob/gh-pages/index.html Demonstrates how to create a backup of a project directory by copying it, simulating a basic version control snapshot. ```bash $ cp -R project project.bak $ cp -R project project.2010-06-01.bak ``` -------------------------------- ### 克隆现有的 Git 仓库 Source: https://github.com/git/git-reference/blob/gh-pages/zh/creating/index.html 使用 `git clone` 命令复制一个远程 Git 仓库到本地。这会下载项目的全部历史记录和主分支,让你可以在本地查看、修改或贡献代码。你可以指定一个不同的本地目录名。 ```bash git clone # 或者指定一个不同的本地目录名: git clone ``` -------------------------------- ### Git Basic Snapshot Commands Source: https://github.com/git/git-reference/blob/gh-pages/_layouts/zh_reference.html This section covers essential Git commands for managing basic snapshots of your project. It includes commands for staging changes, viewing differences, committing changes, resetting states, and removing or moving files. ```APIDOC add Stages changes for the next commit. status Shows the working tree status. diff Shows project differences. commit Records changes to the repository. reset Resets current HEAD to the specified state. rm, mv Removes or renames files. ``` -------------------------------- ### Diff Since Merge Base Source: https://github.com/git/git-reference/blob/gh-pages/inspect/index.html Compares changes between two branches starting from their common ancestor. It uses `git merge-base` to find the ancestor and then `git diff` to show the differences, with `--stat` providing a summary of changes. ```git git diff --stat $(git merge-base master erlang) erlang ``` -------------------------------- ### Filter Git Log by Author Source: https://github.com/git/git-reference/blob/gh-pages/inspect/index.html Filters the Git commit history to show only commits made by a specific author. The search is case-sensitive and also matches the author's email address. This example limits the output to the last 5 commits. ```git git log --author=Linus --oneline -5 ``` -------------------------------- ### Git 快照创建示例 Source: https://github.com/git/git-reference/blob/gh-pages/zh/index.html 展示了如何通过手动复制项目目录来创建项目快照,以及如何生成和应用补丁文件进行协作。这是 Git 核心思想的早期实现方式。 ```bash $ cp -R project project.bak ``` ```bash $ cp -R project project.2010-06-01.bak ``` ```bash $ wget http://example.com/project.2010-06-01.zip $ unzip project.2010-06-01.zip $ cp -R project.2010-06-01 project-my-copy $ cd project-my-copy $ (做了某些修改) $ diff project-my-copy project.2010-06-01 > change.patch $ (通过E-mail发送修改补丁) ``` -------------------------------- ### Filter Git Log by Date Range Source: https://github.com/git/git-reference/blob/gh-pages/inspect/index.html Filters the Git commit history by a specified date range using `--before` and `--after` options. It also uses `--no-merges` to exclude merge commits from the results. This example shows commits before three weeks ago but after April 18th, 2010. ```git git log --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges ``` -------------------------------- ### Git Add: Staging Files Source: https://github.com/git/git-reference/blob/gh-pages/basic/index.html The `git add` command stages new and modified files, preparing them for the next commit. It can add specific files, all files in the current directory (`.`), or use wildcards. The staging area allows for precise snapshot composition. ```git git add README hello.rb git add . git add * ``` -------------------------------- ### Sharing Changes via Patches Source: https://github.com/git/git-reference/blob/gh-pages/index.html Illustrates the process of sharing changes between project copies using zip archives and diff patches, a precursor to modern version control. ```bash $ wget http://example.com/project.2010-06-01.zip $ unzip project.2010-06-01.zip $ cp -R project.2010-06-01 project-my-copy $ cd project-my-copy $ (change something) $ diff project-my-copy project.2010-06-01 > change.patch $ (email change.patch) ``` -------------------------------- ### Git Status: Viewing Project State Source: https://github.com/git/git-reference/blob/gh-pages/basic/index.html The `git status` command displays the state of your working directory and staging area. The `-s` option provides a concise, short output, while the default output offers more detailed information and hints for further actions. ```git git status -s # Output: # AM README # A hello.rb git status # Output: # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached ..." to unstage) # # new file: README # new file: hello.rb # ``` -------------------------------- ### Git Sharing and Updating Commands Source: https://github.com/git/git-reference/blob/gh-pages/_layouts/zh_reference.html This section outlines Git commands for sharing your project with others and updating your local repository with changes from remote repositories. It includes commands for fetching, pulling, pushing, and managing remote connections. ```APIDOC fetch, pull Download objects and refs from another repository. push Update remote refs along with associated objects. remote Manage set of tracked repositories. ``` -------------------------------- ### Managing Git Remotes Source: https://github.com/git/git-reference/blob/gh-pages/zh/remotes/index.html Commands for adding, listing, and removing remote repositories in Git. ```git git remote -v # Lists all remote repositories and their URLs. git remote add origin git://github.com/pjhyett/hw.git # Adds a new remote repository named 'origin' with the specified URL. git remote rm origin # Removes the remote repository named 'origin'. ``` -------------------------------- ### Git Commit Message Best Practices Source: https://github.com/git/git-reference/blob/gh-pages/basic/index.html Illustrates the recommended format for Git commit messages, including a short summary line and a more detailed body, separated by a blank line. ```git Short (50 chars or less) summary of changes More detailed explanatory text, if necessary. Wrap it to about 72 characters or so. In some contexts, the first line is treated as the subject of an email and the rest of the text as the body. The blank line separating the summary from the body is critical (unless you omit the body entirely); some git tools can get confused if you run the two together. Further paragraphs come after blank lines. - Bullet points are okay, too - Typically a hyphen or asterisk is used for the bullet, preceded by a single space, with blank lines in between, but conventions vary here ``` -------------------------------- ### Git Basic Snapshotting Commands Source: https://github.com/git/git-reference/blob/gh-pages/_layouts/reference.html Core commands for managing changes within a Git repository. This includes staging changes ('add'), checking status ('status'), viewing differences ('diff'), committing changes ('commit'), undoing changes ('reset'), removing/moving files ('rm', 'mv'), and temporarily saving changes ('stash'). ```git git add git status git diff git commit -m "" git reset git rm git mv git stash ``` -------------------------------- ### Creating and Committing on a New Branch Source: https://github.com/git/git-reference/blob/gh-pages/zh/branching/index.html Illustrates the process of creating a new branch, making changes to a file, staging it, and committing the changes on that new branch. ```git git checkout -b erlang vim erlang_hw.erl git add erlang_hw.erl git commit -m 'added erlang' vim haskell.hs git add haskell.hs git commit -m 'added haskell' ``` -------------------------------- ### Committing Changes with a Message Source: https://github.com/git/git-reference/blob/gh-pages/zh/basic/index.html Stages changes using `git add` and then commits them with a descriptive message using the `-m` option. This creates a snapshot of the staged content. ```git git add hello.rb git commit -m 'my hola mundo changes' ``` -------------------------------- ### Listing Remote Repositories Source: https://github.com/git/git-reference/blob/gh-pages/zh/remotes/index.html Lists the aliases for remote repositories configured for the current Git project. The `-v` flag shows the URLs associated with each alias. ```git git remote ``` ```git git remote -v ``` -------------------------------- ### Git Branch Management Commands Source: https://github.com/git/git-reference/blob/gh-pages/branching/index.html This section details essential Git commands for managing branches, including listing, creating, and switching between them. It explains how branches represent different contexts and how Git updates the working directory when switching branches. ```git git branch # Lists local branches. The current branch is marked with an asterisk and often shown in green if coloring is enabled. git branch (branchname) # Creates a new branch named '(branchname)' at the current commit. git checkout (branchname) # Switches the working directory to the specified branch. git branch -v # Lists local branches along with the last commit on each branch. ``` -------------------------------- ### Git Reset 简述 Source: https://github.com/git/git-reference/blob/gh-pages/zh/basic/index.html 简要说明了 `git reset HEAD` 命令用于取消之前 `git add` 添加但又不希望包含在下一提交快照中的缓存。 ```bash 简而言之,执行 `git reset HEAD` 以取消之前 `git add` 添加,但不希望包含在下一提交快照中的缓存。 ``` -------------------------------- ### Git Inspect and Compare Commands Source: https://github.com/git/git-reference/blob/gh-pages/_layouts/zh_reference.html This section focuses on Git commands used for inspecting the project's history and comparing different versions of files. It includes commands for viewing commit logs and differences between various states of the project. ```APIDOC log Shows commit logs. diff Shows project differences. ``` -------------------------------- ### Git 分支管理命令 Source: https://github.com/git/git-reference/blob/gh-pages/zh/branching/index.html 此部分涵盖了 Git 管理分支的基础命令。它解释了如何列出现有分支、创建新分支、切换分支以及删除不再需要或已合并的分支。 ```git git branch # 列出所有本地分支。当前分支会用星号标记。 git branch <分支名> # 创建一个名为 <分支名> 的新分支。 git checkout <分支名> # 切换到指定的分支。 git checkout -b <分支名> # 创建一个新分支并立即切换到该分支。 git branch -d <分支名> # 删除指定的分支。使用 -D 强制删除。 ``` -------------------------------- ### Git RM 简述 Source: https://github.com/git/git-reference/blob/gh-pages/zh/basic/index.html 简要说明了 `git rm` 命令用于删除 Git 追踪的文件,并且该命令还会删除工作目录中的相应文件。 ```bash 简而言之, 执行 `git rm` 来删除 Git 追踪的文件。它还会删除你的工作目录中的相应文件。 ``` -------------------------------- ### Fetching and Pulling from Remotes Source: https://github.com/git/git-reference/blob/gh-pages/zh/remotes/index.html Commands for synchronizing local repositories with remote ones, including fetching data and merging changes. ```git git fetch github # Fetches all new data from the remote repository named 'github'. # This updates remote-tracking branches like 'github/master'. git merge github/master # Merges the fetched 'github/master' branch into the current local branch. git fetch --all # Fetches data from all configured remote repositories. git pull # Fetches data from the default remote and merges it into the current branch (equivalent to git fetch + git merge). ``` -------------------------------- ### Git Remote Commands Source: https://github.com/git/git-reference/blob/gh-pages/remotes/index.html This section details essential Git commands for managing remote repositories. It covers listing remote aliases, adding new remote repositories, and understanding the default 'origin' remote. It also explains how to view the URLs associated with these remotes. ```git git remote list your remote aliases Without any arguments, Git will simply show you the remote repository aliases that it has stored. By default, if you cloned the project (as opposed to creating a new one locally), Git will automatically add the URL of the repository that you cloned from under the name 'origin'. If you run the command with the -v option, you can see the actual URL for each alias. $ git remote origin $ git remote -v origin git@github.com:github/git-reference.git (fetch) origin git@github.com:github/git-reference.git (push) You see the URL there twice because Git allows you to have different push and fetch URLs for each remote in case you want to use different protocols for reads and writes. ``` ```git git remote add add a new remote repository of your project If you want to share a locally created repository, or you want to take contributions from someone else's repository - if you want to interact in any way with a new repository, it's generally easiest to add it as a remote. You do that by running `git remote add [alias] [url]`. That adds `[url]` under a local remote named `[alias]`. For example, if we want to share our Hello World program with the world, we can create a new repository on a server (Using GitHub as an example), which should give you a URL, in this case "git@github.com:schacon/hw.git". To add that to our project so we can push to it and fetch updates from it we would do this: $ git remote $ git remote add github git@github.com:schacon/hw.git $ git remote -v github git@github.com:schacon/hw.git (fetch) github git@github.com:schacon/hw.git (push) Like the branch naming, remote alias names are arbitrary - just as 'master' has no special meaning but is widely used because `git init` sets it up by default, 'origin' is often used as a remote name because `git clone` sets it up by default as the cloned-from URL. In this case we'll name the remote 'github', but you could name it just about anything. ``` -------------------------------- ### Git Branching and Merging Commands Source: https://github.com/git/git-reference/blob/gh-pages/_layouts/zh_reference.html This section details Git commands used for managing branches and merging changes. It covers creating, switching, and listing branches, as well as merging different lines of development and viewing commit history. ```APIDOC branch List, create, or delete branches. checkout Switch branches or restore working tree files. merge Join two or more development histories together. log Shows commit logs. tag List, create, delete or verify a tag object. ``` -------------------------------- ### Git Commit with Message Option Source: https://github.com/git/git-reference/blob/gh-pages/basic/index.html Records a snapshot of the staging area with a commit message provided directly on the command line using the -m option. ```shell git add hello.rb git commit -m 'my hola mundo changes' ``` -------------------------------- ### Git Push Command Reference Source: https://github.com/git/git-reference/blob/gh-pages/remotes/index.html Provides the basic syntax for the `git push` command, explaining its purpose in sharing local commits and branches with a remote repository. It also references external documentation for more details. ```APIDOC git push [alias] [branch] Purpose: Pushes your local branches and commits to a specified remote repository. Parameters: [alias]: The name of the remote repository (e.g., 'origin', 'github'). [branch]: The local branch to push. Notes: - If the branch does not exist on the remote, it will be created. - If the branch exists, Git attempts to update it. - Pushes are rejected if the remote branch has diverged from the local branch (non-fast-forward). Related Commands: git fetch: Retrieves data from a remote repository. git merge: Integrates changes from one branch into another. Documentation: http://git-scm.com/docs/git-push http://git-scm.com/book/en/Git-Basics-Working-with-Remotes#Pushing-to-Your-Remotes ``` -------------------------------- ### Git Sharing and Updating Projects Source: https://github.com/git/git-reference/blob/gh-pages/_layouts/reference.html Commands for interacting with remote repositories. 'remote' manages remote connections. 'fetch' downloads commits and objects from a remote repository. 'pull' fetches and integrates changes. 'push' uploads local commits to a remote repository. ```git git remote add git fetch git pull git push ``` -------------------------------- ### Git Configuration for User Information Source: https://github.com/git/git-reference/blob/gh-pages/basic/index.html Configures the global user name and email address for Git commits. This information is recorded with each commit. ```shell git config --global user.name 'Your Name' git config --global user.email you@somedomain.com ``` -------------------------------- ### Git Add Command Source: https://github.com/git/git-reference/blob/gh-pages/zh/basic/index.html The `git add` command stages changes in the working directory, preparing them to be included in the next commit. It can add new files, modified files, or all changes in a directory. ```git git add README hello.rb git add . git add * ``` -------------------------------- ### Git Inspection and Comparison Source: https://github.com/git/git-reference/blob/gh-pages/_layouts/reference.html Commands used to examine the project's history and differences. 'log' shows commit history with various formatting options. 'diff' displays the differences between commits, branches, or the working directory and the index. ```git git log git diff ``` -------------------------------- ### Configuring Git User Information Source: https://github.com/git/git-reference/blob/gh-pages/zh/basic/index.html Before committing, configure your global user name and email address. This information is recorded with each commit. ```git git config --global user.name 'Your Name' git config --global user.email you@somedomain.com ``` -------------------------------- ### Git Commit and File Operations Source: https://github.com/git/git-reference/blob/gh-pages/branching/index.html Demonstrates a typical workflow involving creating new files, adding them to the staging area, and committing them to the current branch. It also shows how these changes are reflected or removed when switching between branches. ```bash echo 'test content' > test.txt echo 'more content' > more.txt git add *.txt git commit -m 'added two files' # After committing, switching to a different branch will revert the working directory to the state of that branch, potentially removing newly added files. ``` -------------------------------- ### Create and Switch to a New Branch Source: https://github.com/git/git-reference/blob/gh-pages/branching/index.html The `git checkout -b` command creates a new branch and immediately switches your working directory to it. This is a shortcut for creating a branch with `git branch ` and then checking it out with `git checkout `. It's useful for isolating new work. ```git git checkout -b newbranch ``` -------------------------------- ### Git Diff with Stat Summary Source: https://github.com/git/git-reference/blob/gh-pages/basic/index.html Shows a summary of changes instead of a full diff. Useful for quickly seeing which files were modified and the extent of changes. ```shell git diff --stat git diff --cached --stat git diff HEAD --stat ``` -------------------------------- ### Pushing to Remotes Source: https://github.com/git/git-reference/blob/gh-pages/zh/remotes/index.html Commands for uploading local commits and branches to remote repositories. ```git git push github master # Pushes the local 'master' branch to the remote repository named 'github'. # If the branch doesn't exist on the remote, it will be created. git push github erlang # Pushes the local 'erlang' branch to the remote repository named 'github'. # Example of a rejected push due to non-fast-forward updates: git push github master To git@github.com:schacon/hw.git ! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'git@github.com:schacon/hw.git' To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes before pushing again. ``` -------------------------------- ### Git Status Short Output Source: https://github.com/git/git-reference/blob/gh-pages/basic/index.html Displays a concise overview of staged and unstaged changes in the working directory. The first column indicates the staging area status, and the second column indicates the working directory status. ```git # Changed but not updated: # (use "git add ..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # # modified: README # M README D hello.rb ``` ```git # MM README # D hello.rb ``` -------------------------------- ### Branch Creation and Committing Source: https://github.com/git/git-reference/blob/gh-pages/branching/index.html Demonstrates the workflow of creating a new branch, making changes to a file, staging the changes, and committing them to the new branch. This is a fundamental Git operation for isolated development. ```git git checkout -b erlang vim erlang_hw.erl git add erlang_hw.erl git commit -m 'added erlang' ``` ```git vim haskell.hs git add haskell.hs git commit -m 'added haskell' ``` -------------------------------- ### Git MV: Renaming Files Source: https://github.com/git/git-reference/blob/gh-pages/zh/basic/index.html Describes how 'git mv' works by performing 'git rm --cached', renaming the file on disk, and then 'git add' for the new file, explaining that Git tracks renames by comparing snapshots. ```bash git mv git rm --cached orig; mv orig new; git add new ``` -------------------------------- ### Fetch All Tags from Remote Source: https://github.com/git/git-reference/blob/gh-pages/branching/index.html Fetches all tags from the remote repository ('origin'). This command ensures that tags not reachable from branch heads are also downloaded, providing a complete set of tags from the remote. ```git git fetch origin --tags ``` -------------------------------- ### Git Diff with Stat Summary Source: https://github.com/git/git-reference/blob/gh-pages/zh/basic/index.html Use the `--stat` option with `git diff` to display a summary of changes (files changed, insertions, deletions) instead of the full diff output. This provides a concise overview of modifications. ```git git diff --stat ``` ```git git diff --cached --stat ``` ```git git diff HEAD --stat ``` -------------------------------- ### Switching Branches and Committing Source: https://github.com/git/git-reference/blob/gh-pages/branching/index.html Illustrates switching back to the master branch, modifying an existing file, and committing the changes. This shows how to manage work across different branches. ```git git checkout master vim ruby.rb git commit -am 'reverted to old class name' ``` -------------------------------- ### Git Commit with Automatic Staging Source: https://github.com/git/git-reference/blob/gh-pages/basic/index.html The `git commit -a` command stages all modified tracked files and then commits them. This is a shortcut to avoid running `git add` separately for each modified file. New files still require `git add`. ```git git commit -am "changes to hello file" ``` -------------------------------- ### Git MV Source: https://github.com/git/git-reference/blob/gh-pages/basic/index.html `git mv` is a convenience command that performs a `git rm --cached`, moves the file in the working directory, and then runs `git add` on the new file. Git tracks renames by comparing snapshots. ```git git mv ``` -------------------------------- ### Adding a New Remote Repository Source: https://github.com/git/git-reference/blob/gh-pages/zh/remotes/index.html Adds a new remote repository to the project with a specified alias and URL. This allows for synchronization with the newly added remote. ```git git remote add [alias] [url] ``` -------------------------------- ### Git Commit without Message Option Source: https://github.com/git/git-reference/blob/gh-pages/basic/index.html Stages changes and then initiates a commit, opening a text editor (like vim) to write the commit message. Includes status information for context. ```shell # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # On branch master # Changes to be committed: # (use "git reset HEAD ..." to unstage) # # modified: hello.rb # ~ ``` -------------------------------- ### Git 合并命令 Source: https://github.com/git/git-reference/blob/gh-pages/zh/branching/index.html 本节解释了如何使用 'git merge' 命令将一个分支的更改集成到另一个分支。它演示了如何将特性分支合并回主分支。 ```git git merge <分支名> # 将指定的分支合并到当前分支。 ``` -------------------------------- ### Handling Git Merge Conflicts Source: https://github.com/git/git-reference/blob/gh-pages/zh/branching/index.html Demonstrates the process of encountering and resolving a merge conflict in Git. It shows how Git marks conflicts and the steps to manually fix them, stage the changes, and commit the resolution. ```git git checkout -b fix_readme vim README git commit -am 'fixed readme title' git checkout master vim README git commit -am 'fixed readme title differently' git merge fix_readme # Auto-merging README # CONFLICT (content): Merge conflict in README # Automatic merge failed; fix conflicts and then commit the result. cat README # <<<<<<< HEAD # Many Hello World Examples # ======= # Hello World Lang Examples # >>>>>>> fix_readme # # This project has examples of hello world in # nearly every programming language. vim README # Manually fix the conflict git diff # diff --cc README # index 9103e27,69cad1a..0000000 # --- a/README # +++ b/README # @@ -1,4 -1,4 +1,4 @@ # - Many Hello World Examples # Hello World Lang Examples # +Many Hello World Lang Examples # # This project has examples of hello world in git add README git commit # [master 8d585ea] Merge branch 'fix_readme' ``` -------------------------------- ### Git Branching and Merging Operations Source: https://github.com/git/git-reference/blob/gh-pages/_layouts/reference.html Commands for managing branches and integrating changes. 'branch' lists, creates, or deletes branches. 'checkout' switches between branches. 'merge' combines changes from one branch into another. 'log' displays commit history, and 'tag' marks specific points in history. ```git git branch git checkout git merge git log git tag ``` -------------------------------- ### Git Diff Between Release and Current Version Source: https://github.com/git/git-reference/blob/gh-pages/inspect/index.html Compares the current state of the project with a specific release tag (e.g., v0.9) to show all changes made since that release. Includes the --stat option for a summary. ```git git diff v0.9 git diff v0.9 --stat ``` -------------------------------- ### Git Branch and File Operations Source: https://github.com/git/git-reference/blob/gh-pages/zh/branching/index.html This snippet shows the sequence of Git commands used to create a new branch, modify a file, commit changes, switch back to the master branch, rename a file, modify it again, and commit those changes. ```git $ git branch * master $ cat hello.rb class HelloWorld def self.hello puts "Hello World" end end HelloWorld.hello $ git checkout -b change_class M hello.rb Switched to a new branch 'change_class' $ vim hello.rb $ head -1 hello.rb class HiWorld $ git commit -am 'changed the class name' [change_class 3467b0a] changed the class name 1 files changed, 2 insertions(+), 4 deletions(-) $ git checkout master Switched to branch 'master' $ git mv hello.rb ruby.rb $ vim ruby.rb $ git diff diff --git a/ruby.rb b/ruby.rb index 2aabb6e..bf64b17 100644 --- a/ruby.rb +++ b/ruby.rb @@ -1,7 +1,7 @@ class HelloWorld def self.hello - puts "Hello World" + puts "Hello World from Ruby" end end $ git commit -am 'added from ruby' [master b7ae93b] added from ruby 1 files changed, 1 insertions(+), 1 deletions(-) rename hello.rb => ruby.rb (65%) ``` -------------------------------- ### Git Branch Management Commands Source: https://github.com/git/git-reference/blob/gh-pages/zh/branching/index.html This section covers the fundamental Git commands for managing branches. It explains how to list existing branches, create new branches, switch between branches, and delete branches that are no longer needed or have been merged. ```git git branch # Lists all local branches. The current branch is marked with an asterisk. git branch # Creates a new branch named . git checkout # Switches to the specified branch. git checkout -b # Creates a new branch and immediately switches to it. git branch -d # Deletes the specified branch. Use -D to force deletion. ``` -------------------------------- ### Git Merge Command Source: https://github.com/git/git-reference/blob/gh-pages/zh/branching/index.html This section explains how to integrate changes from one branch into another using the 'git merge' command. It demonstrates merging a feature branch back into the main branch. ```git git merge # Merges the specified branch into the current branch. ``` -------------------------------- ### Git Commit 简述 Source: https://github.com/git/git-reference/blob/gh-pages/zh/basic/index.html 简要说明了 `git commit` 命令的作用是记录缓存区的快照,该快照可用于比较、共享和恢复。 ```bash 简而言之,执行 `git commit` 记录缓存区的快照。 ``` -------------------------------- ### Committing Changes Without a Message (Editor Opens) Source: https://github.com/git/git-reference/blob/gh-pages/zh/basic/index.html If the `-m` option is omitted, Git will open a configured editor (like vim) to allow you to write a more detailed commit message, including a summary and a more extensive description. ```git git add hello.rb git commit ``` -------------------------------- ### Basic Git Log Source: https://github.com/git/git-reference/blob/gh-pages/branching/index.html Displays a compact, one-line summary of each commit in the repository's history. This is useful for quickly scanning commit messages and their associated SHAs. ```git git log --oneline ``` -------------------------------- ### Git Diff: Staged Changes Source: https://github.com/git/git-reference/blob/gh-pages/basic/index.html Displays the differences between the staging area and the last commit (HEAD). This command shows the changes that are prepared to be included in the next commit. ```git diff --git a/hello.rb b/hello.rb index d62ac43..8d15d50 100644 --- a/hello.rb +++ b/hello.rb @@ -1,7 +1,7 @@ class HelloWorld def self.hello - puts "hello world" + puts "hola mundo" end end ``` -------------------------------- ### Viewing Commit History Source: https://github.com/git/git-reference/blob/gh-pages/zh/branching/index.html Displays the commit history that led to the current branch's snapshot, showing how the project reached its current state. ```bash $ git log --oneline ``` -------------------------------- ### Git Fetch Command Source: https://github.com/git/git-reference/blob/gh-pages/remotes/index.html Shows how to use the 'git fetch' command to download new branches and data from a remote repository without merging them into the current local branch. ```shell $ git fetch github remote: Counting objects: 4006, done. ... ``` -------------------------------- ### Basic Git Push Source: https://github.com/git/git-reference/blob/gh-pages/zh/remotes/index.html This snippet demonstrates the fundamental usage of the `git push` command to send local changes to a remote repository. It pushes commits from the current local branch to its upstream counterpart on the remote. ```git git push [alias] [branch] ``` -------------------------------- ### Push All Tags to Remote Source: https://github.com/git/git-reference/blob/gh-pages/branching/index.html Pushes all local tags to the remote repository ('origin'). By default, `git push` does not push tags, so the `--tags` option is necessary to synchronize tags. ```git git push origin --tags ``` -------------------------------- ### Git Diff Between Version and Commit Source: https://github.com/git/git-reference/blob/gh-pages/zh/inspect/index.html Compares the project's current state with a specific version or tag, showing the differences. The --stat option provides a summary of changes. ```git git diff v0.9 git diff v0.9 --stat ``` -------------------------------- ### Resolving Git Merge Conflicts Source: https://github.com/git/git-reference/blob/gh-pages/branching/index.html Demonstrates the process of encountering and resolving a merge conflict in Git. It shows how Git marks conflicts and the steps to manually fix them, stage the changes, and commit the resolution. ```git $ git checkout -b fix_readme Switched to a new branch 'fix_readme' $ vim README $ git commit -am 'fixed readme title' [fix_readme 3ac015d] fixed readme title 1 files changed, 1 insertions(+), 1 deletions(-) $ git checkout master Switched to branch 'master' $ vim README $ git commit -am 'fixed readme title differently' [master 3cbb6aa] fixed readme title differently 1 files changed, 1 insertions(+), 1 deletions(-) $ git merge fix_readme Auto-merging README CONFLICT (content): Merge conflict in README Automatic merge failed; fix conflicts and then commit the result. $ cat README <<<<<<< HEAD Many Hello World Examples ======= Hello World Lang Examples >>>>>>> fix_readme This project has examples of hello world in nearly every programming language. $ vim README $ git diff diff --cc README index 9103e27,69cad1a..0000000 --- a/README +++ b/README @@@ -1,4 -1,4 +1,4 @@@ - Many Hello World Examples Hello World Lang Examples +Many Hello World Lang Examples This project has examples of hello world in $ git add README $ git status -s M README $ git commit [master 8d585ea] Merge branch 'fix_readme' ``` -------------------------------- ### Git Reset Configuration Alias Source: https://github.com/git/git-reference/blob/gh-pages/zh/basic/index.html Shows how to configure a global alias for 'git reset HEAD' to 'git unstage' for easier unstaging operations. ```bash git config --global alias.unstage "reset HEAD" ``` -------------------------------- ### Creating Annotated Git Tags Source: https://github.com/git/git-reference/blob/gh-pages/zh/branching/index.html Creates an annotated tag for the latest commit (HEAD) with a version number and a message. Annotated tags are recommended as they store tagger name, email, date, and a tagging message. ```bash $ git tag -a v1.0 ``` -------------------------------- ### Git Log with Graph Source: https://github.com/git/git-reference/blob/gh-pages/branching/index.html Visualizes the commit history with a text-based graph, showing branch and merge points. This helps in understanding the branching and merging patterns of a project. ```git git log --oneline --graph ```