DEV Community

Cover image for Git Cheat Sheet for Hacktoberfest
Mohammad-Ali A'RÂBI
Mohammad-Ali A'RÂBI

Posted on

Git Cheat Sheet for Hacktoberfest

Hacktoberfest is upon us. Here is a git cheat sheet that you can keep handy for your future hackings:

0. Git Configuration

Main article: Seven Git Configs to Set When Moving to a New Machine

When starting with git for the first time (on a new machine), there are a few configs you should set.

# set name and email
git config --global user.name "Mohammad-Ali A'râbi"
git config --global user.email "my-name-at-work@employer.com"

# set default branch name
git config --global init.defaultBranch master

# set rebase the default pull policy
git config --global pull.rebase true

# set to auto-stash upon rebase(/pull)
git config --global rebase.autoStash true

# set to default branch name upon push
git config --global push.default current
Enter fullscreen mode Exit fullscreen mode

For the default branch name, main is gaining more popularity replacing the good old master. Alternatives are trunk and develop, so feel free.

We will also get into stashing in a bit.

1. Basic Git Workflow

These commands are the bread and butter of working with Git. They help you interact with the repository and manage your working directory.

Cloning a Repository

Clone a remote repository to your local machine.

git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode

You can copy the repository URL from the repo's GitHub/GitLab page.

Check Repository Status

Check the status of your working directory.

git status
Enter fullscreen mode Exit fullscreen mode

This command shows which files are changes, which ones are added for committing, etc.

Adding Changes

Stage changes (add files to the staging area) before committing.

git add <file-name>
Enter fullscreen mode Exit fullscreen mode

I will skip the command for "staging all changes" as that's a bad practice.

Commit Changes

Main article: Ten Commandments of Git Commit Messages

After staging, commit changes with a meaningful message.

git commit -m "Commit message"
Enter fullscreen mode Exit fullscreen mode

Viewing Commit History

Check your commit history.

git log
Enter fullscreen mode Exit fullscreen mode

For a simpler, one-line format:

git log --oneline
Enter fullscreen mode Exit fullscreen mode

Push Changes to Remote

Push committed changes to the remote repository.

git push
Enter fullscreen mode Exit fullscreen mode

2. Branching and Merging

Main article: 7 Tips to Work with Branches on Git

Branching allows you to work on different parts of your project independently. Here's how to manage branches:

Create a New Branch

Create a new branch for your feature or bug fix.

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

If you just want to create the new branch and don't want to switch to it:

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

Switch to Another Branch

Main article: Git Commands to Use Instead of Checkout

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

List All Branches

See all branches in your repository.

git branch
Enter fullscreen mode Exit fullscreen mode

Merge a Branch

Merge changes from one 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 delete it.

# Locally
git branch -d <branch-name>

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

3. Stashing Changes

Stashing temporarily stores your changes without committing them.

Save Changes to a Stash

If you're working on something and want to switch branches without committing:

git stash
Enter fullscreen mode Exit fullscreen mode

Apply Stash

To retrieve your changes later:

git stash apply
Enter fullscreen mode Exit fullscreen mode

List Stashed Changes

If you have multiple stashes, you can see them with:

git stash list
Enter fullscreen mode Exit fullscreen mode

Final Words

The following articles should also be useful:

Top comments (12)

Collapse
 
0xwdg profile image
Wesley de Groot

master is a bit outdated, main is nowadays more used.

Since the name does not contain spaces or special characters it is not required to be wrapped in quotation marks (")

# set default branch name
git config --global init.defaultBranch main
Enter fullscreen mode Exit fullscreen mode
Collapse
 
aerabi profile image
Mohammad-Ali A'RÂBI

Thanks for the comment. I added a few lines explaining the fact you just mentioned. Also, the quotation mark were left originally for a better visibility through syntax highlight.

Collapse
 
thomasbnt profile image
Thomas Bnt ☕

Very cool post to preparing for the Hacktoberfest!

Collapse
 
aerabi profile image
Mohammad-Ali A'RÂBI

Thanks, Thomas. 😊
Looking forward to it. 🥳

Collapse
 
martinbaun profile image
Martin Baun • Edited

Great work! Nice design and amazing organization.

Would you consider adding git clean and git bisect?

Those are commands I use a lot :)

Collapse
 
aerabi profile image
Mohammad-Ali A'RÂBI

Thanks Martin. 😊
Definitely. Do you think it would be better if I add them to a follow-up article, or should I add them here?

Collapse
 
martinbaun profile image
Martin Baun

A follow up would be great!

Collapse
 
ujjwall-r profile image
Ujjwal Raj • Edited

This hacktoberfest, we are inviting contributors:

github.com/ujjwall-R/Dree

join our community group : join.slack.com/t/dreecommunity/sha...

Collapse
 
chideracode profile image
Chidera Humphrey

Nice read, Mohammad

We look forward to Hacktoberfest

Collapse
 
aerabi profile image
Mohammad-Ali A'RÂBI

Thanks, Chidera! 🫡
Yeah, me too.

Collapse
 
kin7777 profile image
kince marando

Good and and in simplicity way to recap on Git

Collapse
 
phil_enslin_035200a1266c4 profile image
Phil Enslin

Thank you. i am new to Git so this helps

Some comments may only be visible to logged-in visitors. Sign in to view all comments.