Git Cheatsheet
Git Cheatsheet
1. Introduction
Git is a tool that allows multiple people to work on the same project simultaneously and helps them track
changes in the code.
• Basic Teminology:
◦ Repository (Repo): A folder that contains all the project files and the version history.
◦ Branch: A separate version of the project. The original version is usually called main or master.
◦ Commit: A snapshot of changes made in the repository with a message describing what was changed
at the given time.
◦ Staging area: The staging area is where files (ready to be committed) go before they are saved
(committed) to the Git repository. It helps to review the changes made and create meaningful commits.
◦ Check out: This means switching to a different branch or commit in a project and making it
available in the working directory.
◦ Commit hash: It is a unique ID (a long string of letters and numbers) that Git generates for each
commit made to the project.
◦ Reference: Also known as a ref, it is a label that Git uses to keep track of important points in your
project’s history.
◦ Tag: It is a reference that cannot be changed later to point elsewhere. It’s commonly used to denote
releases, versions, or significant milestones in a project’s development.
◦ HEAD: It is a reference that points to the latest change (commit) in the branch being worked on.
◦ Reference logs: They track all updates to branches and pointers (like HEAD), including lost commits,
merges, resets, and other changes.
◦ Logs: They only track the history of commits in a branch.
◦ Remote-tracking branches: They are branch references in the local Git repository that track the
currently known state of branches in a remote repository. They keep the local repository in sync with
the remote repository.
◦ Untracked files: These files are not part of the repository and have not been added to the staging area
or committed to the repository. They are often new files created by the build process.
• Key Features:
◦ Collaboration: Git makes it easy for teams to work on the same project by allowing different
workflows, such as centralized, feature-branch, and forking workflows.
◦ Distributed system: Each person working on the project has a copy of the project’s history. This means
team members can work offline, and their data is safe.
◦ Branching and merging: Branches can be created to test new ideas without altering the main branch.
If necessary, these changes can be merged into the main branch.
◦ Commit history: This records all the changes made to a project over time. It helps track changes, fix
mistakes, and return to earlier versions.
Git Cheat Sheet
Command Description
Opens the global Git configuration file in the default editor for
git config --global --edit manual editing. With the --global flag, the settings apply to all
repositories for the current user.
Opens the local Git configuration file for the current repository.
git config --local --edit With the --local flag, the settings apply only to that repository
and override any global settings.
• User Information:
Command Description
git config --global
Sets the name to be used in commits.
user.name “Name”
• Aliases:
Command Description
git config --global alias.st Sets up git st as a shorthand (called alias) for the
status command git status.
Command Description
git config --global color.ui Configures Git to automatically add a color to the output
auto in the terminal for all commands, making it easier to read.
Command Description
Lists all the current configuration settings in Git,
git config --list
including both global and local configurations.
Git Cheat Sheet
Command Description
Sets up a new Git repository in the project folder where the
git init
command is executed.
Command Description
Copy an existing repository from a remote server to a local
git clone <repository-URL> machine. The remote repository from which a project is
cloned is, by default, named origin.
git commit -m "commit Saves the staged changes to the repository with a descriptive
message" message.
Command Description
Lists the names of the remote repositories configured for the
git remote
local project.
git remote add <name> <url> Adds a new remote repository with the specified name and
URL.
Command Description
Retrieves updates from the default remote repository without
git fetch
merging them into the local branches.
git fetch
Fetch from the specified remote repository.
<remote-name>
git fetch <remote-name> Fetch from a specific branch of the specified remote
<branch-name> repository.
Command Description
Retrieves updates from the default remote repository, usually
git pull
called origin, and merges them into the current branch.
git pull <remote-name> Retrieves updates from a specific remote repository branch
<branch-name> and merges them into the current branch.
Command Description
Sends the commits from the current branch to the
git push
corresponding branch on the remote repository.
git push <remote-name> Pushes local changes to a specific branch of the remote
<branch-name> repository.
git push --all Pushes all local branches to the remote repository.
Command Description
Creates a new branch locally but stays in the current branch.
git branch <branch-name>
(The local branch can be pushed to create it remotely.)
Command Description
Lists all branches in the local repository with the current
git branch
branch marked with an asterisk (*).
git branch -m
<new-branch-name> Renames the current branch.
git branch -m
<old-branch-name> Renames the specified branch.
<new-branch-name>
Command Description
git switch <branch-name> Moves from the current branch to the specified one.
Command Description
Merge changes from the specified branch into the current
git merge <branch-name>
branch.
Git Cheat Sheet
• Using Rebase (git rebase):
Command Description
Takes the commits from the current branch and re-applies
git rebase <base-branch> them on top of the <base-branch>, creating a cleaner
history of the commits.
Command Description
Applies the changes introduced by <commit-hash> to the
git cherry-pick <commit-hash>
current branch.
Command Description
Replaces the last commit with a new one. It can be an update in the
git commit --amend
commit message or some new changes.
7. Undoing Changes
• Revert Commits (git revert):
Command Description
Creates a new commit that reverts the changes made by the
git revert <commit-hash> specified commit. The history of commits is not changed.
git revert <commit-hash> Reverts the specified commit but doesn’t automatically create
--no-commit a new commit.
git revert
Creates a new commit that reverts multiple specified commits
<commit-hash1>
<commit-hash2> ... in the order they are listed.
Command Description
The state of the current branch is reverted to the specified
git reset --soft
commit. However, the contents of the staging area and the
<commit-hash>
working directory remain unchanged.
git reset --hard Resets the current branch to a specific commit and discards
<commit-hash> all changes in the working directory and staging area.
Command Description
Deletes the specified file from the working directory and stages
git rm <file-name>
the deletion so that it will be committed.
git rm --cached Removes the file from the staging area but keeps the file in the
<file-name> working directory
git rm -r
Deletes all files and subdirectories within the specified directory.
<directory-name>
Command Description
Lists all the untracked files and directories that would be deleted if
git clean -n
git clean commands are used.
git clean -fX Deletes only the files listed in the file .gitignore and are
untracked.
git clean -i This enters an interactive mode for selecting which files or
directories to delete.
Git Cheat Sheet
8. Saving Changes Temporarily
• Stash Changes (git stash):
Command Description
Saves the changes in the working directory and staging area in a
git stash temporary store (a stash) and reverts the working directory to the
latest commit.
git stash apply Restores the changes saved in the specific stash entry identified by
stash@{n} index n to the working directory.
git stash pop Restores the most recent stash and removes it from the list of
stashes.
git stash pop Restores the stash with a specific index, n, and removes it from the
stash@{n} list of stashes
Command Description
git tag Displays a list of all the existing tags in the repository.
git tag <tag-name> Creates a new tag for the latest commit.
git tag -a <tag-name> Creates a tag with some additional message. Annotated tags
-m "message" include metadata like the tagger’s name, email, and date.
git tag <tag-name> Creates a new tag named <tag-name> that points to the
<commit-hash> commit with the hash <commit-hash>.
git push <remote-name> Deletes the tag <tag-name> from the remote repository
--delete <tag-name> <remote-name>.
git push <remote-name> Pushes a specific tag <tag-name> to the remote repository
<tag-name> <remote-name>.
git push --tags Pushes all tags to the remote repository that is associated with
the local repository.
Command Description
Displays the commit history, including a list of commits in the
git log current branch with details like the commit hash, author, date, and
commit message.
git log <file-path> Displays the commit history for the specified file in the current branch.
Command Description
Displays the reference logs (reflogs) for the current branch. Each log
git reflog includes an index, the commit hash, and a message describing the
change.
git reflog
Shows the reflog for a specific reference (e.g., branch or HEAD).
<reference>
git reflog -n
Displays the specified number of most recent reflog entries.
<number>
git reflog
Displays the reflog entries for the specified branch.
<branch-name>
git reflog show Displays a specific entry from the reflog. Replace ref with the
ref@{n} reference (e.g., HEAD) and n with the entry number.
Command Description
git blame <file> Shows who last modified each line of the specified file.
git blame -e <file> Displays the email addresses of the authors in the blame information.
Command Description
Initiates a binary search to pinpoint the commit that introduced a
git bisect start change or a bug. Next, the search range must be specified by marking
two commits as good and bad.
git bisect bad Marks the current commit as bad, implying that it contains the bug.
git bisect reset Ends the bisect session and returns to the original branch.
• Workflow Recommendations:
◦ Use branches for feature development, following a strategy like Git Flow or GitHub Flow.
◦ Commit small, focused changes regularly.
◦ Frequently pull changes from the remote repository to stay updated.
◦ Use pull requests for code review and collaboration; ensure they are approved before merging with
the remote branch.
◦ Combine related commits before merging for a clean history.
◦ Tag important milestones or versions.
• Git Flow
◦ Organized branching and Git flow process:
▪ Main branch: It always holds the stable, production-ready code version deployed or released.
▪ Develop branch: This is an integration branch where new features and fixes are combined before
release. It’s a staging area for the next release.
▪ Feature branches: These allow developers to work on new features or fixes independently. Start
each new feature or task by creating a feature branch from develop. When the feature is complete,
merge it back into develop.
▪ Release branches: These are used to prepare for a new release. A release branch is created when
the develop branch is stable and ready for release. Once the release is finalized (adjustments and
bug fixes are made), the release branch is merged into both main and develop for deployment.
▪ Hotfix branches: These are used for urgent fixes that must go directly to production. They are
created from main and merged back into both main and develop.
◦ Handling Merge Conflicts:
▪ Once we have identified the file(s) with conflicts, use a text editor or merge tool to resolve conflicts
in the files. Look for conflict markers (<<<<<<<, =======, >>>>>>>) in the affected files.
▪ Add the file to the staging area: git add <file>
▪ Commit the file to the remote: git commit -m "Resolve merge conflicts"