๐ŸŽ‰ ๐—š๐—ถ๐˜ ๐—–๐—ต๐—ฒ๐—ฎ๐˜ ๐—ฆ๐—ต๐—ฒ๐—ฒ๐˜ ๐—ณ๐—ผ๐—ฟ ๐—›๐—ฎ๐—ฐ๐—ธ๐˜๐—ผ๐—ฏ๐—ฒ๐—ฟ๐—ณ๐—ฒ๐˜€๐˜ 2024! ๐ŸŽ‰

HimanshuHimanshu
3 min read

Hacktoberfest is in full swing! Whether you're new to Git or a seasoned contributor, managing your Git workflow can sometimes be challenging. But don't worryโ€”Iโ€™m here to help!

๐Ÿ’ก Here's a handy Git Cheat Sheet to streamline your open-source contributions. ๐Ÿ’ช

1. Git Configuration

- Set Your Name and Email

git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"

- Set the Default Branch Name

git config --global init.defaultBranch master

While many developers are switching to main, options like master, trunk, or develop are also common.

- Set the Default Pull Policy to Rebase

git config --global pull.rebase true

- Enable Auto-Stashing During Rebase

git config --global rebase.autoStash true

This automatically stashes local changes during a rebase and reapplies them afterward.

- Set Push Behavior to the Current Branch

git config --global push.default current

This ensures that git push only pushes the current branch.

2. Basic Git Workflow

These fundamental Git commands are essential for everyday repository management:

- Cloning a Repository Copy a remote repository to your local machine:

git clone <repository-url>

Find the repository URL on the GitHub or GitLab page of the repository you want to clone.

- Check Repository Status Check the current state of your working directory:

git status

This shows which files have changes, which are staged, and which are ready for commit.

- Adding Changes Stage changes by adding files to the staging area:

git add <file-name>

Avoid using git add . to stage everything; it's more precise to add specific files.

- Commit Changes After staging changes, commit them with a meaningful message:

git commit -m "Your commit message"

For tips on writing effective commit messages, check out resources like "Ten Commandments of Git Commit Messages."

- Viewing Commit History To see a log of your commits:

git log

For a simpler, one-line summary of each commit:

git log --oneline

- Push Changes to Remote Send your committed changes to the remote repository:

git push

3. Branching and Merging

Branching allows you to work on different features or bug fixes independently. Hereโ€™s how to handle branches effectively:

- Create a New Branch To create and switch to a new branch:

git switch -c <branch-name>

Or, if you just want to create the branch without switching:

git branch <branch-name>

- Switch to Another Branch To change to a different branch:

git switch <branch-name>

- List All Branches View all branches in your repository:

git branch

- Merge a Branch Integrate changes from another branch into your current branch:

git merge <branch-name>

- Delete a Branch Once a branch is no longer needed, you can remove it:

Locally:

git branch -d <branch-name>

Remotely:

git push origin --delete <branch-name>

4. Stashing Changes

Stashing is useful for temporarily saving changes that you donโ€™t want to commit yet:

- Save Changes to a Stash If you need to switch branches but arenโ€™t ready to commit your work:

git stash

- Apply Stash To reapply the stashed changes later:

git stash apply

- List Stashed Changes If you have multiple stashes, view them with:

git stash list

By following these tips and using the commands effectively, you can manage your Git workflow more efficiently and keep your repository clean and organized. Happy coding! ๐Ÿš€

GitHub LinkedIn Portfolio


Feel free to make any further adjustments or let me know if there's anything else you'd like to modify!

1
Subscribe to my newsletter

Read articles from Himanshu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Himanshu
Himanshu

As a Software Engineer, I am passionate about staying up-to-date with the latest technologies in the tech field. I am committed to upskilling myself through ongoing learning and development.