github demo
This project is maintained by shanptom
I used to save multiple copies of files like:
NMDS_trial_v1.R
NMDS_trial_v2.R
NMDS_working_Final.R
This quickly becomes messy—it’s hard to track changes, and files take up unnecessary space. Instead of creating multiple files, saving multiple versions inside a single file (like versions in google docs) makes folders clutter free and accessing different versions easier. Version Control Systems enable storing multiple versions inside a single file and among the many Version Control Systems , Git is the most popular.
Git is a free, open-source version control system that:
_final_final
naming chaos!).Instead of manually saving copies, Git keeps a complete history of modifications, so you can always go back in time.
Git is preinstalled on macOS and Linux, so you can start using it right away.
If you’re on Windows, follow these steps:
Then, type:
git --version
If you see a version number (e.g., git version 2.40.1
), Git is ready to use!
After installing Git, follow these steps to start tracking changes in your project:
cd /path/to/your/project
git init
.git
folder that stores all version history.Exclude Unwanted Files (Optional) Some files (like large datasets, logs, or sensitive info) shouldn’t be tracked. To ignore them:
Create a .gitignore
file:
List files/folders to exclude (one per line):
# Example .gitignore file
credentials.txt
raw_data/
*.log
.env
.gitignore
in your project’s root folder.Git will now skip these files when tracking changes.
Verify Setup
git status
.gitignore
) will appear in red.Now your project is version-controlled!
Git doesn’t track files automatically—you must stage them first.
You create a new file, Scripts.R
, containing R code. To start tracking it run:
git add Scripts.R
Staging Multiple Files at Once
To stage all new/modified files in the folder, run:
git add .
git add file1.R file2.py
git add "file name.R"
A commit saves a snapshot of your project at a specific time.
How to Commit
After staging files, save the version with:
git commit -m "Initial script with basic data cleaning"
-m
lets you add a short commit message (required).Example Workflow
Scripts.R
you introduced a bug.git log
d3b4a1c....
) and restore it:git checkout d3b4a1c Scripts.R
Stage changes → git add <file>
Save snapshots → git commit -m "message"
Recover old versions → git log
+ git checkout <commit>
This workflow keeps your project organized and safe from accidental errors.
While Git tracks changes locally, GitHub serves as a centralized cloud platform for:
push
those commits to GitHub, making them available to your team.pull
the latest version to their local machines.GitHub requires a PAT (instead of passwords) for secure Git connections.
To create one:
Profile > Settings > Developer Settings > Personal Access Tokens > Generate New Token
repo
(full control of private repositories).git config --global user.name "YourGitHubUsername"
git config --global user.email "your.email@example.com"
Now that you have a local Git repository and a GitHub account, let’s upload your project to GitHub for collaboration and backup.
my-project
)README.md
, .gitignore
, or license (since your local repo already exists).https://github.com/your-username/my-project.git
)git remote add origin https://github.com/your-username/my-project.git
Makes sure you are running it from your local project directory
git add .
(Or stage specific files with git add file1.txt file2.R
)
git commit -m "Initial upload: data cleaning scripts"
git push -u origin main
-u
sets origin main
as the default remote branch (so next time, just git push
will work).git remote remove origin
first, then re-add.Your local files are now on GitHub.
Teammates can git clone
your repo to contribute.
Future changes can be pushed with just git push
.
Your project is now backed up, shareable, and ready for collaboration!
Forking creates your personal copy of someone else’s repository on GitHub. This allows you to:
Now you have an independent copy under your GitHub account!
To get the code on your local machine:
git clone https://github.com/your-username/forked-repo.git
cd forked-repo
This downloads the repository to your computer.
To sync with the original repository:
git remote add upstream https://github.com/original-owner/original-repo.git
git fetch upstream
git merge upstream/main
If you’ve made improvements:
git push origin main
Cloning downloads a full copy of a GitHub repository (including all files, branches, and commit history) to your local computer. This lets you:
On GitHub:
https://github.com/user/repo.git
)In your terminal (Git Bash, Terminal, or CMD), run:
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git my-folder
cd my-folder
Once cloned, you can:
git add .
git commit -m "Your message"
git push origin main
If you don’t have direct write access (common in open-source projects):
Cloning is different from forking:
Always git pull
before working to get the latest changes from teammates!
git pull
to Sync Remote ChangesWhen collaborating on shared repositories:
git push
until you sync these changes to avoid conflictsgit pull
performs two actions:
git pull
! [rejected] main -> main (non-fast-forward)
error: failed to push some refs...
hint: Updates were rejected because the remote contains work you don't have locally
git pull origin main
origin
= Remote repository name (default)main
= Branch nameIf remote changes conflict with your local changes:
git add <file>
git commit -m "Resolved merge conflicts"
git pull && git push
git pull --rebase
for cleaner history (advanced)Your Local: A -- B -- C
Remote: A -- B -- D -- E
After git pull:
Your Local: A -- B -- C -- D -- E
Git branches allow you to experiment with changes in a project without affecting the original (main) version. Each branch acts as an independent workspace where you can modify files, test new approaches, and refine your work before deciding whether to incorporate those changes into the primary codebase.
Suppose you are working on a dataset and want to compare two different analysis techniques:
main
branch (or master
in older repos) contains the original, stable version of your project.main
, ensuring a reliable baseline.git checkout -b analysis_method1
to create and switch to a new branch.main
.main
(git checkout main
) to ensure a clean starting point.git checkout -b analysis_method2
.main
(e.g., git merge analysis_method1
).analysis_method2
) can be kept for reference or discarded.git branch
→ Lists all branches.git checkout -b <branch_name>
→ Creates and switches to a new branch.git push origin <branch_name>
→ Uploads the branch to a remote repository (e.g., GitHub).git merge <branch_name>
→ Combines the specified branch into your current branch (e.g., main
).main
remains unaffected.By using branches, you ensure a structured, reversible, and collaborative workflow—especially useful in data science, software development, and research.
git merge
Merging combines changes from different branches into one. Common scenarios:
main
git checkout main
git pull origin main
git merge feature-branch
When Git can’t automatically combine changes:
<<<<<<< HEAD
Your local changes
=======
Incoming changes
>>>>>>> feature-branch
git add resolved-file.txt
git commit
Always merge into the target branch (checkout main first)
Test merged code before pushing
Delete merged branches to keep repo clean:
git branch -d feature-branch
ABC feature-branch
/ \
main DE--F (merge commit)
git stash
Assume you’re working on Analysis Step 3, but a collaborator wants you to fix to something in Analysis Step 2 and need updated file:
git stash
Stashing lets you temporarily shelve uncommitted changes so you can:
git stash push -m "WIP: Analysis Step 3"
-m
adds a descriptive message (recommended)git stash list # Shows all stashes
git status # Shows clean working directory
git checkout main
# Switch to correct branch and make changes for Analysis Step 2
git add .
git commit -m "Fixed analysis step 2"
git push origin main
git stash pop # Restores most recent stash AND removes it from stash list
Alternative:
git stash apply # Keeps stash in list for reuse
Clean context switching - No half-finished code in commits
Emergency fix readiness - Handle urgent requests without losing progress
Multi-task management - Juggle multiple features/bugs simultaneously
R Studio has built-in Git GUI window for streamlined workflows. If you already set up git in your project directory , choose Existing directory
and proceed. If you want to clone a GitHub repo, select Version Control.
Select Git
Go to GitHub and copy the the url for GitHub repo.
The new project will have Git Window enabled. If you open a git enabled project directory, it will automatically enable the Git window.
Open the file
Once you saved the modified file, it will appear on Git window.
Click the checkbox to stage files (=git add
)
Then click commit (= git commit
)
Add the commit message on the pop up window.
Click push to send changes to GitHub
# Git Cheat Sheet: Essential Commands
## Setup & Configuration
```sh
git config --global user.name "Your Name" # Set username
git config --global user.email "your@email.com" # Set email
git config --list # View settings
git init # Initialize new repo
git clone <repo-url> # Clone existing repo
git status # Show working directory status
git add <file> # Stage specific file
git add . # Stage all changes
git commit -m "Commit message" # Commit staged changes
git commit --amend # Edit last commit
git branch # List branches
git branch <name> # Create new branch
git checkout <branch> # Switch branches
git switch # modern version of checkout
git checkout -b <new-branch> # Create & switch to branch
git merge <branch> # Merge branch into current
git remote add origin <url> # Add remote repository
git push -u origin <branch> # Push branch to remote
git pull origin <branch> # Pull remote changes
git fetch # Download objects without merging
git restore <file> # Discard unstaged changes
git reset <file> # Unstage file
git reset --hard HEAD # Discard all local changes
git revert <commit-hash> # Create undo commit
git stash # Stash current changes
git stash list # List stashes
git stash pop # Apply most recent stash
git stash apply stash@{n} # Apply specific stash
git log # Show commit history
git log --oneline # Compact history
git log --graph # Visual branch history
git diff # Show unstaged changes
git fork # Fork a repo (GitHub web UI)
git pull-request # Create PR (GitHub CLI)
git cherry-pick <commit> # Apply specific commit
Create .gitignore
file to exclude:
Pro Tip: Use git <command> --help
for detailed documentation on any command.