Git Cheat Sheet

Dec. 31, 2022


0
7 min read
440

In this article, we will look at Git Cheat Sheet. This cheat sheet will be useful for all programmers and developers, you can keep this page bookmarked and you can also share it with your friends, so let's start it.

Git Basics 

  1. git init <directory>

    Create empty Git repo in the specified directory. Run with no arguments to initialize the current directory as a git repository.

  2. git clone <repo>

    Clone repo located at <repo> onto the local machine. The original repo can be located on the local filesystem or on a remote machine via HTTP or SSH.

  3. git config user.name <name>

    Define the author name to be used for all commits in the current repo. Devs commonly use the --global flag to set config options for the current user.

  4. git add <directory>

    Stage all changes in <directory> for the next commit. Replace <directory> with a <file> to change a specific file.

  5. git commit -m "<message>"

    Commit the staged snapshot, but instead of launching a text editor, use <message> as the commit message.

  6. git status

    List which files are staged, unstaged, and untracked.

  7. git log

    Display the entire commit history using the default format. For customization see additional options.

  8. git diff

    Show unstaged changes between your index and the working directory.

Undoing Changes

  1. git revert <commit>

    Create a new commit that undoes all of the changes made in <commit>, then apply it to the current branch.

  2. git reset <file>

    Remove <file> from the staging area, but leave the working directory unchanged. This unstaged a file without overwriting any changes.

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

  1. git commit --amend
    

    Replace the last commit with the staged changes and the last commit combined. Use with nothing staged to edit the last commit’s message.

  2. git rebase <base>

    Rebase the current branch onto <base>. <base> can be a commit ID, branch name, tag, or a relative reference to HEAD.

  3. git reflog

    Show 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

  1. git branch

    List all of the branches in your repo. Add an <branch> argument to create a new branch with the name <branch>.

  2. git checkout -b <branch>

    Create and check out a new branch named <branch>. Drop the -b flag to check out an existing branch.

  3. git merge <branch>

    Merge <branch> into the current branch.

Remote Repositories

  1. git remote add <name> <url>
    

    Create a new connection to a remote repo. After adding a remote, you can use <name> as a shortcut for <url> in other commands.

  2. git fetch <remote> <branch>
    

    Fetches a specific <branch>, from the repo. Leave off <branch> to fetch all remote refs.

  3. git pull <remote>

    Fetch the specified remote’s copy of the current branch and immediately merge it into the local copy.

  4. git push <remote> <branch>

    Push the branch to <remote>, along with the necessary commits and objects. Creates a named branch in the remote repo if it doesn’t exist.

Git Pull

  1. git pull --rebase <remote>

    Fetch the remote’s copy of the current branch and rebase it into the local copy. Uses git rebase instead of the merge to integrate the branches.

Git Push

  1. git push <remote> --force

    Forces the git push even if it results in a non-fast-forward merge. Do not use the --force flag unless you’re absolutely sure you know what you’re doing.

  2. git push <remote> --all

    Push all of your local branches to the specified remote.

  3. git push <remote> --tags

    Tags aren’t automatically pushed when you push a branch or use the --all flag. The --tags flag sends all of your local tags to the remote repo.

Git Rebase

  1. git rebase -i <base>

    Interactively rebase current branch onto <base>. Launches editor to enter commands for how each commit will be transferred to the new base.

Git Reset

  1. git reset

    Reset the staging area to match the most recent commit, but leave the working directory unchanged.

  2. git reset --hard

    Reset the staging area and working directory to match the most recent commit and overwrites all changes in the working directory.

  3. git reset <commit>

    Move the current branch tip backwards to <commit>, reset the staging area to match, but leave the working directory alone.

  4. git reset --hard <commit>

    Same as previous, but resets both the staging area & working directory to match. Deletes uncommitted changes, and all commits after <commit>.

Git Diff

  1. git diff HEAD

    Show the difference between the working directory and the last commit.

  2. git diff --cached

    Show the difference between staged changes and the last commit.

Git Config

  1. git config --global user.name <name>

    Define the author name to be used for all commits by the current user.

  2. git config --global user.email <email>

    Define the author email to be used for all commits by the current user.

  3. git config --global alias. <alias-name> <git-command>

    Create a shortcut for a Git command. E.g. alias.glog “log --graph --oneline” will set ”git glog”equivalent to ”git log --graph --oneline.

  4. git config --system core.editor <editor>

    Set text editor used by commands for all users on the machine. <editor> arg should be the command that launches the desired editor (e.g., vi).

  5. git config --global --edit

    Open the global configuration file in a text editor for manual editing.

Git Log

  1. git log -<limit>

    Limit the number of commits by <limit>. E.g. ”git log -5” will limit to 5 commits.

  2. git log --oneline

    Condense each commit to a single line.

  3. git log -p

    Display the full diff of each commit.

  4. git log --stat

    Include which files were altered and the relative number of lines that were added or deleted from each of them.

  5. git log --author= ”<pattern>”

    Search for commits by a particular author.

  6. git log --grep=”<pattern>”

    Search for commits with a commit message that matches <pattern>.

  7. git log <since>..<until>

    Show commits that occur between <since> and <until>. Args can be a commit ID, branch name, HEAD, or any other kind of revision reference.

  8. git log -- <file>

    Only display commits that have the specified file.

  9. git log --graph --decorate

    --graph flag draws a text-based graph of commits on the left side of commit msgs. --decorate adds names of branches or tags of commits shown.

 

If you want to know more about git then you can know from here https://git-scm.com/docs

 

coding Programming Git GitHub Appreciate you stopping by my post! 😊

Add a comment


Note: If you use these tags, write your text inside the HTML tag.
Login Required