DEV Community

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

Posted on

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

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"
Enter fullscreen mode Exit fullscreen mode

- Set the Default Branch Name

git config --global init.defaultBranch master
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

- Enable Auto-Stashing During Rebase

git config --global rebase.autoStash true
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

git log --oneline
Enter fullscreen mode Exit fullscreen mode

- Push Changes to Remote

Send your committed changes to the remote repository:

git push
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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

git branch <branch-name>
Enter fullscreen mode Exit fullscreen mode

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

git switch <branch-name>
Enter fullscreen mode Exit fullscreen mode

- List All Branches

View all branches in your repository:

git branch
Enter fullscreen mode Exit fullscreen mode

- Merge a Branch

Integrate changes from another branch into your current branch:

git merge <branch-name>
Enter fullscreen mode Exit fullscreen mode

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

Locally:

git branch -d <branch-name>
Enter fullscreen mode Exit fullscreen mode

Remotely:

git push origin --delete <branch-name>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

- Apply Stash
To reapply the stashed changes later:

git stash apply
Enter fullscreen mode Exit fullscreen mode

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

git stash list
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)