Hacktoberfest is in full swing, and whether youโre a Git
newbie or a seasoned contributor, we all know managing your Git workflow can be tricky sometimes. But donโt worryโIโve got your back!
๐ก Hereโs a handy Git Cheat Sheet to make your open-source contributions smoother and faster. ๐ช
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
Many developers are switching to main
as the default branch name, but master
, trunk
, or develop are also common options.
- 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 re-applies them afterward.
- Set Push Behavior to the Current Branch
git config --global push.default current
This will ensure that git push
only pushes the current branch.
2. Basic Git Workflow
These are the fundamental Git commands you'll use for everyday repository management:
- Cloning a Repository
To copy a remote repository to your local machine:
git clone <repository-url>
You can 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>
It's generally best to avoid using git add .
to stage everything, as 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 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! ๐
Top comments (0)