Version control is a crucial part of modern software development, and Git is the most widely used version control system. GitHub, on the other hand, provides a cloud-based platform for hosting Git repositories.
This article covers essential Git and GitHub commands to help you manage your projects efficiently.
Want to learn Git + GitHub from Scratch? Check out my 2+ hours crash course.
1. Getting Started with Git
Before using Git, you need to install and configure it on your machine.
Commands:
Install Git: Download and install Git from git-scm.com.
Check Git version:
git --version
- Set up username and email (required for commits):
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
- View configuration settings:
git config --list
2. Git Basics: Local Repository Operations
These commands allow you to initialize a repository, add files, commit changes, and check history.
Commands:
- Initialize a new repository:
git init
- Check repository status:
git status
- Add files to the staging area:
git add <filename>
git add . # Add all files
- Commit staged files with a message:
git commit -m "Initial commit"
- View commit history:
git log
- Show details of a specific commit:
git show <commit-hash>
3. Working with Git Branches
Branches allow you to work on new features without affecting the main codebase.
Commands:
- Create a new branch:
git branch <branch-name>
- Switch to a branch:
git checkout <branch-name>
- Create and switch to a new branch:
git checkout -b <branch-name>
- List all branches:
git branch
- Merge another branch into the current branch:
git merge <branch-name>
- Delete a branch:
git branch -D <branch-name>
4. Introduction to GitHub
GitHub hosts Git repositories online, making collaboration easier.
Commands:
- Generate an SSH key for authentication:
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
- View and copy your SSH public key:
cat ~/.ssh/id_rsa.pub
- Connect Git to GitHub via HTTPS:
git remote add origin https://github.com/<your-username>/<repo-name>.git
- Connect Git to GitHub via SSH:
git remote add origin git@github.com:<your-username>/<repo-name>.git
- Check remote repository connections:
git remote -v
5. Interacting with GitHub
Collaboration involves pushing, pulling, and cloning repositories.
Commands:
- Pull the latest changes before pushing:
git pull origin <branch-name>
- Push local commits to GitHub:
git push origin <branch-name>
- Clone a remote repository:
git clone https://github.com/<user>/<repo>.git
- Clone a remote repository with a new name:
git clone https://github.com/<user>/<repo>.git newname
6. Undoing Changes and Managing History
These commands help revert changes and modify commit history.
Commands:
- Unstage a file:
git reset HEAD <file>
- Discard changes in a file:
git checkout -- <file>
- Reset to a previous commit:
git reset --hard <commit-hash>
- Revert a commit (without modifying history):
git revert <commit-hash>
- Modify the most recent commit message:
git commit --amend -m "Updated commit message"
- Stash uncommitted changes:
git stash
- Apply latest stashed changes:
git stash apply
- Apply specific stashed changes:
git stash list # See all stashed list
git stash apply stash@{1} # Apply a specific stash
7. Managing Contributions: Forks, Pull Requests, and Code Review
Commands:
- Clone a forked repository:
git clone https://github.com/<your-username>/<forked-repo>.git
- Push changes to your forked repository:
git push origin <branch-name>
8. Viewing File Changes
Commands:
- View unstaged changes:
git diff
- View staged changes:
git diff --staged
9. Ignoring Files using .gitignore
Commands:
- Create a .gitignore file:
touch .gitignore
10. Inspecting the Log of Commits
Commands:
- View commit history:
git log
- Compact commit history:
git log --oneline
11. Renaming Files in Git
Commands:
- Rename a file and track changes:
git mv old-file.txt new-file.txt
12. Advanced Git Features
Commands:
- Cherry-pick a specific commit:
git cherry-pick <commit-hash>
- Rebase a branch:
git rebase master
To understand the difference between merge and rebase with live demo check out the course.
- Create a new tag:
git tag v1.0
- Push tags to GitHub:
git push origin v1.0
With these Git and GitHub commands, you can manage version control efficiently and collaborate seamlessly with others. Mastering these tools will enhance your productivity as a developer.
Top comments (0)