cheatsheet-git

Git Cheatsheet

Git is a distributed version control system that allows you to manage and track changes in the source code of a project.

It facilitates collaboration among developers and the maintenance of software versions.

Installation and Initial Setup

Install on Windows

Download the installer from the official page and follow the instructions.

Install on Linux

Use the corresponding package manager:

sudo apt-get install git   # For Ubuntu/Debian
sudo yum install git       # For CentOS/RHEL
Copied!

Install on Mac

Use Homebrew:

brew install git
Copied!

Initial Configuration

Configure User

Set your name and email (they will appear in commit logs)

git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Copied!

View Current Configuration

You can check your current configurations with:

git config --list
Copied!

Configure Editor

Choose your preferred text editor for commit messages.

git config --global core.editor "editor_name"
Copied!

Basic Workflow Cycle

Working Directory

Local files not tracked by Git.

Staging Area

Files prepared for the next commit.

Repository (HEAD)

Files that have been committed and stored in Git.

Repositories

Initialize a New Repository

Create a new Git repository in the current directory.

git init
Copied!

Clone an Existing Repository

Copy a remote repository to your local machine.

git clone https://github.com/user/repo.git
Copied!

View Repository Status

Show the current status of files in the working directory (modified, untracked, etc.).

git status
Copied!

Show

Displays details of a specific commit, such as file changes, commit message, and metadata (author, date).

git show <commit>
Copied!

Blame

Shows who made the last change to each line of a file, useful for tracking when and who made modifications to the code.

git blame <file>
Copied!

Change Control

Add Changes

Add files to the staging area.

Add a Specific File

git add filename
Copied!

Add All Modified Files

git add .
Copied!

Commit Changes

Make a Commit

Commit the changes added to the staging area:

git commit -m "Descriptive commit message"
Copied!

Combine add and commit in One Step

Add and commit all changes in a single command.

git commit -a -m "Commit message"
Copied!

Remove a File and Untrack it from Git

Delete the file and remove it from Git tracking.

git rm filename
Copied!

Rename or Move a File

Rename or move a file in the repository.

git mv new_filename
Copied!

History and Change Review

View Commit History

List recent commits in the project.

git log
Copied!

View History with Diffs

Show commit history with detailed line-by-line changes.

git log -p
Copied!

Compact History

Display the history with a summarized representation of commits.

git log --oneline --graph --all
Copied!

View Uncommitted Changes

Show the differences between files in the working directory and the last version of the repository.

git diff
Copied!

View Changes Between Two Specific Commits

Compare differences between two commits using their identifiers.

git diff commit_id1 commit_id2
Copied!

Undo Changes

Revert Unstaged Changes

Discard changes in modified files that have not been added to the staging area.

git checkout -- filename
Copied!

Undo Staged Changes

Return files from the staging area to their previous state.

git reset filename
Copied!

Reset a Commit Locally (Soft Reset)

Undo the last commits while keeping changes in the working area.

git reset --soft HEAD~1
Copied!

Hard Reset

Discard the last commits and all changes.

git reset --hard HEAD~1
Copied!

Revert a Published Commit

Create a new commit that undoes the changes from a previous commit.

git revert commit_id
Copied!

Working with Branches

List All Branches

Show all local and remote branches.

git branch -a
Copied!

Create a New Branch

Create a new branch and stay on the current branch.

git branch branch_name
Copied!

Switch Branches

Switch to an existing branch.

git checkout branch_name
Copied!

Create and Switch to a New Branch

Command that creates a branch and automatically switches to it.

git checkout -b branch_name
Copied!

Merge a Branch with the Current One

Combine changes from another branch into the current branch.

git merge branch_name
Copied!

Delete a Branch

Once merged, you can safely delete a branch.

git branch -d branch_name
Copied!

Force Delete a Branch

Delete a branch even if it has not been merged.

git branch -D branch_name
Copied!

Rebase

Reorganize the commits of the current branch onto another branch. It is useful for cleaning up history or integrating changes without creating a merge commit.

git rebase <branch>
Copied!

Cherry Pick

Apply a specific commit from any branch or change history to the current branch. This allows selectively applying only certain changes.

git cherry-pick <commit>
Copied!

Remote Repositories

View Configured Remotes

Show a list of remote repositories.

git remote -v
Copied!

Add a Remote Repository

Link your local repository with a remote one.

git remote add origin https://github.com/user/repo.git
Copied!

Remove a Remote

Unlink a remote repository.

git remote remove origin
Copied!

Fetch Changes from a Remote Repository

Download changes from the remote repository without applying them.

git fetch
Copied!

Update the Local Repository with Remote Changes

Fetch and merge changes from the remote repository to the current branch.

git pull origin branch_name
Copied!

Push Changes to a Remote Repository

Send your commits to the remote repository.

git push origin branch_name
Copied!

Conflict Resolution

Resolve Merge Conflicts

When there are conflicts during a merge, Git will indicate the conflicting files. You need to edit them and resolve the conflicts manually. After resolving:

git add resolved-file.txt

git commit -m "Conflicts resolved"
Copied!

Tags

Tags are used to mark specific versions.

View All Tags

Show a list of all created tags.

git tag
Copied!

Create a Tag

Mark a tag.

git tag tag_name
Copied!

Push Tags to a Remote Repository

Send tags to the remote repository.

git push origin --tags
Copied!

Stash

The Stash is used to temporarily save changes.

Save Changes in Stash

Temporarily save uncommitted changes in the working area to allow working on other changes without losing progress.

git stash
Copied!

List Saved Stashes

Show a list of saved changes.

git stash list
Copied!

Apply Saved Changes

Restore changes from the most recent stash.

git stash apply
Copied!

Remove the Most Recent Stash

Delete the last applied stash.

git stash drop
Copied!

Optimization and Cleanup

Remove Untracked Files

Clean the working directory by removing files that are not under version control.

git clean -f
Copied!

Compact Commit History

Rewrite the history of the last commits.

git rebase -i HEAD~n
Copied!

Optimize the Repository

Remove unreferenced objects and optimize the size of the repository.

git gc
Copied!