๐ ๐๐ถ๐ ๐๐ต๐ฒ๐ฎ๐ ๐ฆ๐ต๐ฒ๐ฒ๐ ๐ณ๐ผ๐ฟ ๐๐ฎ๐ฐ๐ธ๐๐ผ๐ฏ๐ฒ๐ฟ๐ณ๐ฒ๐๐ 2024! ๐
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! ๐
Feel free to make any further adjustments or let me know if there's anything else you'd like to modify!
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.