Here’s a concise guide to the main Git commands and what they do. These are essential for version control and collaboration in software development:
Setup & Configuration
- git config –global user.name “Your Name”
Set your Git username. - git config –global user.email “you@example.com“
Set your Git email.
Repository Management
- git init
Initialize a new Git repository. - git clone <url>
Copy a remote repository to your local machine.
Tracking Changes
- git status
Show the current state of the working directory and staging area. - git add <file>
Stage changes for commit. - git add .
Stage all changes in the current directory. - git commit -m “message”
Save staged changes with a descriptive message.
Branching & Merging
- git branch
List all branches. - git branch <name>
Create a new branch. - git checkout <branch>
Switch to another branch. - git merge <branch>
Merge changes from another branch into the current one.
Working with Remotes
- git remote -v
View remote repository URLs. - git push
Upload local commits to a remote repository. - git pull
Fetch and merge changes from a remote repository. - git fetch
Download changes from a remote repository (without merging).
Viewing History
- git log
View commit history. - git diff
Show changes between commits, branches, or files. - git show <commit>
Show details of a specific commit.
Undoing Changes
- git reset <file>
Unstage a file. - git checkout — <file>
Discard changes in a file. - git revert <commit>
Create a new commit that undoes a previous one.
Would you like a printable cheat sheet or a visual diagram to go with this?
In Git, “staged” refers to files that have been marked to be included in the next commit. It’s part of Git’s three-stage workflow:
Git Workflow States
- Working Directory\ This is where you make changes to your files. These changes are not yet tracked by Git for committing.
- Staging Area (Index)\ When you run git add <file>, Git moves the file to the staging area. This means you’re telling Git:\ “I want to include this change in the next snapshot (commit).”
- Repository (Commit History)\ When you run git commit, Git takes everything in the staging area and saves it permanently in the repository as a new commit.
Example Workflow
# You modify a file
nano hello.py
# You stage the file
git add hello.py
# You commit the staged file
git commit -m “Add greeting script”
Only files that are staged will be included in the commit. If you modify other files but don’t stage them, they won’t be part of that commit.