From basic operations like staging and committing changes to more advanced techniques for synchronizing with remote branches, this cheat sheet will help you to contribute effectively to any Git-based project π.
Setting Up and Cloning
Cloning a repository is often the first step in the Git workflow for a developer. It provides you with a local copy of the project including all branches and history. You can experiment and make commits locally without affecting the central repository.
git clone git@github.com:username/repository.git
Basic Operations
Checking Status π
git status
Purpose: Displays the state of the working directory and staging area.
Why It Matters: Lets you see which changes are staged, which are not, and which files aren't being tracked by Git.
Reviewing Changes π
git diff
Purpose: Shows the differences not yet staged.
Why It Matters: Before staging, you can review what changes are being made to ensure accuracy.
git restore <file>
Staging Changes π€
git add <file>
Purpose: Adds file changes to the staging area, preparing them for a commit.
Why It Matters: Enables selective staging of changes for better commit management.
git restore --staged <file>
. It is equivalent to using old git reset HEAD <file>
command.Committing Changes π¨
git commit -m "message"
Purpose: Saves your staged changes to the local repository along with a descriptive message.
Why It Matters: Captures a snapshot of your project's currently staged changes.
git revert <commit>
. However, if you want the previous commit to disappear from the history, do git reset --hard <commit>
.Viewing Commit History π
git log
Purpose: Shows the commit history for the current branch.
Why It Matters: Allows you to review changes and navigate your project's development history.
Branch Management
Listing Branches π³
git branch -a
Purpose: Lists all local and remote branches in your repository.
Why It Matters: Helps you see all the current branches and manage them.
Creating a Branch π±
git branch <branch-name>
Purpose: Creates a new branch.
Why It Matters: Allows parallel development by isolating work in separate branches.
Switching Branches π
git checkout <branch-name>
Purpose: Switches to another branch.
Why It Matters: Allows you to work on different parts of your project simultaneously.
git checkout -b <new-branch>
git stash
to temporarily save them and then git stash apply
to retrieve the most recent stashed changes.Merging Branches βοΈ
git merge <branch-name>
Purpose: Integrates changes from one branch into another.
Why It Matters: Combines separate lines of development, such as merging feature branches into the main branch.
Handling merge conflicts: Manually edit conflicted files, then
git add <file>
to stage the resolved files, andgit commit
to complete the merge.
Deleting a Branch ποΈ
git branch -D <branch-name>
Purpose: Deletes a local branch.
Why It Matters: Cleans up your repository by removing local branches that are no longer needed.
git fetch --all --prune
to clean up old remote branches.Synchronizing with Remote Repositories
Fetching Changes π
git fetch
Purpose: Downloads content from a remote repository without merging it into your local repository.
Why It Matters: Lets you review changes before integrating them into your branch.
Pulling Changes ‡οΈ
git pull
Purpose: Fetches changes from the remote repository and merges them into your current branch.
Why It Matters: Keeps your local repository up-to-date with the remote repository.
Pushing Changes ‴οΈ
git push origin <branch-name>
Purpose: Updates the remote repository with your local changes.
Why It Matters: Shares your contributions with the team and updates the project on the remote server.
Remember, the key to master Git is practice π€. The more you use these commands and experiment with them, the more comfortable you get with Git π§π»ββοΈ.