Редактирование: Участник:Lit-uriy/GitIntroductionWithQt
Материал из Wiki.crossplatform.ru
Внимание: Вы не представились системе. Ваш IP-адрес будет записан в историю изменений этой страницы.
Правка может быть отменена. Пожалуйста, просмотрите сравнение версий, чтобы убедиться, что это именно те изменения, которые вас интересуют, и нажмите «Записать страницу», чтобы изменения вступили в силу.
Текущая версия | Ваш текст | ||
Строка 9: | Строка 9: | ||
__TOC__ | __TOC__ | ||
- | === | + | === Getting the Qt source code === |
Этот раздел показывает, как загрузить исходный код, и объясняет концепцию Git-хранилища. | Этот раздел показывает, как загрузить исходный код, и объясняет концепцию Git-хранилища. | ||
Строка 84: | Строка 84: | ||
* master}} | * master}} | ||
- | + | The output shows that we have only one local branch called “master” and the “*” indicates that it is the current branch. The name “master” in git development usually describes the main line of development. | |
- | + | Besides your local branches git also remembers the branches in the origin repository. The “-r” option shows them, prefixed with “origin”: | |
{{code|bash|code=$ git branch -r | {{code|bash|code=$ git branch -r | ||
origin/4.5 | origin/4.5 | ||
Строка 92: | Строка 92: | ||
origin/master}} | origin/master}} | ||
- | + | For the purpose of developing a new feature we can create a new branch with the same command: | |
{{code|bash|code=$ git branch my-feature origin/master}} | {{code|bash|code=$ git branch my-feature origin/master}} | ||
- | + | The “my-feature” branch is based on the origin’s “master” branch, now visible in the list of branches: | |
{{code|bash|code=$ git branch | {{code|bash|code=$ git branch | ||
* master | * master | ||
my-feature}} | my-feature}} | ||
- | + | The git checkout command can also change the current branch: | |
{{code|bash|code=$ git checkout my-feature}} | {{code|bash|code=$ git checkout my-feature}} | ||
- | + | Switched to branch "my-feature" | |
- | + | ||
{{code|bash|code=$ git branch | {{code|bash|code=$ git branch | ||
master | master | ||
* my-feature}} | * my-feature}} | ||
- | + | This will also update the files in your working directory to match the latest commit in the new current branch. New commits will be recorded in “my-feature” instead of the previous “master” branch. | |
- | + | You can have as many local branches as you like, they are very cheap. Each branch uses only few bytes of disk space. Please see the documentation of the git branch command for instructions on how to delete branches. | |
==== Реальное внесение изменений ==== | ==== Реальное внесение изменений ==== | ||
- | + | Now we can go ahead and start making changes: | |
{{code|bash|code=$ edit src/corelib/tools/qstring.cpp}} | {{code|bash|code=$ edit src/corelib/tools/qstring.cpp}} | ||
- | + | The diff command shows us what we have changed: | |
{{code|bash|code=$ git diff | {{code|bash|code=$ git diff | ||
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp | diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp | ||
Строка 157: | Строка 156: | ||
int lastIndexOf(const QRegExp &, int from = -1) const;}} | int lastIndexOf(const QRegExp &, int from = -1) const;}} | ||
- | + | The status command gives an overview of what “git commit” would do: | |
{{code|bash|code=$ git status | {{code|bash|code=$ git status | ||
# On branch master | # On branch master | ||
Строка 169: | Строка 168: | ||
no changes added to commit (use "git add" and/or "git commit -a")}} | no changes added to commit (use "git add" and/or "git commit -a")}} | ||
- | + | Following the recommendation in the output we mark qstring.cpp and .h as files to be included in the next commit: | |
{{code|bash|code=$ git add src/corelib/tools/qstring.cpp src/corelib/tools/qstring.h | {{code|bash|code=$ git add src/corelib/tools/qstring.cpp src/corelib/tools/qstring.h | ||
$ git status | $ git status | ||
Строка 181: | Строка 180: | ||
# Untracked files not listed (use -u option to show untracked files)}} | # Untracked files not listed (use -u option to show untracked files)}} | ||
- | + | And finally we create a new commit: | |
{{code|bash|code=$ git commit -m "Add a new reversed() function to QString" | {{code|bash|code=$ git commit -m "Add a new reversed() function to QString" | ||
[my-feature c8d9c54] Add a new reversed() function to QString}} | [my-feature c8d9c54] Add a new reversed() function to QString}} | ||
- | Обычно git откроет новый текстовый редактор, если вы пропускаете | + | Обычно git откроет новый текстовый редактор, если вы пропускаете параметр ''-m''. |
- | + | Marking files to be included in commits means adding them to git’s staging area. The staging area is a powerful concept, making it easy to work on multiple files simultaenously while still creating small incremental commits. | |
- | === | + | === Common operations and fixing mistakes === |
- | + | This section gives recipes for fixing common mistakes and explains common operations such as reviewing your changes before committing using the diff command. | |
- | + | You can undo changes to a file before it is committed with the checkout command: | |
{{Команда|$ git checkout HEAD -- src/corelib/tools/qstring.cpp}} | {{Команда|$ git checkout HEAD -- src/corelib/tools/qstring.cpp}} | ||
- | + | If you have accidentially marked a file for the next commit you can reset it like this: | |
{{Команда|$ git reset HEAD -- src/corelib/tools/qstring.cpp}} | {{Команда|$ git reset HEAD -- src/corelib/tools/qstring.cpp}} | ||
- | + | This will exclude the file from the next commit and leave it in your working directory unchanged with your modifications intact. | |
- | + | After changing a file the git diff command shows the lines you have modified. The difference is calculated from the file in your working directory and the file when it was added to the staging area using git add. | |
- | + | If you want to show the difference between the file in the staging area and the last commit you have to pass an option to the diff command: | |
{{Команда|$ git diff --cached}} | {{Команда|$ git diff --cached}} | ||
- | + | The diff command optionally takes directories and files as argument, to limit the output. | |
- | + | Files are renamed in git by renaming them in your working directory using a command/tool of your choice. Once you've renamed a file you have to tell git: | |
- | {{Команда| | + | {{Команда|[ rename oldfile.cpp to newfile.cpp using a graphical file manager ] |
$ git rm oldfile.cpp | $ git rm oldfile.cpp | ||
$ git add newfile.cpp}} | $ git add newfile.cpp}} | ||
- | + | Alternatively you can use the convenience mv command: | |
{{Команда|$ git mv oldfile.cpp newfile.cpp}} | {{Команда|$ git mv oldfile.cpp newfile.cpp}} | ||
- | + | One of git’s strengths is that it does not care how exactly you change your files. It is optimized to find out what changed after the fact. For example when copying a file in your source code it is enough to simply run “git add” with the new file. Git automatically finds out where the file was copied from. | |
=== Отправка и обновление ваших изменений === | === Отправка и обновление ваших изменений === | ||
==== Получение обновлений ==== | ==== Получение обновлений ==== | ||
- | + | After studying the previous chapters you should be able to develop small changes by creating commits in your local branches. This chapter guides you through the steps of combining your changes with the latest changes from the upstream repository and explains how to create separate patch files for submission into the project. | |
- | + | While developing code in your local clone of the project, the original repository evolves and sees new changes. First we update our mirror of the commits in the original repository using the fetch command: | |
{{Команда|$ git fetch | {{Команда|$ git fetch | ||
remote: Counting objects: 3013, done. | remote: Counting objects: 3013, done. | ||
Строка 231: | Строка 230: | ||
be7384d..822114e master -> origin/master}} | be7384d..822114e master -> origin/master}} | ||
- | + | The output indicates that the “master” branch in the original gitorious.org repository has been updated with new commits and these commits are now available in the locally mirrored origin/master branch. | |
- | + | You can inspect the new changes using the log or show commands: | |
{{Команда|$ git log -p origin/master}} | {{Команда|$ git log -p origin/master}} | ||
- | + | The log command will include the diffs of the individual commits if you specify the -p argument. | |
Updating your branch with the downloaded, new changes | Updating your branch with the downloaded, new changes | ||
Строка 255: | Строка 254: | ||
Note: You only need to create one personal clone — there is no need for a separate repository for each fix, use branches for that. | Note: You only need to create one personal clone — there is no need for a separate repository for each fix, use branches for that. | ||
- | === | + | === Conclusion === |
We hope that this introductions helps you familiarize yourself with the use of git. The official Git documentation web page is a very good continuation point: | We hope that this introductions helps you familiarize yourself with the use of git. The official Git documentation web page is a very good continuation point: | ||