A.1 Git
This appendix provides a concise reference to essential Git concepts and commands, tailored for data analysts and researchers managing code and collaboration. For extended learning, explore the following resources:
- Git Cheat Sheet (PDF)
- Git Cheat Sheets in Other Languages
- Interactive Git Tutorial
- Visual Git Cheat Sheet
- Happy Git with R (for R Users)
A.1.1 Basic Setup
Configure your Git environment using the git config
command:
Set your name and email (used in commits):
Set your preferred text editor (e.g., for writing commit messages):
A.1.2 Creating a Repository
To create a new Git repository in your project directory:
This creates a .git
directory where Git stores all version control information.
A.1.3 Tracking Changes
Git tracks changes through a three-tier structure:
- Working Directory: your local folder with files.
- Staging Area: where you prepare changes before committing.
- Local Repository: stores committed snapshots of your code.
Common commands:
Check status:
Add files to the staging area:
Commit staged changes:
A.1.4 Viewing History and Changes
Show changes not yet staged:
Show committed changes:
Restore previous versions of files:
A.1.5 Ignoring Files
To prevent certain files from being tracked by Git, create a .gitignore
file. For example:
View contents using:
A.1.6 Remote Repositories
Git supports linking local and remote repositories (e.g., GitHub):
Add a remote:
Push changes to remote:
Pull changes from remote:
A.1.7 Collaboration
Clone a remote repository:
This creates a local copy and sets up a remote named
origin
.
A.1.8 Branching and Merging
Create and switch to a new branch:
Switch back to main branch:
Merge another branch into the current one:
A.1.9 Handling Conflicts
Merge conflicts occur when multiple changes affect the same lines of a file. Git will:
- Mark the conflict in the file.
- Require manual resolution before committing.
Always review and test code after resolving conflicts.
A.1.10 Licensing
Understanding software licensing is essential in open-source collaboration:
- GPL (General Public License): Requires derivative software to also be GPL-licensed.
- Creative Commons: Offers flexible combinations of attribution, sharing, and commercial use restrictions.
Choose licenses aligned with your intended use and contributions.