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
PHP

Show Branches

Show a list of all local branches.

git branch
0

Related Post

Create new branch

Create a new branch with the given name.

git branch branch-name
0

Delete a branch

This command will delete your provided branch.

git branch -d branch-name
0

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
0

Checkout branch

Switch to the specified existing branch by their name.

git checkout branch-name
0

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
0

Merge branch

Merge the specified branch into the current branch.

git merge branch-name
0

Abort a merge

Abort a merged branch that has conflicts.

git merge --abort
0

Push  commit

Push committed changes to a remote repository.

git push
0

Push Branch

Push changes to a specific branch on a remote repository.

git push remote  branch
0

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
0

Pull

Pull the latest changes from a remote repository.

git pull
0

Pull rebase

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

git pull --rebase
0

Fetch 

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

git fetch
0

Merge remote branch into a local branch

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

git merge remote/branch-name
0

Add a new remote repository to track

git remote add remote-name  remote-url
0

Show list of branches

Show a list of all remote repositories being tracked.

git remote -v
0

Show commit history

Show the commit history for the current branch.

git log
0

Show the commit history for a specific file

git log file-name
0

Commit history with a graph of the branch

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

git log --graph
0

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
0

Add a single file

Stage changes to a file to be committed in git.

git add yourfilename
0

Show the changes made to files in the given commit.

git diff commit-id
0

Show the changes made between two commits

git diff commit-1  commit-2
0

Show the status of the current branch and staged changes.

git status
0

Stash changes are to be reapplied later

git stash
0

Reapply the most recently stashed changes

git stash apply
0

Create a new tag at the specified commit

git tag tag-name commit-id
0

Show a list of all tags in the repository.

git tag
0

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 git@github.com:bootstrapfriendly/cheatsheet.git
0

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
0

Reset file

Unstaged changes to a file from commit.

git reset yourfilename
0

Reset all files

Unstage all changes from commit.

git reset .
0

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

Commit all changes

Stage and commit all changes in one command.

git commit -a
0

Modify recent commit message

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

git commit --amend
0

Leave a comment