Git cheat sheet

Git cheat sheet

Git Cheat Sheet

Essential Git Commands

it, the widely used version control system, is a cornerstone in the world of software development. While most developers are familiar with basic Git commands like git commitgit push, and git pull, there lies a treasure trove of lesser-known commands that can significantly enhance productivity and problem-solving skills. This article will unveil some of these hidden Git commands, offering insights into their utility and scenarios where they shine. Whether you’re a seasoned developer or new to Git, these commands are sure to add value to your development toolkit.

git bisect: Debugging Made Efficient

When dealing with a bug introduced by unknown commits, git bisect can be a lifesaver. This command performs a binary search through your commit history to help you pinpoint the exact commit that introduced an issue.

git bisect start
git bisect bad # Current state where the bug exists
git bisect good commit-hash # Point to a commit where the bug was not present
# Git will now check out a commit halfway between the ‘good’ and ‘bad’ commits
# Test this commit, then mark it ‘good’ or ‘bad’ and Git will continue narrowing down
git bisect reset # Once done, this command will return you to your original branch

git reflog: Your Safety Net

Ever had a moment of panic where a commit seems to have vanished into thin air? git reflog is here to save the day. This command keeps a log of everything you’ve done in Git, allowing you to backtrack and recover lost commits.

git reflog
# Find the commit you’re looking for and reset to it
git reset –hard commit-hash

git cherry-pick: Move Specific Commits Across Branches

Need a specific commit from one branch in another branch? git cherry-pick is your command. It allows you to pick a commit from one branch and apply it onto another.

checkout a repository

create a working copy of a local repository by running the command
git clone /path/to/repository
when using a remote server, your command will be
git clone username@host:/path/to/repository

add & commit

You can propose changes (add it to the Index) using
git add <filename>
git add *
This is the first step in the basic git workflow. To actually commit these changes use
git commit -m “Commit message”
Now the file is committed to the HEAD, but not in your remote repository yet.

pushing changes

Your changes are now in the HEAD of your local working copy. To send those changes to your remote repository, execute
git push origin master
Change master to whatever branch you want to push your changes to.

If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with
git remote add origin <server>
Now you are able to push your changes to the selected remote server

create a new branch named “feature_x” and switch to it using
git checkout -b feature_x
switch back to master
git checkout master
and delete the branch again
git branch -d feature_x
a branch is not available to others unless you push the branch to your remote repository
git push origin <branch>

update & merge

to update your local repository to the newest commit, execute
git pull
in your working directory to fetch and merge remote changes.
to merge another branch into your active branch (e.g. master), use
git merge <branch>
in both cases git tries to auto-merge changes. Unfortunately, this is not always possible and results in conflicts. You are responsible to merge those conflicts manually by editing the files shown by git. After changing, you need to mark them as merged with
git add <filename>
before merging changes, you can also preview them by using
git diff <source_branch> <target_branch>

Leave a Reply

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

Proudly powered by WordPress | Theme: Rits Blog by Crimson Themes.