Step-by-Step Guide to Git and GitHub Commands
This guide walks you through a typical Git workflow, explaining each command in the order you’d use it, its purpose, and why it’s necessary. Follow these steps to set up, manage, and collaborate on a project using Git and GitHub.
Step 1: Configure Your Git Identity
Why? Git needs to know who you are to attribute commits to you, which is essential for collaboration and tracking changes in a project.
- Command:
git config --global user.name "Your Name"
Purpose: Sets your name for all Git commits globally.
Example:git config --global user.name "Khurram Shahzad"
- Command:
git config --global user.email "your.email@example.com"
Purpose: Sets your email for all Git commits globally.
Example:git config --global user.email "kswork@gmail.com"
- Command:
git config --global user.name
orgit config --global user.email
Purpose: Displays your configured name or email to verify settings.
Step 2: Set Up SSH for GitHub
Why? An SSH key allows secure communication with GitHub, enabling you to push code without repeatedly entering your credentials.
- Command:
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
Purpose: Generates a new SSH key pair with RSA encryption and a 4096-bit length, tagged with your email.
Example:ssh-keygen -t rsa -b 4096 -C "kswork@gmail.com"
Note: After this, add the public key to GitHub (copy from~/.ssh/id_rsa.pub
).
Step 3: Initialize a New Repository
Why? This sets up a local Git repository to track your project’s files and changes.
- Command:
git init
Purpose: Initializes a new Git repository in the current directory.
Step 4: Add and Commit Changes
Why? You need to stage and commit your changes to save them in the repository’s history, creating a snapshot of your work.
- Command:
git add
Purpose: Stages a specific file for the next commit.
Example:git add index.js
- Command:
git add .
Purpose: Stages all modified and new files in the current directory for the next commit. - Command:
git commit -m "commit message"
Purpose: Commits staged changes with a descriptive message.
Example:git commit -m "initial code added"
Step 5: Check Status and Differences
Why? These commands help you understand the current state of your repository and review changes before committing.
- Command:
git status
Purpose: Shows the current status of the working directory (modified, staged, or untracked files). - Command:
git diff
Purpose: Shows changes between the working directory and the last commit (or staged changes).
Step 6: Connect to a Remote Repository
Why? Linking your local repository to a remote GitHub repository allows you to collaborate and store your code online.
- Command:
git remote -v
Purpose: Lists all remote repositories and their URLs to verify connections. - Command:
git push -u origin main
Purpose: Pushes the localmain
branch to theorigin
remote and sets it as the upstream branch.
Step 7: Create and Manage Branches
Why? Branches allow you to work on features, fixes, or experiments without affecting the main codebase.
- Command:
git branch
Purpose: Lists all branches, with the current branch marked by an asterisk. - Command:
git branch
Purpose: Creates a new branch with the specified name.
Example:git branch feat/new-feature
- Command:
git checkout
Purpose: Switches to the specified branch.
Example:git checkout feat/new-feature
- Command:
git push --set-upstream origin
Purpose: Pushes the local branch to theorigin
remote and sets it as the upstream branch.
Example:git push --set-upstream origin feat/new-feature
- Note: Use branch naming conventions like
feat
(features),bug
(bug fixes),junk
(experiments), orwip
(work-in-progress) for clarity in collaborative projects.
Step 8: Merge Branches
Why? Merging integrates changes from one branch (e.g., a feature branch) into another (e.g., main
) to combine work.
- Command:
git merge
Purpose: Merges the specified branch into the current branch.
Example:git merge feat/new-feature
Step 9: Stash Temporary Changes
Why? Stashing lets you save uncommitted changes temporarily to switch tasks (e.g., to fix a bug) without committing incomplete work.
- Command:
git stash
Purpose: Saves uncommitted changes and reverts to a clean working directory. - Command:
git stash apply
Purpose: Applies the most recent stashed changes without removing them from the stash. - Command:
git stash pop
Purpose: Applies the most recent stashed changes and removes them from the stash. - Command:
git stash drop
Purpose: Removes the most recent stash without applying it.
Step 10: Review and Undo Changes
Why? Reviewing commit history or undoing changes helps you debug issues or correct mistakes in your project.
- Command:
git log
Purpose: Displays the full commit history of the repository. - Command:
git log --oneline
Purpose: Shows a compact, one-line-per-commit history. - Command:
git blame
Purpose: Shows who last modified each line of a file and in which commit.
Example:git blame index.js
- Command:
git reset --hard
Purpose: Resets the current branch to the specified commit, discarding all subsequent changes.
Warning: Use cautiously, as it permanently deletes changes. - Command:
git revert
Purpose: Creates a new commit that undoes the changes of the specified commit without altering history. - Command:
git push -f
Purpose: Force-pushes the local branch to the remote, overwriting the remote branch.
Warning: Use with caution, as it can overwrite collaborators’ changes.
Summary
Following this sequence—configuring your identity, setting up SSH, initializing a repository, adding/committing changes, managing branches, and reviewing or undoing changes—covers the core Git workflow. These commands empower you to track, collaborate, and manage your codebase effectively. Have questions? Drop them in the comments!
0 Comments