0% found this document useful (0 votes)
12 views11 pages

Git Cheatsheet

The Git Cheat Sheet provides a comprehensive overview of Git, a version control tool that facilitates collaboration on projects by tracking changes in code. It covers essential terminology, common commands for repository management, core operations, branch management, and techniques for undoing changes. Additionally, it includes instructions for stashing changes, tagging releases, and exploring repository history.

Uploaded by

vb_pol@yahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views11 pages

Git Cheatsheet

The Git Cheat Sheet provides a comprehensive overview of Git, a version control tool that facilitates collaboration on projects by tracking changes in code. It covers essential terminology, common commands for repository management, core operations, branch management, and techniques for undoing changes. Additionally, it includes instructions for stashing changes, tagging releases, and exploring repository history.

Uploaded by

vb_pol@yahoo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Git Cheat Sheet

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.

Working Staging Git


Directory Area Repository
Checkout
files

Stage files Commit files

◦ 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

2. Common Git Configurations


• Edit the Configuration File:

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”

git config --global user.email


[email protected]” Sets the email address to be used in commits.

• Aliases:

Command Description
git config --global alias.st Sets up git st as a shorthand (called alias) for the
status command git status.

git config --get-regexp alias Lists all created aliases.

• Git Output Colorization:

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.

git config --global


Sets the color of the current branch name to yellow.
color.branch.current "yellow"

• Check All the Configurations:

Command Description
Lists all the current configuration settings in Git,
git config --list
including both global and local configurations.
Git Cheat Sheet

3. Initiating and Cloning Repositories


• Initialize a New Git Repository:

Command Description
Sets up a new Git repository in the project folder where the
git init
command is executed.

• Clone an Existing Repository:

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.

4. Core Git Operations


Command Description
Shows the current state of the working directory and staging
git status
area, including untracked, modified, and staged files.

git add <filename> Adds a specific file to the staging area.

Adds all changes in the current directory and its subdirectories


git add
to the staging area.

git commit -m "commit Saves the staged changes to the repository with a descriptive
message" message.

Displays the differences between the working directory and


git diff the staging area. It can be used with additional arguments to
compare commits.

5. Working with Remotes


• Manage Remote Repositories (git remote):

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.

git remote remove <name> Removes the specified remote repository.


Git Cheat Sheet
git remote rename Renames a remote repository from <old-name> to
<old-name> <new-name> <new-name>.

git remote set-url


Changes the URL for the specified remote repository.
<name> <new-url>

• Fetch Updates (git fetch):

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.

Fetch from all remote repositories linked to the local


git fetch --all
repository.

• Pull Changes (git pull):

Command Description
Retrieves updates from the default remote repository, usually
git pull
called origin, and merges them into the current branch.

Retrieves updates from a specific remote repository and


git pull <remote-name>
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.

• Push Changes (git push):

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 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.

Forcefully pushes changes to the remote repository,


git push --force overwriting any remote changes that conflict with any of the
local commits.
Git Cheat Sheet

6. Managing Branches and Merges


• Create and Delete Branches (git branch):

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.)

Deletes the specified branch. The -d option prevents deletion


git branch -d <branch-name>
if the branch has commits that are not merged into another
branch.

Using -D forcefully deletes the specified branch, even if it has


git branch -D <branch-name>
unmerged changes.

git push <remote-name>


--delete <branch-name> Deletes the specified branch from the remote repository.

• List and Rename Branches:

Command Description
Lists all branches in the local repository with the current
git branch
branch marked with an asterisk (*).

git branch -r Lists all remote-tracking branches.

git branch -a Lists both local and remote branches.

git branch -m
<new-branch-name> Renames the current branch.

git branch -m
<old-branch-name> Renames the specified branch.
<new-branch-name>

• Switching Branches (git switch):

Command Description
git switch <branch-name> Moves from the current branch to the specified one.

Creates a new branch named <branch-name> and


git switch -b <branch-name>
switches to it.

• Merge Branches (git merge):

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.

Opens an interactive editor to specify how to combine


(squash), reorder, or drop the last few commits from the
git rebase -i HEAD~<n> specified commit. Change the word “pick” listed against each
commit to “squash” or “drop.” Finally, edit the commit
message as needed.

• Using cherry-pick (git cherry-pick):

Command Description
Applies the changes introduced by <commit-hash> to the
git cherry-pick <commit-hash>
current branch.

git cherry-pick Applies all commits from <commit-hash1> to


<commit-hash1>.. <commit-hash2> to the current branch, excluding
<commit-hash2> <commit-hash1>.

• Replace Last Commits (git commit --amend)

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>


Specifies a commit message when reverting.
-m "message"

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.

git revert Reverts a range of commits from <oldest-commit-hash> to


<oldest-commit-hash>.. <newest-commit-hash>, excluding <oldest-commit-hash>
<newest-commit-hash> and creating a separate commit for each revert.
Git Cheat Sheet
• Reset Changes (git reset):

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.

Resets the current branch to a specific commit and discards the


git reset <commit-hash> changes in the staging area to match but does not change the
working directory.

git reset --hard Resets the current branch to a specific commit and discards
<commit-hash> all changes in the working directory and staging area.

• Using (git rm):

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 <file1> <file2> ... Removes multiple files in one command.

git rm -r
Deletes all files and subdirectories within the specified directory.
<directory-name>

• Clean Untracked Files (git clean)


Configuration: To make git clean work without any flags, set requireForce to false in the .gitconfig
file. Otherwise, use the -f or -i flag with the respective command.

Command Description
Lists all the untracked files and directories that would be deleted if
git clean -n
git clean commands are used.

git clean -f Deletes untracked files from the working directory.

git clean -fd Deletes both untracked files and directories.

Deletes all untracked files, including those listed in the file


git clean -fx
.gitignore.

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.

Displays a list of all stashes created, with names like stash@{0},


git stash list
stash@{1}, etc., for those created in that order.

Restores the changes saved in the latest stash (stash@{0}) to the


git stash apply
working directory.

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

git stash drop Deletes the most recent stash entry.

git stash drop


Deletes a specific saved stash.
stash@{n}

git stash clear Deletes all the saved stashes.

9. Marking Releases and Milestones


• Using (git tag):

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 tag -a <tag-name>


<commit-id> -m "Tag Tags a specific commit with some additional message.
message"
Git Cheat Sheet
git tag -d <tag-name> Deletes the specified tag from the local Git repository.

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.

10. Exploring Repository History


• Working with the log (git log):

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.

Displays a simplified view of the commit history of the current


git log --oneline
branch with only the commit hash and message, one line per commit.

Displays the specified number of most recent commits of the current


git log -n <number>
branch.

git log <file-path> Displays the commit history for the specified file in the current branch.

git log --since=


Displays only the commits in the specified date range for the current
"YYYY-MM-DD"
branch.
--until="YYYY-MM-DD"

git log --author=


Filters and displays the commits by a specific author.
"Author Name"

This includes the differences (called a patch) introduced by each


git log -p
commit in the commit history.

Provides a summary of changes in each commit, including file changes


git log --stat
and line counts.

git log --pretty=


format:"%h %s Customizes the output format of the commit history.

Displays the commit history as a graph to visualize branch and merge


git log --graph
history.
Git Cheat Sheet
• View Reference Logs (git reflog):

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.

• Using (git blame):

Command Description
git blame <file> Shows who last modified each line of the specified file.

git blame <file>


Shows the blame information for a file as of a specific commit.
<commit>

git blame -L <start>


Shows the blame information for a specific range of lines in a file.
,<end> <file>

git blame -e <file> Displays the email addresses of the authors in the blame information.

Shows more comprehensive blame information like full commit


git blame -p <file>
messages.

Shows the blame information, ignoring the changes solely applied to


git blame -w <file>
whitespaces.

• Using (git bisect):

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 bad


Marks the specified commit as bad.
<commit-hash>

git bisect good


Marks the specified commit as good, implying the absence of the bug.
<commit-hash>
Git Cheat Sheet
git bisect status Displays the current status of the bisect session.

git bisect reset Ends the bisect session and returns to the original branch.

11. Git Best Practices


• Commit Messages and Best Practices:
• Write the commit message with clear, imperative subject lines and optional body details.
• For the subject line:
◦ Keep it short—ideally within 50 characters.
◦ Write in the imperative mood (e.g., “Add feature” instead of “Added feature”).
◦ Capitalize the first letter of the subject.
◦ Avoid ending with a period
• For the body:
◦ Separate the subject line from the body with a blank line.
◦ Use the body for more detailed explanations when necessary.
◦ Wrap text at 72 characters per line for better readability.
◦ Briefly explain the reasoning behind the changes, not just what was changed.
◦ Add reference for any relevant issue numbers or tickets.

• 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"

You might also like