Language: EN

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

Install on Mac

Use Homebrew:

brew install git

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 "your_email@example.com"

View Current Configuration

You can check your current configurations with:

git config --list

Configure Editor

Choose your preferred text editor for commit messages.

git config --global core.editor "editor_name"

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

Clone an Existing Repository

Copy a remote repository to your local machine.

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

View Repository Status

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

git status

Show

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

git show <commit>

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>

Change Control

Add Changes

Add files to the staging area.

Add a Specific File

git add filename

Add All Modified Files

git add .

Commit Changes

Make a Commit

Commit the changes added to the staging area:

git commit -m "Descriptive commit message"

Combine add and commit in One Step

Add and commit all changes in a single command.

git commit -a -m "Commit message"

Remove a File and Untrack it from Git

Delete the file and remove it from Git tracking.

git rm filename

Rename or Move a File

Rename or move a file in the repository.

git mv new_filename

History and Change Review

View Commit History

List recent commits in the project.

git log

View History with Diffs

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

git log -p

Compact History

Display the history with a summarized representation of commits.

git log --oneline --graph --all

View Uncommitted Changes

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

git diff

View Changes Between Two Specific Commits

Compare differences between two commits using their identifiers.

git diff commit_id1 commit_id2

Undo Changes

Revert Unstaged Changes

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

git checkout -- filename

Undo Staged Changes

Return files from the staging area to their previous state.

git reset filename

Reset a Commit Locally (Soft Reset)

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

git reset --soft HEAD~1

Hard Reset

Discard the last commits and all changes.

git reset --hard HEAD~1

Revert a Published Commit

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

git revert commit_id

Working with Branches

List All Branches

Show all local and remote branches.

git branch -a

Create a New Branch

Create a new branch and stay on the current branch.

git branch branch_name

Switch Branches

Switch to an existing branch.

git checkout branch_name

Create and Switch to a New Branch

Command that creates a branch and automatically switches to it.

git checkout -b branch_name

Merge a Branch with the Current One

Combine changes from another branch into the current branch.

git merge branch_name

Delete a Branch

Once merged, you can safely delete a branch.

git branch -d branch_name

Force Delete a Branch

Delete a branch even if it has not been merged.

git branch -D branch_name

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>

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>

Remote Repositories

View Configured Remotes

Show a list of remote repositories.

git remote -v

Add a Remote Repository

Link your local repository with a remote one.

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

Remove a Remote

Unlink a remote repository.

git remote remove origin

Fetch Changes from a Remote Repository

Download changes from the remote repository without applying them.

git fetch

Update the Local Repository with Remote Changes

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

git pull origin branch_name

Push Changes to a Remote Repository

Send your commits to the remote repository.

git push origin branch_name

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"

Tags

Tags are used to mark specific versions.

View All Tags

Show a list of all created tags.

git tag

Create a Tag

Mark a tag.

git tag tag_name

Push Tags to a Remote Repository

Send tags to the remote repository.

git push origin --tags

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

List Saved Stashes

Show a list of saved changes.

git stash list

Apply Saved Changes

Restore changes from the most recent stash.

git stash apply

Remove the Most Recent Stash

Delete the last applied stash.

git stash drop

Optimization and Cleanup

Remove Untracked Files

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

git clean -f

Compact Commit History

Rewrite the history of the last commits.

git rebase -i HEAD~n

Optimize the Repository

Remove unreferenced objects and optimize the size of the repository.

git gc