cheatsheets
GIT Brief history
related to git...
version control github gitlab docker compose jenkins continuous integration npm ssh automation
http web services authentication
Cheat Sheet Resources
Beginner Print
git init - Initialize a new Git repository. Tutorial
The 'git init' command is used to create a new, empty Git repository or to reinitialize an existing
one. This command sets up the necessary files and directories for Git to track changes in your
project, allowing you to start version control from scratch. It's typically the first command you run in
a new project to initiate tracking with Git.
Copy
Initialize a New Repository
git init my-project
Reinitialize an Existing Repository
cd existing-project && git init
git clone <url> - Clone a repository into a new directory. Tutorial
The 'git clone <url>' command is used to create a copy of an existing Git repository. This
command downloads the repository and its entire version history into a new directory, allowing
developers to work with the code locally. It is the primary way to obtain a repository from a remote
server.
Copy
Clone a repository from GitHub
git clone https://github.com/user/repository.git
Clone a private repository using SSH
git clone
[email protected]:user/repository.git
Clone with a specific folder name
git clone https://github.com/user/repository.git my-folder
git add <file> - Stage changes to a specified file. Tutorial
The git add <file> command is used to stage changes in a specified file for the next commit in a
cheatsheets
Git repository. This command allows users to selectively choose which changes to include in the
commit, effectively enabling them to build commits incrementally by organizing and grouping
related changes together.
Copy
Stage a single file
git add file.txt
Stage multiple files
git add file1.txt file2.txt
Stage all changes in the directory
git add .
Stage all changes to a specific type of file
git add '*.html'
Stage all changes except for a specific file
git add --all :/ && git reset file_to_exclude.txt
git commit -m 'message' - Commit staged changes with a message.
Tutorial
The 'git commit -m' command is used to record changes to the repository. When changes are
staged using 'git add', you can finalize these changes and include a descriptive message using the
'-m' flag, which helps in tracking the history of modifications made to the files.
Copy
Commit a simple change
git commit -m 'Fixed typo in README file'
Commit after adding new feature
git commit -m 'Added authentication feature to user login'
Commit with detailed message
git commit -m 'Refactored user service to improve performance and reduce
complexity'
Commit after fixing a bug
git commit -m 'Resolved issue with data validation on form submission'
git status - Show the working tree status. Tutorial
The 'git status' command is used to display the state of the working directory and the staging area.
It helps to inform you about which changes have been staged, which haven't, and which files
aren’t being tracked by Git. This is a crucial command to verify the current state of your repository
before committing changes.
Copy
Check Status
git status
Check Status for Specific Path
git status <file-path> cheatsheets
advertisement
git log - View commit history. Tutorial
The 'git log' command is used to view the commit history of a Git repository. It displays a
chronological list of commits made to the repository, showing details such as the commit hash,
author, date, and commit message, which helps developers track changes and understand the
evolution of the project.
Copy
Basic Log
git log
One-line Log
git log --oneline
Log with Graph
git log --graph --abbrev-commit --decorate
Log by Author
git log --author='Author Name'
Log with Date Range
git log --since='2023-01-01' --until='2023-12-31'
git branch - List branches in the repository. Tutorial
The 'git branch' command is used to list, create, or delete branches in a Git repository. A branch is
a pointer to a commit, allowing developers to work on different features or fixes in isolation while
keeping the main codebase intact. This command helps in visualizing and managing the branches
available in the repository.
Copy
List All Branches
git branch
List All Remote Branches
git branch -r
List All Local and Remote Branches
git branch -a
Show Current Branch
git branch --show-current
git checkout <branch> - Switch to the specified branch. Tutorial
The 'git checkout <branch>' command is used to switch between branches in a Git repository. This
allows developers to work on different features or fixes in isolation without affecting the main
codebase. When you switch branches, your working directory is updated to reflect the state of that
branch, including any files that are added, modified, or deleted.
cheatsheets
Copy
Checkout to an existing branch
git checkout feature-branch
Checkout to the main branch
git checkout main
Create and switch to a new branch
git checkout -b new-feature
View branches before checkout
git branch
git pull - Fetch from and integrate with another repository or a local
branch. Tutorial
The 'git pull' command is used to fetch changes from a remote repository and merge them into the
current branch. This command is a combination of 'git fetch' (which retrieves updates from a
remote repository) and 'git merge' (which integrates those updates into the local branch). It is
essential for keeping your local repository up to date with the latest changes made by others.
Copy
Basic pull from origin
git pull origin main
Pull changes from a specific remote branch
git pull origin feature-branch
Pull and rebase instead of merge
git pull --rebase origin main
git push - Update remote refs along with associated objects. Tutorial
The 'git push' command is used to upload local repository content to a remote repository. This
command updates the remote refs with the local commits, effectively sharing changes made by
the developer with the rest of the team. It is essential for collaboration, as it allows all contributors
to access the latest changes in the project.
Copy
Push to Default Remote Branch
git push
Push to a Specific Remote and Branch
git push origin main
Force Push Changes
git push --force
Push All Branches to Remote
git push --all
Push and Set Upstream Tracking cheatsheets
git push -u origin feature-branch
advertisement
Intermediate Print
git merge <branch> - Merge specified branch into the current branch.
Tutorial
The 'git merge <branch>' command is used to integrate changes from the specified branch into
the current branch. This operation combines the histories of both branches, allowing you to bring
in updates, features, or bug fixes made on the specified branch while preserving the history of
commits.
Copy
Merge feature branch
git merge feature-branch
Merge with a specific message
git merge feature-branch -m 'Merging feature branch into main'
Merge and resolve conflicts
git merge feature-branch
# Resolve any conflicts, then use:
git add <resolved_file>
git commit
git rebase <branch> - Reapply commits from one branch onto another.
Tutorial
The 'git rebase <branch>' command is used to move or combine a sequence of commits to a new
base commit, which allows for a cleaner project history by incorporating changes from one branch
onto another. It's particularly useful when you want to update a feature branch with the latest
updates from the main branch before merging.
cheatsheets
Copy
Example 1: Rebase a feature branch onto the main branch
git checkout feature-branch
git rebase main
Example 2: Rebase interactively to edit, squash or reorder commits
git rebase -i HEAD~5
Example 3: Skip commits that are already applied during rebasing
git rebase --skip
Example 4: Abort a rebase operation if conflicts occur
git rebase --abort
git stash - Stash changes in a dirty working directory. Tutorial
The 'git stash' command allows developers to temporarily save changes in a dirty working
directory without committing them to the current branch. This is particularly useful when you need
to switch branches or pull in changes from a remote repository but aren't ready to commit your
current work. The stashed changes can be reapplied later using 'git stash apply' or 'git stash pop'.
Copy
Stash your changes
git stash
Stash changes with a message
git stash save 'message describing the stash'
List all stashes
git stash list
Apply the most recent stash
git stash apply
Pop the most recent stash
git stash pop
Drop a specific stash
git stash drop stash@{0}
git remote - Manage set of tracked repositories. Tutorial
The 'git remote' command is used to manage the set of tracked repositories in Git. It allows
developers to add, remove, and update references to remote repositories, making it easier to
collaborate with others and manage codebases that reside in different locations.
Copy
Add a remote repository
git remote add <name> <url>
View remote repositories
git remote -v cheatsheets
Remove a remote repository
git remote remove <name>
Rename a remote repository
git remote rename <old-name> <new-name>
Fetch changes from a remote repository
git fetch <name>
git fetch - Download objects and refs from another repository. Tutorial
The 'git fetch' command downloads the latest changes from a remote repository without merging
them into the current branch. It updates the local copies of remote branches, allowing users to
review changes before integrating them into their local codebase.
Copy
Fetch from default remote repository
git fetch
Fetch from a specific remote repository
git fetch origin
Fetch changes from a specific branch
git fetch origin main
Fetch and prune deleted branches
git fetch --prune
advertisement
git cherry-pick <commit> - Apply the changes introduced by an existing
commit. Tutorial
The 'git cherry-pick <commit>' command allows you to select specific commits from one branch
and apply them to another branch. This is useful when you want to incorporate certain changes
from a different branch without merging all the changes from that branch. Cherry-picking helps
maintain a cleaner commit history by allowing targeted integration of changes.
Copy
Cherry-pick a single commit
git cherry-pick abc1234
Cherry-pick multiple commits
git cherry-pick abc1234 def5678
Cherry-pick a commit with a range
git cherry-pick abc1234^..def5678
Cherry-pick and resolve conflicts
git cherry-pick abc1234
# Resolve any conflicts
git add <resolved files>
git cherry-pick --continue cheatsheets
git tag - Create, list, or delete tags. Tutorial
Tags in Git are used to mark specific points in the repository's history as important. They are
commonly used to mark release points (e.g., v1.0, v2.0). Unlike branches, tags do not change and
are meant to be immutable references to specific commits.
Copy
Create a tag
git tag v1.0
List all tags
git tag
Delete a tag
git tag -d v1.0
Create a tag with a message
git tag -a v1.0 -m 'Release version 1.0'
Push tags to the remote repository
git push origin --tags
git reset <file> - Unstage a file while retaining changes. Tutorial
The 'git reset <file>' command is used to unstage a file that has been added to the staging area
while preserving the changes made to that file in the working directory. This allows the developer
to keep their edits without including them in the next commit, providing flexibility in managing
which changes to track.
Copy
Unstage a specific file
git reset myfile.txt
Unstage multiple files
git reset file1.txt file2.txt
Unstage all files
git reset .
git diff - Show changes between commits, commit and working tree,
etc. Tutorial
The 'git diff' command is used to show the differences between various states of your repository. It
can be applied to compare changes between commits, between the working tree and index, or
between your current working directory and the last commit. This helps developers understand
what modifications have been made, review changes before committing, or assess differences
between branches.
Copy
Show changes in the working directory cheatsheets
git diff
Show changes staged for the next commit
git diff --cached
Show changes between two commits
git diff <commit1> <commit2>
Show changes between a branch and the working tree
git diff <branchname>
Show changes between the working directory and the last commit
git diff HEAD
git fetch --prune - Remove remote-tracking references that no longer
exist on the remote. Tutorial
The 'git fetch --prune' command is used to update the local repository with changes from the
remote repository while also cleaning up any remote-tracking references that no longer exist on
the remote. This is particularly useful when branches have been deleted on the remote, as it helps
keep your local repository tidy by removing references to those branches.
Copy
Fetch and Prune
git fetch --prune
Fetch from a Specific Remote with Pruning
git fetch origin --prune
advertisement
Advanced Print
git reflog - Show reference logs. Tutorial
cheatsheets
The 'git reflog' command is used to show the reference logs in a Git repository. It tracks updates to
the tips of branches and what commits were checked out, providing a record that can help restore
lost commits or simply analyze the changes made within the repository over time. The reflog is
particularly useful in situations where commits might have been lost due to a bad rebase, reset, or
other operations that alter the project history.
Copy
View Reflog
git reflog
Show reflog for a specific branch
git reflog show <branch>
Revert to a previous commit using reflog
git checkout HEAD@{2}
Clear reflog
git reflog expire --expire=now --all
git bisect - Use binary search to find which commit introduced a bug.
Tutorial
Git bisect is a powerful tool used to find the specific commit that introduced a bug by employing a
binary search algorithm. It allows developers to efficiently narrow down the range of commits that
could potentially contain the problematic code, making it easier to identify when the issue was
introduced and subsequently resolve it.
Copy
Basic Bisect Usage
git bisect start
# Mark the current commit as bad
git bisect bad
# Mark a known good commit
git bisect good <commit-hash>
# Git automatically checks out a commit in the middle.
# Test the software, if it's bad:
git bisect bad
# If it's good:
git bisect good
# Repeat the process until Git identifies the bad commit.
# Finish bisecting:
git bisect reset
Using Bisect with a Test Script
git bisect start
# Mark the current commit as bad
git bisect bad
# Mark a known good commit
git bisect good <commit-hash> cheatsheets
# Now use a script to automate the testing:
git bisect run <your-test-script>
# Once completed, the bad commit will be identified.
git submodule - Initialize, update or fetch submodules in a repository.
Tutorial
A Git submodule is a repository embedded within another Git repository, allowing you to keep a Git
repository as a subdirectory of another Git repository. This is particularly useful for managing
dependencies or including libraries that are developed in separate repositories. Submodules allow
for better organization of projects that depend on outside repositories while keeping the main
project's codebase clean and manageable.
Copy
Initialize a Submodule
git submodule add <repository-url> <path>
Update Submodules
git submodule update --init --recursive
Fetch Changes for Submodules
git submodule foreach git pull origin master
git config - Get and set repository or global options. Tutorial
The 'git config' command is used for configuring Git settings at the repository or global level. Users
can set various options such as username, email, and default behaviors to customize their
experience and ensure that their commits are properly attributed. This command can manage both
local settings for a specific repository and global settings that apply across all repositories for a
user.
Copy
Set Global Username
git config --global user.name 'Your Name'
Set Global Email
git config --global user.email '
[email protected]'
View Current Configuration
git config --list
Set Editor for Git
git config --global core.editor nano
Set Default Branch Name
git config --global init.defaultBranch main
git archive - Create an archive of files from a named tree. Tutorial
cheatsheets
The 'git archive' command is used to create an archive file, such as a tar or zip file, that contains
the contents of a specific tree (a snapshot of the project at a particular point in time) in your Git
repository. This is useful for creating a release package or sharing specific versions of your project
without needing to expose the entire repository history.
Copy
Create a zip archive of the current branch
git archive -o latest.zip HEAD
Create a tar.gz archive of a specific tag
git archive -o release.tar.gz refs/tags/v1.0
Create a tar archive of a specific directory in the repository
git archive -o src_archive.tar HEAD:src
advertisement
git merge --no-ff - Create a merge commit even if the merge resolves as
a fast-forward. Tutorial
The 'git merge --no-ff' command is used to create a merge commit in Git, even when the merge
could be resolved with a fast-forward. This is useful for maintaining a clearer history in a project,
as it explicitly shows when a feature or branch was integrated while preserving the context of that
integration.
Copy
example1
git checkout main
git merge --no-ff feature-branch
example2
git checkout develop
git merge --no-ff new-feature
git fetch origin --tags - Fetch all remote tags. Tutorial
The command 'git fetch origin --tags' is used to retrieve all the tags from the remote repository
named 'origin'. Tags are reference points in the commit history, typically used to mark specific
releases or significant points in the project's timeline. This command does not alter your local
working directory or branch but updates your remote tracking branches and tags, allowing you to
see all the tags available on the remote without merging any changes.
Copy
Fetch all remote tags
git fetch origin --tags
git worktree - Manage multiple working trees attached to the same
repository. Tutorial
The 'git worktree' command allows you to manage multiple working trees associated with a single
cheatsheets
Git repository. This feature is useful for working on different branches at the same time without
having to switch back and forth, enabling parallel development and testing of different features or
bug fixes.
Copy
Add a new worktree
git worktree add ../new-feature-branch feature-branch
List active worktrees
git worktree list
Remove a worktree
git worktree remove ../new-feature-branch
Checkout a branch in a new worktree
git worktree add -b new-branch ../new-branch-directory
git format-patch - Prepare patches for e-mail submission. Tutorial
The 'git format-patch' command is used to create patch files from commits. These patches contain
the changes made in the commits and can be emailed to other developers for review or integration
into another repository. This command is particularly useful when you want to share a commit or a
series of commits without requiring the recipient to clone the entire repository.
Copy
Create a patch for the last commit
git format-patch -1 HEAD
Create patches for the last 3 commits
git format-patch -3
Create a patch from a specific commit to the HEAD
git format-patch <commit-hash>..HEAD
Create patches and save them into a specific directory
git format-patch -o /path/to/directory HEAD~3..HEAD
git blame <file> - Show what revision and author last modified each line
of a file. Tutorial
The 'git blame <file>' command is used to display the last modification information for each line of
a specified file in a Git repository. It shows which commit and author modified each line, thereby
helping developers understand the history and context behind changes made to the file. This
command is particularly useful for tracing bugs and understanding the evolution of code.
Copy
Example 1: Basic Usage
git blame README.md
Example 2: Blame with Line Numbering
git blame -n script.js
Example 3: Blame with Show Commit Hash cheatsheets
git blame -p main.py
Example 4: Blame with Specific Revision
git blame <commit_hash> file.txt
advertisement
© 2024 cheatsheet-plus-plus.com. All rights reserved.
Terms of Service | Privacy Policy