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

Mohammad-Ali A'RÂBI @aerabi

About: Backend Engineer @ JobRad | Docker Captain | Author of Docker & Kubernetes Security | Container Security Expert

Location:
Freiburg im Breisgau, DE
Joined:
Jun 25, 2020

Git Cheat Sheet for Hacktoberfest

Publish Date: Sep 23 '24
101 13

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:

Comments 13 total

  • Wesley de Groot
    Wesley de GrootSep 24, 2024

    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
    • Mohammad-Ali A'RÂBI
      Mohammad-Ali A'RÂBISep 24, 2024

      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.

  • Ujjwal Raj
    Ujjwal RajSep 24, 2024

    This hacktoberfest, we are inviting contributors:

    github.com/ujjwall-R/Dree

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

  • kince marando
    kince marandoSep 25, 2024

    Good and and in simplicity way to recap on Git

  • Thomas Bnt
    Thomas BntSep 25, 2024

    Very cool post to preparing for the Hacktoberfest!

  • Martin Baun
    Martin BaunSep 25, 2024

    Great work! Nice design and amazing organization.

    Would you consider adding git clean and git bisect?

    Those are commands I use a lot :)

    • Mohammad-Ali A'RÂBI
      Mohammad-Ali A'RÂBISep 25, 2024

      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?

  • Chidera Humphrey
    Chidera HumphreySep 26, 2024

    Nice read, Mohammad

    We look forward to Hacktoberfest

  • Tonton Drama
    Tonton DramaSep 30, 2024

    Tonton Kepala Bergetar Full Episod hd quality video Dramas free Online Downloads
    Kepala Bergetar offers DFM2U which is a Malaysian tv channel to watch malay dramas.
    Tonton Drama

  • Phil Enslin
    Phil EnslinOct 16, 2024

    Thank you. i am new to Git so this helps

Add comment