The Ultimate Git Cheat Sheet  for Collaboration🐱πŸ”₯

The Ultimate Git Cheat Sheet for Collaboration🐱πŸ”₯

Β·

4 min read

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.

πŸ—‘
If you wish to revert all changes in a file, use 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.

πŸ“₯
If you wish to unstage a file without loosing the changes made in it use 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.

πŸ”
You can undo the commit by 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.

πŸ’‘
You can create a new branch and switch there with one command: git checkout -b <new-branch>
⚠
Keep on mind that you can't switch branches with unsaved changes in the current branch. If you don't want to commit the files, you can use 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, and git 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.

🧹
Use 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 πŸ§˜πŸ»β€β™€οΈ.

Β