Don’t hesitate to contact us if you have any feedback.

Git – My cheatsheet

Git is a powerful tool for version control, widely used by developers to manage code changes collaboratively.
Whether you’re working on a solo project or as part of a team, mastering a few essential Git commands can significantly enhance your workflow. In this article, we’ll explore some useful Git commands that every developer should know, organized logically to reflect a typical workflow.
This is a non-exhaustive list, and these are the command lines I personally use the most.

1. Staging and Committing Changes

Stage All Changes

git add .

Stages all modified and new files in the current directory, preparing them for a commit.

Commit Changes with a Message

git commit -m "Your commit message"

Creates a commit with a descriptive message summarizing the changes made.

2. Branch Management

Create a New Branch and Switch to It

git checkout -b yourbranch

This command creates a new branch named yourbranch and switches to it immediately. It’s useful for starting new features or bug fixes.

Switch to an Existing Branch

git checkout yourbranch

Use this command to switch your working directory to an existing branch named yourbranch.

Switch to the Previous Branch

git checkout -

This command switches back to the last branch you were working on, which is handy when toggling between two branches.

3. Merging and Rebasing

Merge Branches

git merge yourbranch

Integrates changes from yourbranch into the current branch, using fast-forwarding by default if possible.

Merge with No Fast-Forward

git merge yourbranch --no-ff

Merges yourbranch into the current branch without fast-forwarding, preserving the branch history.

Rebase Branch

git rebase

This command reapplies your changes on top of another base tip, allowing you to maintain a linear project history.

4. Pushing Changes

Push Local Changes to Remote

git push

Pushes your committed changes to the remote repository, updating the corresponding branch.

Set Upstream and Push

git push --set-upstream origin yourbranch

Sets the upstream (remote tracking branch) for yourbranch and pushes your changes to it. This is useful when pushing a new branch to a remote repository for the first time.

5. Tagging

Create a New Tag

git tag 1.0.0

Creates a new tag named 1.0.0, which is useful for marking specific points in your history as significant releases.

Push Tags to Remote

git push --tags

This command pushes all your local tags to the remote repository, ensuring versioning consistency across collaborators.

Delete a Local Tag

git tag --delete 1.0.0

Removes the local tag named 1.0.0.

Delete a Remote Tag

git push --delete origin 1.0.0

Deletes the tag 1.0.0 from the remote repository.

6. Stashing Changes

Stash Changes

git stash

Saves your modified and staged changes on a stack for later use, allowing you to work on a clean slate temporarily.

Apply Stashed Changes

git stash pop

Restores the most recent stashed changes and removes them from the stash list.

7. Cleaning Up

Prune Remote Tracking Branches

git remote prune origin

This command cleans up your local repository by removing references to remote branches that no longer exist on the server, keeping your repository tidy and up-to-date.

Ignore already-tracked folder

If node_modules/ (for example) was committed in the past, adding it to .gitignore is not enough — you must remove it from the Git index (tracking) while keeping the folder locally.
Ignore node_modules/

echo "node_modules/" >> .gitignore
git rm -r --cached node_modules/
git add .gitignore
git commit -m "Stop tracking node_modules"

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *