Git Cheat Sheet

dfd7 9年前發布 | 10K 次閱讀 Git 版本控制系統

  • Configure

    • git config –global user.name "[name]"

      • Sets the name you want attached to your commit transactions
      • </ul> </li>

      • git config –global user.email "[email address]"

        • Sets the email you want attached to your commit transactions
        • </ul> </li>

        • git config –global color.ui auto

          • Enables helpful colorization of command line output
          • </ul> </li>

          • git config –global push.default current

            • Update a branch with the same name as current branch if no refspec is given
            • </ul> </li>

            • git config –global core.editor [editor]

              • Which editor to use when commit and tag that lets you edit messages
              • </ul> </li>

              • git config –global diff.tool [tool]

                • Specify which command to invoke as the specified tool for git difftool
                • </ul> </li> </ul> </li>

                • Create repositories

                  • git init [project-name]

                    • Creates a new local repository with the specified name
                    • </ul> </li>

                    • git clone [url]

                      • Downloads a project nd its entire version history
                      • </ul> </li> </ul> </li>

                      • Make changes

                        • git status

                          • Lists all new or modified files to be committed
                          • </ul> </li>

                          • git status -s

                            • Short view of status
                            • </ul> </li>

                            • git diff

                              • Shows file differences not yet staged
                              • </ul> </li>

                              • git add [file]

                                • Snapshots the file in preparation for versioning
                                • </ul> </li>

                                • git add .

                                  • Add all modified files to be commited
                                  • </ul> </li>

                                  • git add '*.txt'

                                    • Add only certain files
                                    • </ul> </li>

                                    • git add –patch filename.x (or -p for short)

                                      • Snapshot only chunks of a file
                                      • </ul> </li>

                                      • git rm [file]

                                        • Tell git not to track the file anymore
                                        • </ul> </li>

                                        • git diff –staged

                                          • Show what has been added to the index via git add but not yet committed
                                          • </ul> </li>

                                          • git diff HEAD

                                            • Shows what has changed since the last commit.
                                            • </ul> </li>

                                            • git diff HEAD^

                                              • Shows what has changed since the commit before the latest commit
                                              • </ul> </li>

                                              • git diff [branch]

                                                • Compare current branch to some other branch
                                                • </ul> </li>

                                                • git difftool -d

                                                  • Same as diff, but opens changes via difftool that you have configured
                                                  • </ul> </li>

                                                  • git difftool -d master..

                                                    • See only changes made in the current branch
                                                    • </ul> </li>

                                                    • git diff –no-commit-id –name-only –no-merges origin/master…

                                                      • See only the file names that has changed in current branch
                                                      • </ul> </li>

                                                      • git diff –stat

                                                        • See statistics on what files have changed and how
                                                        • </ul> </li>

                                                        • git reset [file]

                                                          • Unstages the file, but preserves its contents
                                                          • </ul> </li>

                                                          • git commit

                                                            • Record changes to git. Default editor will open for a commit message
                                                            • </ul> </li>

                                                            • git commit -m "[descriptive message]"

                                                              • Records file snapshots permanently in version history
                                                              • </ul> </li>

                                                              • git commit –amend

                                                                • Changing the history, of the HEAD commit
                                                                • </ul> </li> </ul> </li>

                                                                • Group changes

                                                                  • git branch

                                                                    • Lists all local branches in the current directory
                                                                    • </ul> </li>

                                                                    • git branch [branch-name]

                                                                      • Create a new branch
                                                                      • </ul> </li>

                                                                      • git checkout [branch-name]

                                                                        • Switches to the specified branch and updates the working directory
                                                                        • </ul> </li>

                                                                        • git checkout -b <name> <remote>/<branch>

                                                                          • Switches to a remote branch
                                                                          • </ul> </li>

                                                                          • git checkout [filename]

                                                                            • Return file to it's previous version, if it hasn’t been staged yet
                                                                            • </ul> </li>

                                                                            • git merge [branch]

                                                                              • Combines the specified branch's history into the current branch
                                                                              • </ul> </li>

                                                                              • git merge –no–ff [branch]

                                                                                • Merge branch without fast forwarding
                                                                                • </ul> </li>

                                                                                • git branch -a

                                                                                  • See the full list of local and remote branches
                                                                                  • </ul> </li>

                                                                                  • git branch -d [branch]

                                                                                    • Deletes the specified branch
                                                                                    • </ul> </li>

                                                                                    • git branch -D [branch]

                                                                                      • Hard branch delete, will not complain
                                                                                      • </ul> </li>

                                                                                      • git branch -m <oldname> <newname>

                                                                                        • Rename a branch
                                                                                        • </ul> </li> </ul> </li>

                                                                                        • Refactor filenames

                                                                                          • git rm [file]

                                                                                            • Deletes the file from the working directory and stages the deletion
                                                                                            • </ul> </li>

                                                                                            • git rm –cached [file]

                                                                                              • Removes the file from version control but preserves the file locally
                                                                                              • </ul> </li>

                                                                                              • git mv [file-original] [file-renamed]

                                                                                                • Changes the file name and prepares it for commit
                                                                                                • </ul> </li> </ul> </li>

                                                                                                • Suppress tracking

                                                                                                  • .gitignore

                                                                                                    • *.log
                                                                                                    • build/
                                                                                                    • temp-*
                                                                                                    • A text file named .gitignore suppresses accidental versioning of files and paths matching the specified patterns
                                                                                                    • </ul> </li>

                                                                                                    • git ls-files –other –ignored –exclude-standard

                                                                                                      • Lists all ignored files in this project
                                                                                                      • </ul> </li> </ul> </li>

                                                                                                      • Save fragments

                                                                                                        • git stash

                                                                                                          • Temporarily stores all modified tracked files
                                                                                                          • </ul> </li>

                                                                                                          • git stash pop

                                                                                                            • Restores the most recently stashed files
                                                                                                            • </ul> </li>

                                                                                                            • git stash list

                                                                                                              • Lists all stashed changesets
                                                                                                              • </ul> </li>

                                                                                                              • git stash drop

                                                                                                                • Discards the most recently stashed changeset
                                                                                                                • </ul> </li> </ul> </li>

                                                                                                                • Review history

                                                                                                                  • git log

                                                                                                                    • Lists version history for the current branch
                                                                                                                    • </ul> </li>

                                                                                                                    • git log –follow [file]

                                                                                                                      • Lists version history for a file, including renames
                                                                                                                      • </ul> </li>

                                                                                                                      • git log –pretty=format:"%h %s" –graph

                                                                                                                        • Pretty commit view, you can customize it as much as you want
                                                                                                                        • </ul> </li>

                                                                                                                        • git log –author='Name' –after={1.week.ago} –pretty=oneline –abbrev-commit

                                                                                                                          • See what the author has worked on in the last week
                                                                                                                          • </ul> </li>

                                                                                                                          • git log –no-merges master..

                                                                                                                            • See only changes in this branch
                                                                                                                            • </ul> </li>

                                                                                                                            • git diff [file-branch]…[second-branch]

                                                                                                                              • Shows content differences between two branches
                                                                                                                              • </ul> </li>

                                                                                                                              • git show [commit]

                                                                                                                                • Outputs metadata and content changes of the specified commit
                                                                                                                                • </ul> </li> </ul> </li>

                                                                                                                                • Redo commits

                                                                                                                                  • git reset

                                                                                                                                    • Unstage pending changes, the changes will still remain on file system
                                                                                                                                    • </ul> </li>

                                                                                                                                    • git reset [commit/tag]

                                                                                                                                      • Undoes all commits after [commit], preserving changes locally
                                                                                                                                      • </ul> </li>

                                                                                                                                      • git reset –hard [commit]

                                                                                                                                        • Discards all history and changes back to the specified commit
                                                                                                                                        • </ul> </li> </ul> </li>

                                                                                                                                        • Synchronize changes

                                                                                                                                          • git fetch [bookmark]

                                                                                                                                            • Downloads all history from the repository bookmark
                                                                                                                                            • </ul> </li>

                                                                                                                                            • git fetch -p

                                                                                                                                              • Update history of remote branches, you can fetch and purge
                                                                                                                                              • </ul> </li>

                                                                                                                                              • git merge [bookmark]/[branch]

                                                                                                                                                • Combines bookmark's branch into current local branch
                                                                                                                                                • </ul> </li>

                                                                                                                                                • git push

                                                                                                                                                  • Push current branch to remote branch
                                                                                                                                                  • </ul> </li>

                                                                                                                                                  • git push [remote] [branch]

                                                                                                                                                    • Manually specify remote and branch to use every time
                                                                                                                                                    • </ul> </li>

                                                                                                                                                    • git push -u origin master

                                                                                                                                                      • If a remote branch is not set up as an upstream, you can make it so
                                                                                                                                                      • </ul> </li>

                                                                                                                                                      • git pull

                                                                                                                                                        • Downloads bookmark history and incorporates changes
                                                                                                                                                        • </ul> </li>

                                                                                                                                                        • git pull [remote] [branch]

                                                                                                                                                          • Specify to pull a specific branch
                                                                                                                                                          • </ul> </li>

                                                                                                                                                          • git remote

                                                                                                                                                            • See list of remote repos available
                                                                                                                                                            • </ul> </li>

                                                                                                                                                            • git remote -v

                                                                                                                                                              • Detailed view of remote repos available
                                                                                                                                                              • </ul> </li>

                                                                                                                                                              • git remote add [remote] [url]

                                                                                                                                                                • Add a new remote
                                                                                                                                                                • </ul> </li> </ul> </li> </ul> </span>

 本文由用戶 dfd7 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!