Git tips¶
This section summarizes information related to git.
Basic Flow¶
The general flow of pushing code to a remote repository using Git is as follows.
Initialize or clone the repository¶
If you already have a remote repository, use the git clone
command to clone the existing repository locally.
To create a new repository, use the git init
command to initialize the new repository.
Staging¶
Add the modified file to the staging area.
This is preparatory work before committing your changes.
To stage all changes, use the git add .
command to stage all changes.
To stage only specific files, use the git add filename
command.
Commit¶
Commit the changes you have added to the staging area. This will record the changes in the repository history.
Use the git commit -m "commit message"
command to create a commit. The commit message is a description of the change.
Remote Repository Settings¶
Add a remote repository. Usually, this remote repository is on a hosting service such as GitHub or GitLab.
Use the git remote add origin remote repository URL
command to add a remote repository. origin" is the name of the reference to the remote repository.
Push¶
Push local commits to the remote repository. This will push changes to the remote repository.
Use the git push origin branch name
command to push changes on a specified branch to the remote repository.
Here, branch name is the name of the branch you are pushing to. By default, this is usually main or master.
Details for each command¶
Staging(git add)¶
git add is a command that adds files you have modified in Git to the staging area (index). You can see the files and folders you have changed with the git status
command. By adding a file to the staging area, Git treats that change as the subject of the next commit.
1 2 3 4 5 6 7 8 9 10 11 |
|
This example display contains the following information
- A line indicating that the current branch is
main
. - A line indicating that the branch is one commit ahead of the
origin/main
branch. - A list of files whose changes have not been staged. Lines indicating that changes to
folder/file3.txt
,folder/file4.txt
, andfile1.txt
have not yet been staged. - A message indicating that the changes have not been staged.
From this output, you can see the change status of the files in the working tree. Before committing these changes, you can use the git add
command to stage the necessary changes.
The git add
command is used as follows
1 |
|
If multiple files are to be added to the staging area, multiple files can be specified in the git add
command.
1 |
|
You can also specify a directory to add all changes in that directory to the staging area at once.
1 |
|
If you want to stage all the files that you have changed, you can run the command under the project (. refers to the current directory), execute the command
1 |
|
Files added to the staging area can be committed to the local repository by running the git commit
command.
Undo staging (git reset)¶
The git reset
command can be used to undo files added to the staging area (index).
First, check the files added to the staging area with the git status
command.
1 2 3 4 5 6 7 8 9 10 |
|
Use the git reset
command to cancel files added to the staging area.
1 |
|
The HEAD
refers to the latest commit. You can undo changes added to the staging area by specifying HEAD to the git reset
command.
If no file is specified, all files can be undone from the staging area.
1 |
|
Thus, the git reset
command can be used to undo changes to files that have been added to the staging area. However, the undone changes will remain in the working directory. If necessary, you can also use the git restore
command to undo changes in the working directory.
Note that if you do not specify HEAD for the git reset
command, the commit history is not changed, but you can undo changes to files from the staging area. Specifically, changes to files that are undone from the staging area are returned to the working directory. But note that the commit history is not changed.
Revert changes in the working tree (git restore command)¶
The git restore
command is used to restore changes from the working tree to the staging area or from the staging area to the commits. Specifically, it can be used as follows.
1 |
|
Changes to file.txt
in the staging area are undone and the **file reverts to the state of the previous commit. **
If you added a file to the staging area with the git add
command, but want to revert it, use git restore --staged
.
For example, if you added the file1.txt
file to the staging area and want to revert the changes, use
1 2 |
|
The --satge
option will undo **changes to file.txt
in the staging area, but the file itself will remain unchanged. **
That is, the changes remain in the staging area.
git commit¶
basic form¶
The git commit command is used to commit changes to a local repository.
Below is an example of a basic git commit command.
1 |
|
Example using commit type and emojis¶
Whether or not to include a space after the emoji depends on the style of the commit message and the guidelines of the project. Some projects may require a space after the emoji in the commit message, but generally spaces are often omitted.
1 |
|
commit type¶
A commit type refers to a generic identifier used to succinctly describe the purpose of a commit.
The commit type helps other developers understand the purpose of the developer's modification of the code.
Common commit types include the following
- feat: used when a new feature is added.
- fix: Used to fix bugs.
- docs: Used for document changes.
- style: Used when you have made changes to the style of the code (spacing, formatting, etc.).
- refactor: Used when you have made changes that do not change the functionality of the code.
- test: Used when changes are made to the test code.
- chore: Used when changes are made to the build process or auxiliary tools.
For example, when modifying the README.md file, the commit type is "docs". Therefore, the commit message would be as follows.
1 |
|
This allows other developers to understand that this commit updates the documentation in the README.md file.
commit message¶
There are several elements in the commit message that can be used to add details
- Scope: an optional element that indicates the scope affected by the commit. This element can be used to specify the files, modules, functions, etc. that the commit modifies.
In the case of modifying the README.md file, the scope is the README. Therefore, the commit message would be as follows.
1 |
|
- Subject: A required element that gives a brief summary of the commit. It is written on the first line of the commit message.
Example: If the subject is about modifying the README.md file, the subject would be about the updated information. Therefore, the commit message would look like this
1 |
|
- Body: An optional element detailing the commit. This element is used to describe the details of the changes made by the commit, background, reasons, etc.
Example: In the case of a modification to the README.md file, the body can include a detailed description of the new information. Thus, the commit message would look like this
1 2 3 4 5 6 |
|
- Footer: An optional element indicating metadata about the commit. This element is used to indicate the associated Issue number, significant changes, destructive changes, etc.
Example: In the case of a modification to the README.md file, the footer may include the associated Issue number. Thus, the commit message would look like this
1 2 3 4 5 6 7 8 |
|
Example of adding commit type and git message¶
This example specifies "search" as the scope and uses a commit type of "feat" to indicate that a new feature has been added. The body describes the changes in detail, and the footer describes the associated issue number as "Closes #1234." This clearly indicates which issue this commit is related to and facilitates collaboration across the team.
1 2 3 4 5 |
|
Commit Message Templates¶
How to specify a commit message template¶
In the git commit command, the -t option can be used to specify a commit message template.
Example template for docs¶
This example template includes scope, commit type, changes, and test method to create a more detailed commit message.
Contents of docs_gt
1 2 3 4 5 6 7 8 9 10 |
|
Specified with the -t option at commit time
1 |
|
Template settings for commit messages to be used by default¶
By specifying a path in commit.template, the file specified in that path can be used as the default commit message template.
Reference:Gitコミットスタイル
Creating a .gitmessage¶
1 2 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Specify the path to the template file to be used at commit time in Git's global configuration with the git config --global
command.
1 |
|
If commit.template is set, the template will be automatically loaded and displayed as the initial text of the editable commit message when the git commit command is executed.
In the case of vscode, it will be displayed by default on COMMIT_EDITMSG when you commit after staging changes.
Note that, as with the --template option, the commit.template setting will be overwritten if another template file is specified with the -t option when the git commit command is executed.
1 2 3 4 5 6 |
|
【GitHub】About the display of GitHub after git commit / push¶
When you write the issue number (with #) of the forked source on git commit, it is automatically converted to a URL with the title "account name# issue number"¶
Undo a commit after git commit (git reset / git revert)¶
To undo a commit after a git commit
, the git reset
command is generally used. Be careful, however, as undoing a commit may affect your ability to work with other developers and remote repositories. The following describes how to undo a commit.
Reference(Japanese)¶
Only HEAD is restored.¶
1 |
|
This command undoes the previous commit and returns the changes to the staging area, retaining the commit message. After this, the changes can be modified and re-committed.
HEAD, restore index¶
1 |
|
This command undoes the commit, returns the changes to the working directory, and removes them from the staging area. The commit is not removed from the history, but the changes are retained as unstaged.
HEAD, index, and working tree all restored¶
1 |
|
This command completely undoes the previous commit and its changes, and also restores the working tree to its pre-modified state. Note that, once undone, commits and changes cannot be restored.
Undo a specific commit¶
1 |
|
The git revert
command creates a new commit that undoes the changes in a given commit. This not only undoes the changes, but also records them in the history. This is a way to undo changes while still keeping up with other developers.
Changing commit messages¶
The following method overwrites the commit history with the changed commit message.
1 2 3 4 5 6 |
|
【GitHub】About Project Forking¶
Forking a project on GitHub allows you to copy the original repository and bring it to your own GitHub account.
Below are the etiquette, precautions, and methods for forking.
Etiquette¶
- As a general rule, it is advisable to check the license of the project to be forked and properly credit it.
- When contributing to a forked project, be sure to follow the same proper development process as for the original project.
Note¶
- Make sure you understand and comply with the license, including the project from which you are forking.
- If the project from which you are forking is updated, synchronize regularly to ensure that you are using the latest code.
Usage¶
- Log in to GitHub and access the repository of the project you wish to fork.
- Click the "Fork" button in the upper right corner.
- A copy of the forked repository will be created in your GitHub account.
- Make the necessary modifications to the copied repository and commit.
How to incorporate changes in the fork source¶
Add the forked repository to the remote¶
1 |
|
Update local forked repositories¶
1 |
|
Merge changes from the fork source¶
1 |
|
To rebase, execute the following command¶
1 |
|
Conflict Resolution¶
If conflicts arise when merging or rebasing changes, they must be resolved manually. The method of resolving conflicts depends on the nature of the change.
Push changes¶
After merging or rebasing the changes, push the changes to the forked repository.
1 |
|
By following these steps, you can incorporate the changes from the forked source into your own forked repository.