Git Cheat Sheet
5 min readMar 11, 2022
THE BASICS
- In the provided directory, create an empty Git repository. Run with no arguments to create a git repository in the current directory.
git init <directory>
- Clone the repository at <repository> to your local machine. The original repository can be found on the local drive or remotely through HTTP or SSH.
git clone <repository>
- Define an author name for all commits in the current repository. The --global flag is widely used by developers to define config options for the current user.
git config user.name <name>
- All modifications should be saved in <directory> for the next commit.
To update a specific file, replace the <directory> with the <file>.
git add <directory>
- Instead of opening a text editor, commit the staged snapshot with a commit message of <message>.
git commit -m "<message>"
- List the staged, unstaged, and untracked files.
git status
- Using the default format, show the whole commit history. Additional customization possibilities are available.
git log
- Show changes between your index and working directory that haven’t been staged.
git diff
PUSH CHANGES TO THE ORIGIN
- Push the branch, as well as any necessary commits and objects, to remote>. If a named branch does not exist in the remote repo, it is created.
git push <remote>
- Even if the git push results in a non-fast-forward merging, it is forced. Use the — force flag only if you’re certain you understand what you’re doing.
git push <remote> --force
- All of your local branches will be pushed to the designated remote.
git push <remote> --all
- When you push a branch or use the — all flag, tags aren’t automatically pushed. All of your local tags are sent to the remote repo when you use the — tags flag.
git push <remote> --tags
PULL CHANGES FROM THE ORIGIN
- Fetch the given <remote> branch’s copy and merge it into the local copy right away.
git pull <remote>
- Re-bases the remote copy of the current branch into the local copy. Instead of merging the branches, git rebase is used.
git pull --rebase <remote>
GIT BRANCHING
- List all of your repo’s branches. To create a new branch with the name <branch>, add a <branch> parameter.
git branch
- Make a new branch called <branch> and check it out. To checkout an existing branch, use the -b flag.
git checkout -b <branch>
- <branch> should be merged into the current branch.
git merge <branch>
- The repo is queried for a certain <branch>. To get all <remote> refs, remove <branch>.
git fetch <remote> <branch>
- Create a new remote repository connection. You can use <name> as a shorthand for <url> in other commands after adding a remote.
git remote add <name> <url>
- cherry-picking implies taking a commit from one branch and applying it to another. This is in contrast to other methods such as merge and rebase, which often apply a large number of changes to a different branch.
git cherry-pick <commit-hash>
REVERSING CHANGES
- Make a new commit that undoes all of the modifications made in the previous commit, then apply it to the current branch.
git revert <commit>
- Remove the file from the staging area but keep the working directory. This removes a file from the stage without overwriting any changes.
git reset <file>
- This command displays which files will be removed from the working directory. To run the clean, use the -f flag instead of the -n flag.
git clean -n
CONFIGURE GIT
- Define the author name that will be used for all of the current user’s commits.
git config --global user.name <name>
- Define the author email that will be used for all of the current user’s commits.
git config --global user.email <email>
- Create a Git command shortcut. For example, alias. glog “log --graph. -- oneline” converts “git glog” to “git log --graph --oneline.”
git config --global alias. <alias-name> <git-command>
- Set the default text editor for all users on the computer. The command that launches the desired editor should be specified in the <editor> parameter (e.g., vi).
git config --system core.editor <editor>
- Manually edit the global configuration file in a text editor.
git config --global --edit
GIT LOG’S
- Limit the number of commits with a value of <limit>. “git log -3”, for example, will limit the number of commits to 3.
git log -<limit>
- Each commit should be reduced to a single line.
git log --oneline
- Show the complete diff for each commit.
git log -p
- Include the names of the files that were changed, as well as the number of lines that were added or deleted from each.
git log --stat
- Look for commits from a specific author.
git log --author = ”<pattern>”
- Look for commits using the <pattern> in their commit message.
git log --grep = ”<pattern>”
- Between <since> and <untill>, show commits that occurred. A commit ID, branch name, HEAD, or any other sort of revision reference can be used as arguments.
git log <since>..<untill>
- Only commits with the selected file will be shown.
git log -- <file>
- On the left side of commit messages, the --graph flag displays a text-based graph of commits. --decorate displays the names of branches or commit tags.
git log --graph --decorate
REBASE GIT
- Rebase the current branch to <base> interactively. Launches an editor where you can specify how each commit should be transferred to the new base.
git rebase -i <base>
REWRITING THE GIT THISTORY
- Replace the last commit with a combination of the staged modifications and the last commit. To update the latest commit’s message, use with nothing staged.
git commit --amend
- The current branch should be rebased upon <base>. A commit ID, branch name, tag, or a relative reference to HEAD can all be used as base.
git rebase <base>
Show a history of changes to the HEAD of the local repository.
To display date information, use the --relative-date flag, or use the --all flag to display all references.
git reflog
GIT RESET
- Reset the staging area to match the most recent commit, but do not modify the working directory.
git reset
- Overwrites any changes in the working directory and resets the staging area and working directory to match the most recent commit.
git reset --hard
- Reset the staging area to match, but leave the working directory alone by moving the current branch tip backward to <commit>.
git reset <commit>
- Same as before, but this time the staging area and working directory are both reset to match. After <commit>, all uncommitted modifications are deleted, as well as all commits.
git reset --hard <commit>
GIT DIFFERENCE
- Show the difference between the current working directory and the most recent commit.
git diff HEAD
- Demonstrate the distinction between staged changes and the most recent commit.
git diff --cached
HAPPY CODDING …. :)