1. Initializing a Git Repository
git init
Initializes a new Git repository in the current directory.git init
git clone [repository URL]
Clones a remote Git repository to your local machine.git clone https://github.com/user/repo.git
2. Basic Snapshotting (Staging & Committing)
git status
Shows the current status of your working directory (tracked files, untracked files, etc.).git status
git add [file]
Adds changes to the staging area.git add file.txt
git add .
Stages all changes in the current directory.git add .
git commit -m "[message]"
Commits staged changes with a descriptive message.git commit -m "Initial commit"
git commit --amend
Modifies the previous commit message or adds changes to the last commit.git commit --amend -m "Updated commit message"
3. Branching & Merging
git branch [branch-name]
Creates a new branch.git branch feature-branch
git checkout [branch-name]
Switches to a different branch.git checkout feature-branch
git checkout -b [branch-name]
Creates a new branch and switches to it immediately.git checkout -b new-feature
git merge [branch-name]
Merges changes from the specified branch into the current branch.git merge feature-branch
git branch -d [branch-name]
Deletes a local branch that has been merged.git branch -d feature-branch
git branch -D [branch-name]
Force deletes a branch (even if unmerged).git branch -D old-branch
4. Working with Remotes (GitHub)
git remote add origin [repository URL]
Links your local repository to a remote repository.git remote add origin https://github.com/user/repo.git
git push origin [branch-name]
Pushes the current branch to the remote repository.git push origin main
git pull origin [branch-name]
Fetches and merges changes from the remote branch to your current branch.git pull origin main
git push origin --delete [branch-name]
Deletes a branch on the remote repository.git push origin --delete feature-branch
5. Undoing Changes
git reset --soft [commit-hash]
Resets the current branch to a specific commit but keeps changes in the working directory.git reset --soft abc123
git reset --hard [commit-hash]
Resets the branch to a specific commit and discards all changes.git reset --hard abc123
git revert [commit-hash]
Creates a new commit that undoes the changes made in the specified commit.git revert abc123
git checkout -- [file]
Discards changes in a file and reverts it to the last commit version.git checkout -- file.txt
git stash
Temporarily saves (stashes) your local changes without committing.git stash
git stash apply
Re-applies stashed changes after they’ve been saved.git stash apply
6. Viewing History and Differences
git log
Displays the commit history for the current branch.git log
git log --oneline
Shows a condensed version of the commit history.git log --oneline
git diff
Shows the differences between your working directory and the last commit.git diff
git blame [file]
Shows who modified each line of a file.git blame file.txt
7. Working with Tags
git tag [tag-name]
Creates a new tag at the current commit.git tag v1.0.0
git tag -a [tag-name] -m "[message]"
Creates an annotated tag with a message.git tag -a v1.0.0 -m "Version 1.0.0 release"
git push origin [tag-name]
Pushes the tag to the remote repository.git push origin v1.0.0
git push origin --tags
Pushes all tags to the remote repository.git push origin --tags
8. Collaboration on GitHub
git fork
(via GitHub UI)
Create a personal copy of someone else’s repository to your GitHub account.git pull request
(via GitHub UI)
Propose changes from your branch into another repository (or branch). Used for code reviews and collaboration.
9. Advanced Git Commands
git rebase [branch-name]
Reapplies commits from your current branch on top of another branch’s history, resulting in a cleaner, linear history.git rebase main
git cherry-pick [commit-hash]
Applies a specific commit from one branch to another.git cherry-pick abc123
git reflog
Shows a log of all changes (including resets, checkouts, etc.) made to the HEAD in your local repository.git reflog
git submodule add [repository URL] [path]
Adds a submodule (a separate repository) inside your current project.git submodule add https://github.com/user/repo.git submodules/repo
10. Handling Conflicts
git merge [branch-name]
Merges changes from another branch into the current branch. If there are conflicts, Git will notify you to resolve them manually.git merge feature-branch
Resolving Merge Conflicts:
When Git finds conflicting changes during a merge, it marks the conflicting sections in your files, which look something like this:<<<<<<< HEAD Your changes ======= Incoming changes >>>>>>> feature-branch
After manually resolving the conflicts, you add the resolved files and commit the changes:
git add conflicted-file.txt git commit -m "Resolved merge conflict"
git mergetool
Launches a visual merge conflict resolution tool (if one is set up) to help resolve conflicts.git mergetool
11. Tracking and Managing Changes
git show [commit-hash]
Displays the details and changes of a specific commit.git show abc123
git shortlog
Summarizes git log output by author and shows how many commits each author has contributed.git shortlog
git log --author="[name]"
Filters the commit history by a specific author.git log --author="John Doe"
git diff [branch-name]
Compares the current branch’s differences with another branch.git diff main
12. Ignoring Files
.gitignore
Create a.gitignore
file to specify which files or directories Git should ignore. For example:# Ignore node_modules folder node_modules/ # Ignore all log files *.log
git rm -r --cached [file/directory]
Removes a file or directory from the staging area (but leaves it on your disk). Useful when you accidentally added a file that should be in.gitignore
.git rm -r --cached node_modules
13. Collaboration on GitHub (Pull Requests & Reviews)
Forking a Repository (via GitHub UI)
Use the Fork button on GitHub to create a copy of another user’s repository into your own account. This is useful for contributing to open-source projects.Creating a Pull Request (via GitHub UI)
After pushing your changes to a branch, create a Pull Request (PR) from your branch into the original project. In the Pull Request, other developers can review and suggest improvements before merging.Assigning Reviewers (via GitHub UI)
When you create a PR, you can assign team members to review your changes, ensuring that the code meets the project’s standards before it is merged.
14. GitHub Actions (Automation)
Creating a GitHub Actions Workflow:
You can create automated workflows in GitHub using GitHub Actions. Here's how to set up a basic CI/CD pipeline:Create a file at
.github/workflows/ci.yml
.Add the following example content to automatically run tests when you push code:
name: CI on: push: branches: - main pull_request: branches: - main jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Run tests run: npm test
Triggering GitHub Actions Manually:
You can trigger actions manually from the GitHub UI for tasks like deployments or maintenance workflows. This is helpful for one-time or scheduled tasks.
15. Tagging for Releases
git tag -a [tag-name] -m "[message]"
Creates an annotated tag for marking important releases or versions.git tag -a v2.0.0 -m "Version 2.0.0 release"
git push origin [tag-name]
Pushes a tag to the remote repository (e.g., pushing a release tag).git push origin v2.0.0
git push origin --tags
Pushes all local tags to the remote repository.git push origin --tags
Deleting a Tag:
Locally:
git tag -d [tag-name]
Remotely:
git push origin --delete [tag-name]
16. Rebasing
git rebase [branch-name]
Reapplies commits from your current branch on top of another branch’s commits. This is useful for keeping a clean, linear commit history.git rebase main
git rebase --interactive [commit-hash]
Allows you to interactively edit commit history. You can squash, reorder, or modify commit messages.git rebase -i HEAD~3
17. Stashing and Temporary Changes
git stash
Temporarily saves your changes without committing them. This is helpful when you need to switch branches but don't want to commit unfinished work.git stash
git stash list
Shows a list of all stashes that you have saved.git stash list
git stash pop
Applies the most recent stash and removes it from the stash list.git stash pop
git stash apply
Applies a stash but keeps it in the stash list, allowing you to reapply it later.git stash apply
git stash drop
Deletes a specific stash from the stash list.git stash drop stash@{0}
18. Submodules
git submodule add [repository-url] [path]
Adds a Git submodule to your repository. Submodules are external Git repositories included in your project.git submodule add https://github.com/user/repo.git submodules/repo
git submodule update --init --recursive
Updates all submodules to match the versions specified in the main repository and initializes any that are not already set up.git submodule update --init --recursive
git submodule foreach git pull origin master
Runs a command in each submodule. For example, this updates all submodules.git submodule foreach git pull origin main
19. Recovering and Fixing Mistakes
git reflog
Shows a log of where HEAD has been, including things like resets and branch checkouts. It’s useful for recovering lost commits.git reflog
git reset [commit-hash]
Moves the current branch to the specified commit. You can use--soft
to keep changes or--hard
to discard them.git reset --soft abc123
git checkout [commit-hash] -- [file]
Reverts a file back to its state in a previous commit without affecting other files.git checkout abc123 -- file.txt
20. Collaboration and Teamwork
git fetch
Downloads changes from the remote repository but doesn’t merge them into your current branch.git fetch origin
git pull --rebase
Fetches changes and then rebases your local commits on top of the fetched changes. This avoids unnecessary merge commits.git pull --rebase origin main
git blame [file]
Shows who last modified each line of a file, useful for tracking changes and understanding the context of code.git blame file.txt