16 KiB
16 KiB
# Appendix A: Git in Other Environments
## Purpose / framing
- You’ve learned Git at the command line
- Work with local files
- Connect repositories over a network
- Collaborate effectively
- Git is often part of a larger ecosystem
- Terminal isn’t always the best interface
- Goal of this appendix
- Survey other environments where Git is useful
- Show how other applications work alongside Git
## Graphical Interfaces (GUIs)
- Git’s native environment: the terminal
- New features appear there first
- Full power is available at the command line
- Why GUIs exist / when they help
- Plain text isn’t best for all tasks
- Visual representations can be essential
- Some users prefer point-and-click workflows
- Tools differ by intended workflow
- Some clients expose only a curated subset of Git
- To support a specific way of working the author considers effective
- “Better” is workflow-dependent
- Tools are more/less fit for purpose, not universally better
- Key constraint
- GUIs can’t do anything the command line client can’t
- Command line = most power and control
## gitk and git-gui (bundled with Git)
### gitk (graphical history viewer)
- What it is
- GUI history viewer
- Think: powerful GUI shell over
- `git log`
- `git grep`
- Best used for
- Finding something that happened in the past
- Visualizing project history
- How to start
- In a Git repo directory
- `cd <repo>`
- Run:
- `gitk [git log options]`
- Options
- Accepts many command-line options
- Most are passed through to underlying `git log`
- Especially useful:
- `--all`
- Show commits reachable from any ref (not just `HEAD`)
- Interface overview (Figure 151: The gitk history viewer)
- Top area (graph; similar to `git log --graph`)
- Dot = commit
- Lines = parent relationships
- Refs = colored boxes
- Yellow dot = `HEAD`
- Red dot = changes not yet a commit
- Bottom area (selected commit)
- Left = comments + patch
- Right = summary view
- Middle controls
- Searching history controls
### git-gui (commit crafting tool)
- What it is
- Primarily a tool for crafting commits
- How to start
- Run:
- `git gui`
- Interface overview (Figure 152: The git-gui commit tool)
- Left: index (staging area)
- Unstaged changes: top
- Staged changes: bottom
- Move entire files between states
- Click file icons
- Select a file for viewing
- Click file name
- Top right: diff view
- Shows changes for the selected file
- Stage granular changes
- Stage individual hunks (or lines) by right-clicking
- Bottom right: message + action area
- Enter commit message
- Click “Commit”
- Similar to `git commit`
- Amend last commit workflow
- Select “Amend” radio button
- Updates “Staged Changes” with contents of last commit
- Then
- Stage/unstage changes
- Alter commit message
- Click “Commit” again
- Replaces old commit with a new one
### Tool style classification
- `gitk` and `git-gui` are **task-oriented tools**
- Tailored to one purpose each
- `gitk`: viewing/searching history
- `git-gui`: creating/amending commits
- Omit features not needed for that task
## GitHub for macOS and Windows
- What these clients are
- Two **workflow-oriented** Git clients
- One for Windows
- One for macOS
- Workflow-oriented meaning
- Focus on a curated set of commonly used features
- Avoid exposing all of Git’s functionality
- Similarity across platforms
- Designed to look and work very much alike
- Treated as a single product here
- Not a full product rundown
- They have their own documentation
- Focus here: “changes” view tour (main work area)
### “Changes” view: main areas
- Left: repositories list (tracked by client)
- Add repo via “+” icon
- Clone
- Attach local repository
- Center: commit-input area
- Enter commit message
- Select which files to include
- Commit history placement differs
- Windows: directly below
- macOS: separate tab
- Right: diff view
- Shows working directory changes
- Or changes included in selected commit
- Top-right: “Sync” button
- Primary network interaction method
- Important note
- No GitHub account required
- Designed to highlight GitHub service/workflow
- Still works with
- Any repository
- Any Git host (network operations)
### Installation
- Download locations
- GitHub for Windows: <https://windows.github.com>
- GitHub for macOS: <https://mac.github.com>
- First run behavior
- Walks through first-time Git setup
- Configure name
- Configure email address
- Sets sane defaults for common config options
- Credential caches
- CRLF behavior
- Update model
- “Evergreen”
- Updates downloaded/installed in background while app is open
- Bundled Git
- Likely no need to manually update Git separately
- Windows addition
- Includes shortcut to launch PowerShell with Posh-git (covered later)
- Add repositories
- Shows list of GitHub repos you have access to
- Clone in one step
- Add existing local repo
- Drag directory from
- Finder (macOS)
- Windows Explorer (Windows)
- Added to repository list on the left
### Recommended Workflow (“GitHub Flow”)
- Covered elsewhere in more detail (“The GitHub Flow”), gist here
- (a) Commit to a branch
- (b) Sync with a remote repository fairly regularly
- Branch management differences
- macOS
- “Create Branch” button at top of window (Figure 155)
- Windows
- Type new branch name in branch-switching widget (Figure 156)
- Making commits
- Make changes in working directory
- Client shows changed files
- Enter commit message
- Select files to include
- Click “Commit”
- Shortcut: `Ctrl-Enter` (Windows) or `⌘-Enter` (macOS)
- Network interaction via “Sync”
- Git internally separates
- push / fetch / merge / rebase
- GitHub clients collapse these into one multi-step feature
- Clicking “Sync” does
1. `git pull --rebase`
- If fails due to merge conflict
- Fall back to `git pull --no-rebase`
2. `git push`
- Rationale
- Common network sequence in this style
- Squashing into one command saves time
### GitHub client summary (fit and tradeoffs)
- Strengths
- Well-suited to intended workflow
- Developers and non-developers can collaborate within minutes
- Best practices baked into the tools
- When to choose something else
- Workflow differs
- Want more control over
- How network operations are done
- When network operations are done
- Recommendation
- Use another client or the command line
## Other GUIs (general landscape)
- Many graphical Git clients exist
- From specialized single-purpose tools
- To apps that try to expose everything Git can do
- Where to find them
- Official Git site curated list: <https://git-scm.com/downloads/guis>
- More comprehensive list (Git wiki):
- <https://git.wiki.kernel.org/index.php/Interfaces,_frontends,_and_tools#Graphical_Interfaces>
## Git in Visual Studio
- Built-in Git tooling
- Starting Visual Studio 2019 version 16.8
- Supported Git functionality
- Create or clone a repository
- Open and browse repository history
- Create and checkout branches and tags
- Stash, stage, and commit changes
- Fetch, pull, push, or sync commits
- Merge and rebase branches
- Resolve merge conflicts
- View diffs
- “…and more!”
- Next step
- Read official documentation
## Git in Visual Studio Code
- Built-in Git support
- Requirement
- Git version 2.0.0 (or newer) installed
- Main features
- Diff in the gutter for the file you are editing
- Git Status Bar (lower left)
- Current branch
- Dirty indicators
- Incoming commits
- Outgoing commits
- Common operations inside the editor
- Initialize a repository
- Clone a repository
- Create branches and tags
- Stage and commit changes
- Push/pull/sync with a remote branch
- Resolve merge conflicts
- View diffs
- GitHub Pull Requests support (extension)
- <https://marketplace.visualstudio.com/items?itemName=GitHub.vscode-pull-request-github>
- Official documentation
- <https://code.visualstudio.com/Docs/editor/versioncontrol>
## Git in JetBrains IDEs (IntelliJ / PyCharm / WebStorm / PhpStorm / RubyMine / others)
- Git Integration plugin
- Ships with JetBrains IDEs
- Provides
- Dedicated IDE view for Git
- Dedicated IDE view for GitHub Pull Requests
- Example: Version Control ToolWindow (Figure 157)
- Dependency
- Relies on command-line `git`
- Requires `git` to be installed
- Official documentation
- <https://www.jetbrains.com/help/idea/using-git-integration.html>
## Git in Sublime Text
- Availability
- From version 3.2 onwards
- Features
- Sidebar shows git status with a badge/icon
- `.gitignore`-listed files/folders are faded in sidebar
- Status bar shows
- Current git branch
- How many modifications you have made
- Gutter markers show all changes to a file
- Partial integration with Sublime Merge
- Use some Sublime Merge git client functionality from within Sublime Text
- Requires Sublime Merge installed
- Sublime Merge: <https://www.sublimemerge.com/>
- Official documentation
- <https://www.sublimetext.com/docs/3/git_integration.html>
## Git in Bash
- Motivation
- Use shell features to make Git friendlier in Bash
- Note
- Git ships with plugins for several shells
- Not enabled by default
### Enable tab completion (`git-completion.bash`)
- Get completion script matching your Git version
- Check version: `git version`
- In Git source for that release
- `git checkout tags/vX.Y.Z` (match your installed version)
- Copy file:
- `contrib/completion/git-completion.bash`
- Place somewhere handy (example)
- Home directory
- Enable in `~/.bashrc`
- Add:
- `. ~/git-completion.bash`
- Use completion
- Inside a Git repository, type
- `git chec<tab>`
- Auto-completes to `git checkout`
- Completion coverage
- Git subcommands
- Command-line parameters
- Remotes and ref names (where appropriate)
### Customize prompt (`git-prompt.sh`)
- Motivation
- Show Git info in prompt
- Current branch
- Working directory status
- Setup
- Copy file from Git source:
- `contrib/completion/git-prompt.sh`
- Place in home directory
- Add to `~/.bashrc`
- `. ~/git-prompt.sh`
- `export GIT_PS1_SHOWDIRTYSTATE=1`
- `export PS1='\w$(__git_ps1 " (%s)")\$ '`
- Meaning of prompt pieces
- `\w`
- Print current working directory
- `\$`
- Print `$` part of the prompt
- `__git_ps1 " (%s)"`
- Calls function provided by `git-prompt.sh`
- Uses formatting argument `" (%s)"`
- Result
- Prompt shows Git context inside Git-controlled projects (Figure 158)
- Further info
- Both scripts have documentation
- Inspect `git-completion.bash`
- Inspect `git-prompt.sh`
## Git in Zsh
### Tab completion
- Enable
- Add to `~/.zshrc`
- `autoload -Uz compinit && compinit`
- Interface (more powerful than Bash)
- Example: `git che<tab>`
- Shows options with descriptions, e.g.
- `check-attr` — display gitattributes information
- `check-ref-format` — ensure a reference name is well formed
- `checkout` — checkout branch or paths to working tree
- `checkout-index` — copy files from index to working directory
- `cherry` — find commits not merged upstream
- `cherry-pick` — apply changes introduced by an existing commit
- Ambiguous completions
- Not just listed; include helpful descriptions
- Navigate list by repeatedly hitting tab
- Completion scope
- Git commands and arguments
- Refs/remotes and other repo-internal names
- Filenames and standard Zsh completions
### Prompt integration with `vcs_info`
- Built-in framework
- `vcs_info` provides VCS information for prompts
- Configure branch name in right prompt
- Add to `~/.zshrc`
- `autoload -Uz vcs_info`
- `precmd_vcs_info() { vcs_info }`
- `precmd_functions+=( precmd_vcs_info )`
- `setopt prompt_subst`
- `RPROMPT='${vcs_info_msg_0_}'`
- `# PROMPT='${vcs_info_msg_0_}%# '`
- `zstyle ':vcs_info:git:*' formats '%b'`
- Result
- Branch shown on right side inside Git repos (Figure 159)
- Left-side prompt also possible
- Uncomment the `PROMPT=...` line
- Documentation
- `vcs_info` docs in `zshcontrib(1)`
- Online:
- <http://zsh.sourceforge.net/Doc/Release/User-Contributions.html#Version-Control-Information>
### Alternative: Git’s `git-prompt.sh`
- Option
- Use Git’s `git-prompt.sh` instead of `vcs_info`
- Compatibility
- Works with Bash and Zsh
- Reference
- <https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh>
### Zsh framework: oh-my-zsh
- What it is
- A framework to enhance Zsh
- Location
- <https://github.com/robbyrussell/oh-my-zsh>
- Git-related value
- Powerful git tab completion via plugin system
- Many prompt “themes” showing version-control data
- Example theme shown (Figure 160)
## Git in PowerShell
### Why / what
- Limitation
- `cmd.exe` isn’t really capable of a customized Git experience
- If using PowerShell
- `posh-git` provides
- Powerful tab completion
- Enhanced prompt (repository status awareness)
- Works with PowerShell Core on Linux/macOS too
- Project: <https://github.com/dahlbyk/posh-git>
- Example shown (Figure 161)
### Installation
#### Prerequisites (Windows only): ExecutionPolicy
- Requirement
- Set local `ExecutionPolicy` to `RemoteSigned`
- Anything except `Undefined` and `Restricted`
- `AllSigned` vs `RemoteSigned`
- `AllSigned`
- Local scripts (your own) also need digital signatures
- `RemoteSigned`
- Only scripts with `ZoneIdentifier` = Internet (downloaded from web) need signatures
- Others do not
- Scope guidance
- Admin / all users
- `-Scope LocalMachine`
- Normal user / no admin rights
- `-Scope CurrentUser`
- References
- Scopes: <https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes>
- ExecutionPolicy: <https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy>
- Set `RemoteSigned` for all users
- `Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy RemoteSigned -Force`
#### Install via PowerShell Gallery
- When available
- PowerShell 5+, or PowerShell 4 with PackageManagement installed
- Reference
- <https://docs.microsoft.com/en-us/powershell/scripting/gallery/overview>
- Install commands (CurrentUser)
- `Install-Module posh-git -Scope CurrentUser -Force`
- `Install-Module posh-git -Scope CurrentUser -AllowPrerelease -Force`
- Newer beta with PowerShell Core support
- Install for all users
- Use `-Scope AllUsers`
- Run from elevated PowerShell console
- If install fails due to `PowerShellGet` error
- Run first:
- `Install-Module PowerShellGet -Force -SkipPublisherCheck`
- Then retry
- Reason given
- Built-in Windows PowerShell modules are signed with a different publishment certificate
#### Update PowerShell prompt (load automatically)
- Import now
- `Import-Module posh-git`
- Auto-import every time PowerShell starts
- `Add-PoshGitToProfile -AllHosts`
- Adds import statement to `$profile` script
- Note
- Multiple `$profile` scripts exist
- e.g., console profile vs ISE profile
#### Install from source
- Download a release
- <https://github.com/dahlbyk/posh-git/releases>
- Uncompress
- Import module via full path to `posh-git.psd1`
- `Import-Module <path-to-uncompress-folder>\src\posh-git.psd1`
- Add to profile
- `Add-PoshGitToProfile -AllHosts`
- Effect
- Adds proper line to `profile.ps1`
- Active next time PowerShell opens
#### Further references (prompt meaning & customization)
- Git status summary information
- <https://github.com/dahlbyk/posh-git/blob/master/README.md#git-status-summary-information>
- Prompt customization variables
- <https://github.com/dahlbyk/posh-git/blob/master/README.md#customization-variables>
## Appendix summary
- You can harness Git from within everyday tools (GUIs, IDEs, editors, shells)
- You can also access Git repositories from your own programs