Git and GitHub Beginner Guide
A safe, modern introduction to repositories, commits, branches, remotes, pull requests, authentication, and undoing mistakes.
Liangchao DengPostdoctoral ResearcherProject overview
Git records changes to files on your computer. GitHub hosts Git repositories and adds collaboration features such as pull requests, issues, and automated checks. This guide follows a complete first workflow and separates safe recovery commands from history-rewriting operations.
1. Install and identify yourself
Install Git
-
Windows: download Git for Windows.
-
macOS: install the Xcode Command Line Tools or use Homebrew:
brew install git -
Ubuntu or Debian:
sudo apt updatesudo apt install git
Confirm the installation:
git --version
Configure commit identity
Use the name you want shown in commit history. The email should be an address associated with your GitHub account, or your GitHub-provided private noreply address.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
Review the configuration:
git config --global --list
2. Create a repository
Create a folder locally:
mkdir my-project
cd my-project
git init
Add a minimal project description:
echo "# My Project" > README.md
git status
git add README.md
git commit -m "docs: add project overview"
Before staging data, credentials, or generated files, create a .gitignore:
.env
node_modules/
__pycache__/
*.log
Never commit API keys or passwords. Removing a secret in a later commit does not remove it from earlier history; rotate an exposed credential immediately.
3. Connect the GitHub remote
Create an empty repository on GitHub without initializing another README, then connect it:
git branch -M main
git remote add origin https://github.com/your-username/repository-name.git
git remote -v
git push -u origin main
GitHub does not accept an account password for Git operations over HTTPS. Use a supported credential manager, GitHub CLI, a personal access token, or SSH authentication.
For an existing repository:
git clone https://github.com/your-username/repository-name.git
cd repository-name
4. The everyday edit cycle
Inspect changes before staging:
git status
git diff
Stage intentionally and commit a coherent unit:
git add path/to/file
git diff --staged
git commit -m "feat: describe the change"
git push
Prefer specific paths over git add . when a workspace contains unrelated or generated changes.
Before starting new work on a shared branch:
git fetch origin
git status
git pull --ff-only origin main
--ff-only refuses to create an unexpected merge commit. If local and remote histories have diverged, inspect them and decide whether to rebase or merge rather than forcing the operation.
5. Work on a branch
Create and switch to a feature branch:
git switch -c feature-clear-name
Commit and publish it:
git add path/to/file
git commit -m "feat: add clear capability"
git push -u origin feature-clear-name
After review, return to the default branch and update it:
git switch main
git pull --ff-only origin main
Delete a fully merged local branch:
git branch -d feature-clear-name
6. Forks and pull requests
Use a fork when you do not have permission to push branches to the upstream repository.
-
Open the upstream repository and select Fork.
-
Clone your fork, not the original repository:
git clone https://github.com/your-username/forked-repository.gitcd forked-repository -
Add the original repository as
upstream:git remote add upstream https://github.com/original-owner/repository.gitgit fetch upstream -
Create a branch, commit, and push it to your fork:
git switch -c analysis-updategit add path/to/filegit commit -m "feat: update analysis"git push -u origin analysis-update -
On GitHub, open a pull request from the fork branch to the upstream default branch.
To synchronize later:
git switch main
git fetch upstream
git merge --ff-only upstream/main
git push origin main
7. Undo changes safely
Choose the command based on where the mistake exists.
Discard an unstaged file edit
git restore path/to/file
This permanently replaces the working copy with the last committed version.
Unstage a file but keep its edits
git restore --staged path/to/file
Correct the most recent local commit
If it has not been pushed:
git add path/to/fix
git commit --amend
Reverse a published commit
Create a new commit that reverses the selected commit:
git log --oneline
git revert <commit-hash>
git reset --hard discards local work, and force-pushing rewrites shared history. They are not routine beginner recovery tools. Make a backup branch and confirm the collaboration policy before using either.
8. Useful inspection commands
| Command | Purpose |
|---|---|
git status | Show branch and working-tree state |
git diff | Show unstaged changes |
git diff --staged | Show staged changes |
git log --oneline --graph --decorate --all | Inspect branch history |
git remote -v | Show remote names and URLs |
git branch -vv | Show branches and their upstreams |
git show <commit> | Inspect one commit |
Final checklist
Before pushing:
- no secrets or private data are staged;
- generated files are excluded unless intentionally versioned;
- the diff matches the commit message;
- tests or builds relevant to the change pass;
- the destination branch and remote are correct.
For more detail, use the official Git documentation and GitHub Docs.
DISCUSSION
Questions or field notes?