This document provides an in-depth guide to advanced Git operations, including stashing changes, rewriting history, cherry-picking commits, tagging versions, working with remote repositories, and contributing to open-source projects. This guide is aimed at developers looking to enhance their Git workflow for better efficiency and collaboration.
echo "Temporary changes" >> temp-file.txt- Adds temporary content to a file.
git status- Displays the status of unstaged changes.
git stash- Saves changes to a stash and resets the working directory to the last commit.
git stash list- Shows a list of stashed changes.
git stash apply- Applies the most recent stash without removing it from the stash list.
git stash drop- Deletes the most recent stash entry.
git stash apply stash@{1}- Applies a specific stash by its identifier.
git stash -u- Stashes both tracked and untracked files.
git stash apply- Applies the stashed changes, including untracked files.
git rebase main- Moves the commits of the current branch on top of the
mainbranch.
git rebase --continue- Resolves conflicts and proceeds with the rebase process.
git rebase --abort- Cancels the rebase and restores the branch to its original state.
git log --oneline- Displays a compact commit history.
git rebase -i HEAD~3- Opens an interactive rebase session for the last 3 commits.
Modify commits by changing pick to:
squash(combine commits)reword(edit commit message)edit(modify commit content)drop(remove commit)
git rebase --continue- Completes the rebase after conflict resolution.
git log --oneline- Displays commit hashes and messages.
git cherry-pick <commit-hash>- Applies the specified commit to the current branch.
git cherry-pick --continue- Completes cherry-picking after conflict resolution.
git tag -a v1.0 -m "Initial release"- Creates an annotated tag with a message.
git tag- Displays all available tags.
git push origin v1.0- Pushes the specified tag to the remote repository.
git push --tags- Pushes all tags to the remote repository.
git remote add origin https://github.com/username/repo.git- Links a remote repository named
origin.
git fetch origin- Retrieves updates from the remote repository without merging.
git pull origin main- Fetches and merges changes from the remote
mainbranch.
git push origin main- Pushes local commits to the remote
mainbranch.
- Use the GitHub UI to fork a repository.
- Clone the forked repository:
git clone https://github.com/your-username/repo.git- Submit a pull request (PR) via GitHub after pushing your changes.
git remote add upstream https://github.com/original-owner/repo.git- Links the original repository as
upstream.
git fetch upstream- Fetches updates from the upstream repository.
git merge upstream/main- Merges updates into your local branch.
git push origin main- Pushes the updates to your forked repository.
- Stashing: Temporarily save and apply uncommitted changes.
- Rewriting History: Modify commit history using
git rebase. - Cherry-Picking: Apply specific commits from another branch.
- Tagging: Label commits for versioning.
- Remote Repositories: Manage remotes, pushing, pulling, and fetching changes.
- Forking & Contributing: Collaborate effectively in open-source projects.
🎯 Master these Git techniques to boost your development workflow! 🚀