Select AllThemeDownload
  • Must Read
    Guide to Push for first time

git config --global user.name "Your Name"

This command is used to set the name that will be associated with your commits in Git repositories. This name will appear in the metadata of your commits, identifying you as the author.

E.g. To set your username globally, run:

git config --global user.name "Your Name"

git config --global user.email "your_email@example.com"

This command sets the email address that will be associated with your commits globally. This email address will appear in the metadata of your commits, identifying you as the author.

E.g. To set your email globally, run:

git config --global user.email "your_email@example.com"

git clone [url]

Clone a repository from a remote source (github/gitlab) to your local machine.

E.g. To clone a repository Git-factory (repo) of Majid-Ali-Watto (owner) from GitHub, you can run:

git clone https://github.com/Majid-Ali-Watto/Git-factory.git

git init

This command initializes a new Git repository in the current directory. It sets up the necessary files and directories for Git to start tracking changes in your project.

E.g. To start tracking changes in a new project, navigate to the project directory and run:

git init

git status

This command is used to check the status of your working directory and staging area. It shows you which files have been modified, which files are staged for commit, and which files are untracked by Git.

E.g. To see which files have been modified since your last commit, run:

git status

git add [file]

This command adds changes in the working directory to the staging area. The staging area is a place where you can gather changes you want to include in your next commit. By using git add, you tell Git to include the changes made to a specific file (or files) in the next commit.

E.g. To stage a file named "index.html" ("git add ." for all files) for the next commit, run:

git add index.html		OR		git add .

git commit -m "Commit message"

This command records the changes that have been staged in the repository with a descriptive message. The commit message should summarize the changes made in the commit, providing context and information about the purpose of the changes.

E.g. To commit staged changes with the message "Add new feature", run:

git commit -m "Add new feature"

git push -u origin [branch]

Push the specified branch to the remote repository and set it as the upstream branch. It is needed for first time, after pushing at least once, upstream is not needed.

E.g. To push the local branch named "feature" to the remote repository named "origin" and set it as the upstream branch, run:

git push -u origin feature

git push [remote] [branch]

Upload local repository content to a remote repository (github/gitlab).

E.g. To push changes from your local "master" branch to the remote "origin" repository, run:

git push origin master

git pull [remote] [branch]

Fetch changes from a remote repository and merge them into the current branch.

E.g. To fetch and merge changes from the remote "origin" repository into your local "master" branch, run:

git pull origin master		OR		git pull

git branch

List all branches in the repository.

E.g. To see all branches in the repository and current selected branch, run:

git branch

git checkout [branch_name]

Switch to the specified branch.

E.g. To switch to a branch named "feature", run:

git checkout feature

git checkout -b [branch]

Create a new branch and switch to it.

E.g. To create a new branch named "feature" and switch to it, run:

git checkout -b feature

git switch [branch_name]

Switch to the specified branch.

E.g. To switch to a branch named "feature", run:

git switch feature

git merge [branch_name]

Combine changes from the specified branch into the current branch.

E.g. To merge changes from the "feature" branch into the current branch, run:

git merge feature

git remote add [name] [url]

Add a new remote repository.

E.g. To add a remote repository named "origin" with URL "https://github.com/Majid-Ali-Watto/Git-factory.git", run:

git remote add origin https://github.com/Majid-Ali-Watto/Git-factory.git

git remote -v

List all remote repositories and their URLs. It will show remote origins where repository is connected for push and pull.

E.g. To list all remote repositories and their URLs, run:

git remote -v

git log

Display commit history with author name, commit hash, date and commit message.

E.g. To display the commit history of the current branch, run:

git log

git show [commit]

Show changes introduced by a commit. Display commit history with author name, commit hash, date and commit message. What was added or removed?

E.g. To show changes introduced by a specific commit with hash "abcd123", run:

git show abcd123

git diff

Show changes between commits, commit and working tree, etc.

E.g. To show the changes between the working directory and the staging area, run:

git diff

git stash

This command saves your current working directory's changes into a 'stash' so you can work on something else, like switching branches, without losing your uncommitted changes. The working directory is then restored to the state of the last commit.

E.g. To stash changes in your working directory, run:

git stash

git stash save "Work in progress on feature X"

Stash changes with a descriptive message for easier identification later.

E.g. To stash changes with a specific message, run:

git stash save "Work in progress on feature X"

git stash apply

Applies a stash to the working directory.

E.g. git stash apply stash@{0}. If you don't specify a stash, the most recent stash is applied.

git stash apply stash@{0}		OR		git stash apply

git stash pop

Apply the most recent stash and remove it from the stash list.

E.g. To apply and remove the most recent stash, run:

git stash pop

git stash list

List all stashed changes.

E.g. To list all stashed changes or to list only specific count of stashed changes,e.g. to list the last 3 stashes add -3, run:

git stash list		OR		git stash list -3

git stash drop [stash]

Remove a specific stash.

E.g. To remove a stash named "stash@{0}", run:

git stash drop stash@{0}

git stash clear

Remove all stashed changes.

E.g. To remove all stashed changes, run:

git stash clear

git tag [tag_name]

Create a lightweight tag pointing to the current commit.

E.g. To create a tag named "v1.0" for the current commit, run:

git tag v1.0

git tag -a [tag_name] -m "message"

Create an annotated tag with a descriptive message.

E.g. To create an annotated tag named "v1.0" with the message "Version 1.0 release", run:

git tag -a v1.0 -m "Version 1.0 release"

git tag -d [tag_name]

Delete a tag from the local repository.

E.g. To delete the tag named "v1.0", run:

git tag -d v1.0

git tag -l

List all tags in the repository.

E.g. To list all tags, run:

git tag -l

git show [tag_name]

Show detailed information about a specific tag.

E.g. To show details about the tag named "v1.0", run:

git show v1.0

git push origin [tag_name]

Push a specific tag to the remote repository.

E.g. To push the tag named "v1.0" to the remote repository, run:

git push origin v1.0

git push --tags

Push all tags to the remote repository.

E.g. To push all local tags to the remote repository, run:

git push --tags

git cherry-pick [commit1] [commit2] ...

The `git cherry-pick` command allows you to apply changes from one or more specific commits from another branch or location directly onto your current branch. This is useful when you want to integrate particular changes without merging an entire branch.

E.g. To apply the changes from commits 'abcd123' and 'efgh456' onto the current branch, run:

git cherry-pick abcd123 efgh456

git rebase [branch]

The `git rebase [branch]` command integrates changes from one branch into your current branch by replaying your commits on top of the specified branch. Unlike merging, rebasing rewrites the commit history, creating a cleaner, linear history without merge commits.

E.g. To rebase the current branch onto the 'master' branch, applying your commits on top of 'master', run:

git rebase master

git pull --rebase

The `git pull --rebase` command fetches changes from the remote repository and rebases your current branch on top of the fetched commits instead of merging. This avoids unnecessary merge commits and keeps your project history linear and clean.

E.g. To fetch and rebase the changes from the remote repository 'origin' instead of merging them, run:

git pull --rebase origin

git rebase --abort

The `git rebase --abort` command stops an ongoing rebase process and returns your repository to the exact state it was in before the rebase began. This is helpful if you encounter conflicts or issues and decide not to continue with the rebase.

E.g. If conflicts arise during a rebase and you want to cancel it, run:

git rebase --abort

git rebase --continue

The `git rebase --continue` command is used to resume an ongoing rebase after you have resolved any conflicts. It tells Git that the conflicts have been fixed and it can proceed with applying the rest of the commits.

E.g. After resolving any conflicts during a rebase, to continue the rebase process, run:

git rebase --continue

git reset <file>

Unstage changes for the specified file while keeping the changes in the working directory.

E.g. To unstage changes for 'myfile.txt' without deleting the changes, run:

git reset myfile.txt

git checkout -- [file]

The `git checkout -- [file]` command discards any local changes made to a specific file in your working directory, reverting it back to the last committed state. This is useful if you want to undo modifications to a file and return it to how it was in the last commit.

E.g. To discard changes made to a file named 'index.html' and restore it to the version in the last commit, run:

git checkout -- index.html		OR		git checkout -- src/App.jsx

git revert [commit]

Reverse a commit by creating a new commit.

E.g. To reverse the changes introduced by a commit with hash "abcd123", run:

git revert abcd123

git reset --hard [commit]

Reset the current branch and index to a specified state.

E.g. To reset the current branch and index to the state of a commit with hash "abcd123", run:

git reset --hard abcd123

git reset --soft [commit]

Reset the current branch to a specified commit but keep changes in the working directory.

E.g. To reset the current branch to a commit with hash "abcd123" but keep changes in the working directory, run:

git reset --soft abcd123

git reset --mixed <commit-hash>

Undo commits and unstage changes, but keep the working directory intact.

E.g. To reset the commit history up to 'abc123' and unstage the changes, run:

git reset --mixed abc123

git restore [file]

Restore changes in the working directory for a file to their state at the last commit.

E.g. To restore changes in the working directory for a file named "index.html" to their state at the last commit, run:

git restore index.html

git restore --staged <file>

Unstage changes without altering the file in the working directory.

E.g. To unstage changes for 'myfile.txt', run:

git restore --staged myfile.txt

git rm --cached -r <directory>

Remove a directory or file from the staging area (index) while keeping it in the working directory. This is often used when you want to stop tracking a file or directory in Git without deleting it from your local system.

E.g. To stop tracking the 'dev-dist/' directory but keep it in your working directory, run:

git rm --cached -r dev-dist/

git gc

Cleanup unnecessary files and optimize the local repository.

E.g. To run garbage collection and optimize the repository, run:

git gc

git commit --allow-empty -m 'message'

Create an empty commit with a message.

E.g. To create an empty commit with the message 'Initial commit', run:

git commit --allow-empty -m 'Initial commit'

git fetch [remote]

This command is used to download objects and references (refs) from another repository, typically a remote repository. It updates your local repository with the latest commits, branches, and tags from the remote repository without merging them into your working branch.

E.g. To download objects and refs from the remote repository "origin", run:

git fetch origin

git remote rm [name]

Remove a remote repository.

E.g. To remove a remote repository named "origin", run:

git remote rm origin

git branch -d <branch-name>

Delete the specified branch only if it has been fully merged into your current branch or another branch. If the branch hasn't been merged, Git will refuse to delete it to prevent potential data loss.

E.g. To delete the 'test' branch only if it's merged, run:

git branch -d test

git branch -D <branch-name>

Forcefully delete the specified branch, even if it hasn't been fully merged into your current branch or any other branches. **Use with caution as this action cannot be undone easily.**

E.g. To forcefully delete the branch named 'test', run:

git branch -D test

git push [remote] --delete [branch_name]

Delete a branch on the remote repository.

E.g. To delete a branch named "feature" on the remote repository "origin", run:

git push origin --delete feature

git fetch --prune

Remove remote tracking branches that no longer exist on the remote.

E.g. To remove remote tracking branches that no longer exist on the remote repository "origin", run:

git fetch --prune

git log --graph --oneline --decorate --all

Visualize the commit history graph.

E.g. To visualize the commit history graph with one-line commit messages and decorations, run:

git log --graph --oneline --decorate --all

git log --since=[date]

Show commit history since a specific date.

E.g. To show commit history since January 1, 2022, run:

git log --since=2022-01-01

git log --until=[date]

Show commit history until a specific date.

E.g. To show commit history until December 31, 2022, run:

git log --until=2022-12-31

git log --stat

Display additional statistics in the commit log including date,time and author.

E.g. To display additional statistics in the commit log, run:

git log --stat

git shortlog

Summarize git log output by user in simple words.

E.g. To summarize git log output by user, run:

git shortlog

git log --graph

Display commit history in a more visual way.

E.g. To display commit history in a more visual way, run:

git log --graph

git diff [branch1] [branch2]

Show the differences between two branches.

E.g. To show the differences between the "main" and "feature" branches, run:

git diff main feature

git show-branch --all

Show branches, their commits, and their relationships.

E.g. To show branches, their commits, and their relationships, run:

git show-branch --all

git diff [commit1] [commit2]

Show the differences between two commits.

E.g. To show the differences between commits "abcd123" and "efgh456", run:

git diff abcd123 efgh456

git blame [file]

Show who last modified each line of a file and when.

E.g. To show who last modified each line of a file named "index.html" and when, run:

git blame index.html

git grep [pattern]

Search the contents of files in the repository for a pattern.

E.g. To search the contents of files in the repository for the pattern "TODO", run:

git grep "TODO"

git log --author="[author_name]"

Filter commit history by author.

E.g. To filter commit history by author name "Majid Ali", run:

git log --author="Majid Ali"

git log --grep="[commit_message]"

Filter commit history by commit message.

E.g. To filter commit history by commit message containing "bug fix", run:

git log --grep="bug fix"

git log --follow [file]

Show the history of a file, including renames.

E.g. To show the history of a file named "index.html", including renames, run:

git log --follow index.html

git diff --cached

Show changes staged for commit.

E.g. To show changes staged for commit, run:

git diff --cached

git commit --amend

Amend (to change or modify) the last commit with new changes.

E.g. To amend the last commit with new changes, run:

git commit --amend

git branch -M main

The `git branch -M main` command renames the current branch to 'main', using the `-M` flag to force the rename. This is useful for changing the default branch name, especially in scenarios where you want to adopt 'main' as the primary branch instead of 'master'. If a branch named 'main' already exists, it will be overwritten.

E.g. To rename the current branch to 'main', run:

git branch -M main

git branch -m [old_branch] [new_branch]

The `git branch -m` command renames an existing branch in your Git repository. If you provide only one argument, it renames the current branch to the specified name. This command is useful for improving branch naming conventions or correcting naming mistakes without affecting your commit history.

E.g. To rename the branch 'feature-xyz' to 'feature-abc', run: 1. For renaming the current branch: `git branch -m feature-abc` 2. For renaming a specific branch: `git branch -m feature-xyz feature-abc`

git branch -m feature-xyz feature-abc

git reflog

Show a log of all commits that have been referenced in your local repository.

E.g. To show a log of all commits that have been referenced in your local repository, run:

git reflog

git clean -df

Remove untracked files and directories from the working directory.

E.g. To remove untracked files and directories from the working directory, run:

git clean -df

git show-branch

Show branches and their commits.

E.g. To show branches and their commits, run:

git show-branch

git cherry-pick [commit]

Apply the changes introduced by the specified commit to the current branch.

E.g. To apply the changes introduced by a commit with hash "abcd123" to the current branch, run:

git cherry-pick abcd123

git remote show [remote_name]

Display information about a remote repository.

E.g. To display information about the remote repository named "origin", run:

git remote show origin

git mv [old_file] [new_file]

Rename or move a file.

E.g. To rename a file named "old.txt" to "new.txt", run:

git mv old.txt new.txt

git archive [branch_name] --format=zip --output=[output_file.zip]

Create a zip archive of a branch.

E.g. To create a zip archive of the "master" branch named "repo.zip", run:

git archive master --format=zip --output=repo.zip

git remote rename [old_name] [new_name]

Rename a remote repository.

E.g. To rename a remote repository named "origin" to "new-origin", run:

git remote rename origin new-origin

git remote set-url [remote_name] [new_url]

This command allows you to change the URL of a remote repository in your Git configuration. This is useful when the repository’s URL has changed, or you want to switch between HTTPS and SSH URLs for the remote.

E.g. To change the URL of the remote repository named "origin", run:

git remote set-url origin 

git bisect

Use binary search to find the commit that introduced a bug.

E.g. To use binary search to find the commit that introduced a bug, run:

git bisect

git submodule add [repository_url] [path]

Adds a new submodule to the current repository. This allows you to include an external Git repository inside your project, which can be tracked separately. The submodule will be added at the specified path.

E.g. To add a submodule from a repository and place it in a specific path, run:

git submodule add https://github.com/user/repo.git path/to/submodule

git submodule init

Initializes the submodules that have been defined in the .gitmodules file but are not yet set up in the local repository. It prepares the submodule configuration so that they can be fetched or updated later.

E.g. To initialize all submodules that are declared in the .gitmodules file, run:

git submodule init

git submodule update

Fetches the latest commits for all initialized submodules and checks them out in the working directory. This command ensures that the submodules are updated to match the versions specified in the main project.

E.g. To fetch and check out the latest changes for all submodules, run:

git submodule update

git submodule update -f --init megaset

Forces the update of a specific submodule (megaset) by fetching the submodule's latest content and initializing it if it hasn't been initialized yet. The `-f` option forces the update, and `--init` ensures the submodule is initialized if it hasn't been already. This command is useful if you want to ensure a submodule is fetched and checked out even in the case of conflicts or missing initialization.

E.g. To force the update and initialization of the submodule named `megaset`, run:

git submodule update -f --init megaset

git submodule foreach [command]

Executes a specified command in each of the submodule directories. This is useful for performing the same action (like pulling updates) across all submodules.

E.g. To run `git pull` in all submodules, run:

git submodule foreach git pull

git submodule deinit [path]

Deinitializes a submodule, removing it from the working directory but keeping its configuration in the .gitmodules file. This does not delete the submodule from the repository, but removes its files locally.

E.g. To deinitialize a submodule at a specific path, run:

git submodule deinit path/to/submodule

git submodule status

Displays the current status of each submodule in the repository, showing which commit each submodule is checked out at, whether there are uncommitted changes, and more.

E.g. To check the current status of all submodules, run:

git submodule status

git submodule sync

Updates the URL of submodules in the .git/config file to match those defined in the .gitmodules file. This is useful if the URL of a submodule has changed and you want to synchronize it with the latest configuration.

E.g. To synchronize the URLs of all submodules, run:

git submodule sync

git submodule remove [path]

Completely removes a submodule from the repository. This process involves deinitializing the submodule, removing the submodule entry from the .gitmodules file, and deleting the submodule's directory and related metadata.

E.g. To remove a submodule from the repository entirely, run:

git submodule deinit -f -- path/to/submodule
git rm -f path/to/submodule
rm -rf .git/modules/path/to/submodule
If you want to learn more about Git,Click Here