iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💊
Common Git Operations and Troubleshooting with SourceTree
- This article does not cover Git overviews or basic operations.
- It includes frequently used features and basic troubleshooting helpful for Git beginners.
- The instructions assume the use of SourceTree on Mac in a Japanese environment, so please adjust accordingly for Windows or English environments.
Operations before committing
Splitting changes into multiple commits
- Select the file.
- Select the lines you want to commit, right-click, and select "Stage Selected Lines".
- Commit as needed in the desired units.
Saving ongoing changes temporarily
- Select the "Stash" button.
- Enter a message and click the "Stash" button to save (Optional; recorded in the format
On <branch name>: <message>).
To restore a stash, follow these steps:
- Select "Stashes" in the sidebar to display the list of stashes.
- Double-click the stash you want to restore, or right-click and select "Apply Stashed Changes".
Reverting all changes to a file
- Right-click the file and select "Reset...".
Reverting all changed files at once
The "Reset..." method can be slow if there are many files.
Consider the following method:
- Double-click another branch in the sidebar.
- Check "Discard local changes" and select "OK".
- Uncommitted untracked files will remain, so delete them using the "Reset..." procedure.
Reverting only part of the changes
- Select the file.
- Select "Discard Hunk" to revert by hunk, or select the lines you want to revert and select "Discard Selected Lines" to revert by line.
Operations after committing
Changing the last commit message
- Select the "Commit" button.
- From "Commit Options...", select "Amend last commit".
- The commit message will be displayed; modify the text and click "Commit".
Adding files to the last commit
- Select the "Commit" button.
- From "Commit Options...", select "Amend last commit".
- Stage the files or the lines you want to include, and click "Commit".
Reverting part of a committed change
- Select the file from the commit.
- Select "Reverse Hunk" to revert by hunk, or select the lines you want to revert and select "Reverse Selected Lines" to revert by line.
- A reverse patch (diff file) will be created, then commit it.
Reverting an entire commit
- Right-click the commit you want to revert and select "Reverse commit...".
- The revert will be applied as a new commit in the format
Revert "Commit Name".
Resolving a conflict by prioritizing others' changes
- Select the file(s) where the conflict occurred (multiple selection possible).
- From "Resolve Conflicts", select "Resolve Using 'Theirs'".
- A resolved file will be created, then commit it.
The above applies when merging. Note that the logic is reversed when rebasing.
- When a conflict occurs while trying to merge another branch:
- ours: Becomes the state of the current branch
- theirs: Becomes the state of the branch being merged
- When a conflict occurs while trying to rebase another branch:
- ours: Becomes the state of the branch being rebased onto
- theirs: Becomes the state of the current branch
Importing a commit from another branch into your own branch
- Select the commit you want to import, right-click, and select "Cherry-pick".
- A diff file will be created, so commit it (resolve any conflicts if they occur).
Viewing file change history
- Right-click the file and select "Log Selected...".
⚠️ Changing a commit message at an arbitrary location
- Select the commit immediately preceding (older than) the one you want to modify, right-click, and select "Rebase children of <commit ID> interactively...".
- Select the commit row and click "Edit Message".
- After modifying the message, click "OK" to save.
⚠️ Completely deleting the last saved content
- Right-click the commit and select "Reset <branch name> to this commit".
- Select "Hard - Discard all working copy changes" and click "OK".
Common Troubleshooting
Discarding changes in a local branch to start over from the remote branch
- Switch to another branch.
- Right-click the branch you want to delete and select "Delete <branch name>".
- Uncheck "Delete remote branch [origin/<branch name>]" and click "OK".
If the branch has already been committed, check "Force delete" and click "OK".
⚠️ Moving commits mistakenly made on the main branch to another branch
- Create a branch from the main branch and push it.
- Return to the main branch, right-click the "origin/main" commit, and select "Reset main to this commit".
- Select "Hard - Discard all working copy changes" and click "OK".
- Continue working on the branch created in step 1.
⚠️ Restoring a deleted branch
- Press
command + control + Wto display the command history (or "Show Command History" in the "View" menu). - Expand the toggle for the item labeled "Deleting branch...".
- Identify the part that looks like
branch -d <branch name> Deleted branch <branch name> (was<commit ID>).(actually, many options are specified) and take note of the "branch name" and "commit ID". - Select the "Branch" button.
- Enter the "branch name" in "New Branch", check "Specified commit:" for "Commit:", paste the "commit ID" into the text field, and select "Create Branch".
⚠️ Fixing the case sensitivity difference in filenames between local and remote
Mac does not distinguish between uppercase and lowercase in filenames, so changing a filename after pushing will not show any differences.
Simply enabling the setting to distinguish between uppercase and lowercase will result in the following unintended behavior:
- Push A.txt
- Even if A.txt is corrected to a.txt locally, no difference is detected
- Run
git config core.ignorecase falseat the repository root to enable difference detection - Push the difference
- Both A.txt and a.txt exist on the remote
- Updating a.txt locally causes differences in both a.txt and A.txt
- Pushing step 6 updates both a.txt and A.txt on the remote
Correct it as follows:
- Run
git config core.ignorecase falseat the repository root (or editignorecasein SourceTree under "Settings", "Advanced", "Edit Config File..."). - Locally change the filename to a temporary name (e.g., A.txt -> temporary.txt) and commit it.
- Change the temporary filename (temporary.txt) to the correct filename and commit it.
- Push the changes.
Discussion