Git Cheat Sheet

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which git log option displays a summarized list of modified files and the number of added or deleted lines for each commit?

  • `git log --graph`
  • `git log --stat` (correct)
  • `git log -p`
  • `git log --oneline`

What is the primary function of git rebase -i?

  • To interactively rebase the current branch onto another, allowing commit editing. (correct)
  • To list all commits in the repository in a condensed format.
  • To force a push to the remote repository, ignoring potential conflicts.
  • To automatically merge all remote branches into the current branch.

When should the --force flag be used with git push?

  • When you need to create a new branch on the remote repository.
  • When you want to include tags during your push.
  • When you want to push all local branches to the remote repository.
  • When you are certain about overwriting remote changes and understand the implications. (correct)

What is the purpose of the command git reset --hard?

<p>It resets both the staging area and the working directory to match the specified commit, deleting uncommitted changes and all commits after that point. (D)</p> Signup and view all the answers

You want to view the commits made by a specific author. Which git log command would you use?

<p><code>git log --author=&quot;author_name&quot;</code> (A)</p> Signup and view all the answers

Which command retrieves the remote's version of the current branch and integrates it into the local copy using rebasing?

<p><code>git pull --rebase</code> (B)</p> Signup and view all the answers

What is the purpose of the command git reset without the --hard option?

<p>It moves the current branch tip backward, resetting the staging area to match, but leaves the working directory alone. (C)</p> Signup and view all the answers

How can you push all of your local branches to a remote repository?

<p><code>git push --all</code> (D)</p> Signup and view all the answers

You have made several commits on a feature branch, but now realize they should never have been committed. What is the safest approach to remove these commits from the shared repository, assuming others may have based work on them?

<p>Use <code>git revert &lt;commit-1&gt;..&lt;commit-last&gt;</code> to create a new commit undoing the changes, then push. (B)</p> Signup and view all the answers

A developer modifies a tracked file in their working directory. Before committing, they want to discard the changes made to the file and revert it to the version in the last commit. What is the most efficient Git command to achieve this?

<p><code>git checkout -- &lt;file&gt;</code> (A)</p> Signup and view all the answers

A developer has made changes to a file and staged it using git add. They now want to remove the file from the staging area but keep the changes in their working directory. Which command should they use?

<p><code>git reset HEAD &lt;file&gt;</code> (A)</p> Signup and view all the answers

You need to inspect the differences between your current working directory and the last commit made on your branch. Which Git command will display these changes?

<p><code>git diff HEAD</code> (A)</p> Signup and view all the answers

You want to create a shortcut command in Git, such that typing git lg is equivalent to typing git log --graph --oneline. How can you configure this?

<p><code>git config --global alias.lg 'log --graph --oneline'</code> (B)</p> Signup and view all the answers

You need to configure Git to use VS Code as your default text editor for commit messages and other operations requiring text input. Which command achieves this?

<p><code>git config --system core.editor 'code --wait'</code> (A)</p> Signup and view all the answers

What is the primary function of the git clean -n command?

<p>It lists which untracked files <em>would</em> be removed from the working directory. (B)</p> Signup and view all the answers

You want to retrieve the latest changes from a remote repository named 'origin' for your current branch, and immediately integrate these changes into your local branch. Which Git command accomplishes this in a single step?

<p><code>git pull origin</code> (C)</p> Signup and view all the answers

You have made several commits on a feature branch, but now realize that these commits should be part of the develop branch. What is the most efficient Git command to integrate these changes while maintaining a clean history?

<p><code>git rebase develop</code> on the feature branch, followed by <code>git push</code> (A)</p> Signup and view all the answers

After modifying several files in your working directory, you want to stage only some of the changes for the next commit, while leaving others unstaged. What is the best approach to accomplish this?

<p>Use selective staging with <code>git add &lt;file&gt;</code> for each file you want to include in the commit. (D)</p> Signup and view all the answers

You've accidentally committed sensitive information to a public Git repository. What steps should you take to remove the sensitive data and ensure it's no longer accessible in the repository's history?

<p>Use <code>git filter-branch</code> or the <code>BFG Repo-Cleaner</code> tool to rewrite the repository's history, removing the sensitive data, then force-push the cleaned history. (B)</p> Signup and view all the answers

You have been working on a feature branch and want to update it with the latest changes from the main branch without creating a merge commit. Which Git command should you use?

<p><code>git rebase main</code> (A)</p> Signup and view all the answers

You've made a series of commits on your local machine but have not yet pushed them to a remote repository. You realize one of the commits has a mistake. How can you combine the last three commits into a single commit, edit the commit message, and then push them?

<p>Use <code>git rebase -i HEAD~3</code> and squash the commits, then edit the commit message. (A)</p> Signup and view all the answers

What is the primary purpose of the .git directory in a Git repository?

<p>It contains all the metadata and object database for the repository. (A)</p> Signup and view all the answers

After accidentally deleting a branch on your local machine, how can you recover it?

<p>Use <code>git reflog</code> to find the commit the branch pointed to, then create a new branch pointing to that commit with <code>git branch &lt;branch_name&gt; &lt;commit_id&gt;</code>. (D)</p> Signup and view all the answers

Which command would you use to see a list of all commits reachable from the current commit, displayed in a chronological order?

<p><code>git log</code> (A)</p> Signup and view all the answers

Flashcards

git init

Create a new, empty Git repository in the specified directory.

git clone

Copy a repository from a remote URL to your local machine.

git config user.name

Configure Git settings (e.g., user name, email).

git add

Add changes from the working directory to the staging area ready for commit.

Signup and view all the flashcards

git commit -m

Record staged changes in the repository with a descriptive message.

Signup and view all the flashcards

git status

List the status of files in the working directory and staging area.

Signup and view all the flashcards

git log

Display the commit history of the repository.

Signup and view all the flashcards

git branch

Create a new branch in the repository.

Signup and view all the flashcards

git revert

Creates a new commit that undoes changes from a previous commit.

Signup and view all the flashcards

git fetch

Fetches a specific branch from a remote repository.

Signup and view all the flashcards

git reset

Removes a file from the staging area without altering the working directory.

Signup and view all the flashcards

git pull

Combines 'git fetch' and 'git merge'.

Signup and view all the flashcards

git clean -n

Shows files that would be removed from the working directory by 'git clean'.

Signup and view all the flashcards

git push

Pushes a branch to a remote repository, including commits and objects.

Signup and view all the flashcards

git diff HEAD

Shows changes between the working directory and the last commit.

Signup and view all the flashcards

git diff --cached

Shows the difference between staged changes and the last commit.

Signup and view all the flashcards

git reset --hard

Resets both the staging area and working directory to match the specified commit. Deletes uncommitted changes and all commits after the specified commit.

Signup and view all the flashcards

git log -

Limits the number of commits displayed in the log. For example, git log -5 shows the last 5 commits.

Signup and view all the flashcards

git log --oneline

Condenses each commit to a single line in the log.

Signup and view all the flashcards

git rebase -i

Interactively rebase the current branch onto another. Allows you to edit, skip, or squash commits.

Signup and view all the flashcards

git pull --rebase

Fetch the remote's copy of the current branch and rebase it into the local copy. This integrates changes using rebase instead of merge.

Signup and view all the flashcards

git push --force

Forces the push to the remote repository, even if it results in a non-fast-forward merge. Use with caution.

Signup and view all the flashcards

git push --all

Pushes all local branches to the specified remote repository.

Signup and view all the flashcards

Study Notes

  • This document serves as a concise Git cheat sheet, offering quick reference to essential commands and concepts that facilitate the use of Git in software development. It aims to help developers efficiently manage their code changes and collaboration.
  • Git is a distributed version control system for tracking changes in source code during software development

Git Basics

  • git init <directory>: Creates an empty Git repository in the specified directory
  • Running git init without arguments initializes the current directory as a Git repository
  • git clone <repo>: Clones a repository located at <repo> onto the local machine
  • The original repository can be located on the local file system or on a remote machine via HTTP or SSH
  • git config user.name <name>: Defines the author name to be used for all commits in the current repository
  • Developers commonly use the --global flag to set configuration options for the current user
  • git add <directory>: Stages all changes in <directory> for the next commit
  • Replace <directory> with a <file> to change a specific file
  • git commit -m "<message>": Commits the staged snapshot, using <message> as the commit message
  • git status: Lists which files are staged, unstaged, and untracked
  • git log: Displays the entire commit history using the default format
  • git diff: Shows unstaged changes between your index and working directory

Undoing Changes

  • git revert <commit>: Creates a new commit that undoes all of the changes made in <commit>, then applies it to the current branch
  • git reset <file>: Removes <file> from the staging area, but leaves the working directory unchanged
  • This unstages a file without overwriting any changes
  • git clean -n: Shows which files would be removed from the working directory
  • Use the -f flag in place of the -n flag to execute the clean

Rewriting Git History

  • git commit --amend: Replaces the last commit with the staged changes and last commit combined
  • Use with nothing staged to edit the last commit's message
  • git rebase <base>: Rebases the current branch onto <base>
  • <base> can be a commit ID, branch name, a tag, or a relative reference to HEAD
  • git reflog: Shows a log of changes to the local repository's HEAD
  • Add --relative-date flag to show date info or --all to show all refs

Git Branches

  • git branch: Lists all of the branches in your repository
  • Add a <branch> argument to create a new branch with the name <branch>
  • git checkout -b <branch>: Creates and checks out a new branch named <branch>
  • Drop the -b flag to checkout an existing branch
  • git merge <branch>: Merges <branch> into the current branch

Remote Repositories

  • git remote add <name> <url>: Creates a new connection to a remote repository
  • After adding a remote, use <name> as a shortcut for <url> in other commands
  • git fetch <remote> <branch>: Fetches a specific <branch> from the repository
  • Leave off <branch> to fetch all remote refs
  • git pull <remote>: Fetches the specified remote's copy of current branch and immediately merges it into the local copy
  • git push <remote> <branch>: Pushes the branch to <remote>, along with necessary commits and objects
  • Creates named branch in the remote repo if it doesn't exist

Additional Options

  • git config --global user.name <name>: Defines the author name to be used for all commits by the current user globally
  • git config --global user.email <email>: Defines the author email to be used for all commits by the current user globally
  • git config --global alias. <alias-name> <git-command>: Creates a shortcut for a Git command
  • Example: alias.glog "log --graph --oneline" will set "git glog" equivalent to "git log --graph --oneline"
  • git config --system core.editor <editor>: Sets the text editor used by commands for all users on the machine
  • <editor> argument should be the command that launches the desired editor (e.g., vi)
  • git config --global --edit: Opens the global configuration file in a text editor for manual editing
  • git log -<limit>: Limits the number of commits by <limit>
  • Example: "git log -5" will limit to 5 commits
  • git log --oneline: Condenses each commit to a single line
  • git log -p: Displays the full diff of each commit
  • git log --stat: Includes which files were altered and the relative number of lines that were added or deleted from each of them
  • git log --author="<pattern>": Searches for commits by a particular author
  • git log --grep="<pattern>": Searches for commits with a commit message that matches <pattern>
  • git log <since>..<until>: Shows commits that occur between <since> and <until>
  • Arguments can be a commit ID, branch name, HEAD, or any other kind of revision reference
  • git log <file>: Only displays commits that have the specified file
  • git log --graph --decorate: The --graph flag draws a text-based graph of commits on the left side of commit messages, and --decorate adds names of branches or tags of commits shown
  • git diff HEAD: Shows the difference between the working directory and the last commit
  • git diff --cached: Shows the difference between staged changes and last commit
  • git reset: Resets the staging area to match the most recent commit, but leaves the working directory unchanged
  • git reset --hard: Resets the staging area and working directory to match the most recent commit, overwriting all changes in the working directory
  • git reset <commit>: Moves the current branch tip backward to <commit>, resets the staging area to match, but leaves the working directory alone
  • git reset --hard <commit>: Same as previous, but resets both the staging area & working directory to match, deleting uncommitted changes, and all commits after <commit>
  • git rebase -i <base>: Interactively rebases current branch onto <base>
  • Launches editor to enter commands for how each commit will be transferred to the new base
  • git pull --rebase <remote>: Fetches the remote's copy of current branch and rebases it into the local copy
  • Uses git rebase instead of merge to integrate the branches
  • git push <remote> --force: Forces the git push even if it results in a non-fast-forward merge
  • The --force flag should not be used unless you're absolutely sure you know what you're doing
  • git push <remote> --all: Pushes all of your local branches to the specified remote
  • git push <remote> --tags: Tags aren't automatically pushed when pushing a branch or using the --all flag
  • The --tags flag sends all of your local tags to the remote repo

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

Git Cheat Sheet PDF - Atlassian

More Like This

Use Quizgecko on...
Browser
Browser