Git command cheatsheet

This Git command cheatsheet includes commonly used commands for setting up a repository, committing changes, working with branches, publishing changes, and inspecting changes. It also includes miscellaneous commands such as stashing and tagging.

Note: The commands are described in brief and can be helpful for Git users of all levels.

Git Initialization

This command will Initialize a new Git repository in the current directory

        
        git init
        
    

Show Branches

Show a list of all local branches.

        
        git branch
        
    

Related Post

Create new branch

Create a new branch with the given name.

        
        git branch branch-name
        
    

Delete a branch

This command will delete your provided branch.

        
        git branch -d branch-name
        
    

List all branches

 git branch -a command is used to list all branches in a Git repository, including both local and remote branches

        
        git branch -a
        
    

Checkout branch

Switch to the specified existing branch by their name.

        
        git checkout branch-name
        
    

Create and checkout a new branch

Using this single command you can create a new branch and switch to it.

        
        git checkout -b new-branch-name
        
    

Merge branch

Merge the specified branch into the current branch.

        
        git merge branch-name
        
    

Abort a merge

Abort a merged branch that has conflicts.

        
        git merge --abort
        
    

Push  commit

Push committed changes to a remote repository.

        
        git push
        
    

Push Branch

Push changes to a specific branch on a remote repository.

        
        git push remote  branch
        
    

Clone a repository 

This will create a clone or copy inside a folder of a remote repository in your local machine.

        
        git clone repositry-url
        
    

Pull

Pull the latest changes from a remote repository.

        
        git pull
        
    

Pull rebase

Pull changes from a remote repository and rebase instead of merging.

        
        git pull --rebase
        
    

Fetch 

Fetch the latest changes from a remote repository without merging them into your local branches.

        
        git fetch
        
    

Merge remote branch into a local branch

Merge changes from a remote branch into your current local branch.

        
        git merge remote/branch-name
        
    

Add a new remote repository to track

        
        git remote add remote-name  remote-url
        
    

Show list of branches

Show a list of all remote repositories being tracked.

        
        git remote -v
        
    

Show commit history

Show the commit history for the current branch.

        
        git log
        
    

Show the commit history for a specific file

        
        git log file-name
        
    

Commit history with a graph of the branch

Show the commit history with a graph of branch and merge information.

        
        git log --graph
        
    

Show the changes made to files that have been staged but not yet committed.

Git diff is commonly used to compare branches in a repository. It displays removed, added, and changed lines in the original file.

        
        git diff
        
    

Add a single file

Stage changes to a file to be committed in git.

        
        git add yourfilename
        
    

Show the changes made to files in the given commit.

        
        git diff commit-id
        
    

Show the changes made between two commits

        
        git diff commit-1  commit-2
        
    

Show the status of the current branch and staged changes.

        
        git status
        
    

Stash changes are to be reapplied later

        
        git stash
        
    

Reapply the most recently stashed changes

        
        git stash apply
        
    

Create a new tag at the specified commit

        
        git tag tag-name commit-id
        
    

Show a list of all tags in the repository.

        
        git tag
        
    

Change the URL of the existing remote repository

This command is used to change the URL of an existing Git remote repository. This command is useful when you need to update the URL of a remote repository.

        
        git remote set-url [remote-name] [repository-url]

//EX:

git remote set-url origin [email protected]:abdultechhub/cheatsheet.git
        
    

Add or commit all files

Stage all changed files or new files in the current directory to be committed

        
        git add .

//All changes including deletions 
git add -A
        
    

Reset file

Unstaged changes to a file from commit.

        
        git reset yourfilename
        
    

Reset all files

Unstage all changes from commit.

        
        git reset .
        
    

Commit changes with a message

Commit staged changes with a brief message describing the changes made in the local working branch.

        
        git commit -m "Brief message that describes committed changes."
        
    

Commit all changes

Stage and commit all changes in one command.

        
        git commit -a
        
    

Modify recent commit message

This command will help to modify the most recent commit message.

        
        git commit --amend
        
    

Leave a comment