This cheat sheet covers the Git commands students need to create, save, inspect, share, branch, and repair project history. Git is important because it tracks changes in code and helps teams work on the same project safely. Students can use this reference while coding in class, submitting projects, or collaborating on group assignments.
The goal is to make common commands easy to find and understand without crowding the page.
Key Facts
- git init creates a new Git repository in the current folder.
- git clone URL copies an existing remote repository onto your computer.
- git status shows changed, staged, untracked, and clean files in the working directory.
- git add filename stages one file, and git add . stages all changed files in the current folder and its subfolders.
- git commit -m "message" saves the staged changes as a new snapshot with a short description.
- git branch branch-name creates a new branch, and git switch branch-name moves you to that branch.
- git pull downloads remote changes and merges them into your current branch, while git push uploads your local commits to the remote repository.
- git restore filename discards unstaged changes in a file, and git revert commit-id creates a new commit that undoes an earlier commit.
Vocabulary
- Repository
- A repository is a project folder that Git tracks, including its files and change history.
- Commit
- A commit is a saved snapshot of staged changes with an identifying hash and message.
- Staging area
- The staging area is the place where selected file changes wait before being committed.
- Branch
- A branch is a separate line of development used to work on features, fixes, or experiments.
- Merge
- A merge combines changes from one branch into another branch.
- Remote
- A remote is a version of a repository stored on another computer or service, such as GitHub.
Common Mistakes to Avoid
- Running git commit before git add is wrong because Git only commits changes that have been staged.
- Using git push before pulling team changes can be wrong because the remote branch may contain commits you do not have yet.
- Confusing git restore and git revert is risky because restore can discard local file changes, while revert safely creates a new undo commit.
- Committing unclear messages like "stuff" is a mistake because future readers cannot tell what changed or why.
- Working directly on the main branch for every task is a mistake because branches help keep experiments and unfinished features separate from stable code.
Practice Questions
- 1 A project has 5 changed files, and you run git add on 3 of them. How many files are staged for the next commit?
- 2 You make 2 commits locally, then your teammate pushes 4 commits to the remote branch. After you run git pull successfully, how many new teammate commits are added to your local branch?
- 3 Write the Git commands to create a branch named login-page, switch to it, stage all changes, and commit with the message "Add login page".
- 4 Why is git revert usually safer than deleting commits from shared project history?