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"
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"
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
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
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
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 .
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"
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
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
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
List all branches in the repository.
E.g. To see all branches in the repository and current selected branch, run:
git branch
Switch to the specified branch.
E.g. To switch to a branch named "feature", run:
git checkout feature
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
Switch to the specified branch.
E.g. To switch to a branch named "feature", run:
git switch feature
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
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
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
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
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
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
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
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"
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
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
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
Remove a specific stash.
E.g. To remove a stash named "stash@{0}", run:
git stash drop stash@{0}
Remove all stashed changes.
E.g. To remove all stashed changes, run:
git stash clear
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
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"
Delete a tag from the local repository.
E.g. To delete the tag named "v1.0", run:
git tag -d v1.0
List all tags in the repository.
E.g. To list all tags, run:
git tag -l
Show detailed information about a specific tag.
E.g. To show details about the tag named "v1.0", run:
git show v1.0
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
Push all tags to the remote repository.
E.g. To push all local tags to the remote repository, run:
git push --tags
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
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
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
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
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
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
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
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
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
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
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
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
Unstage changes without altering the file in the working directory.
E.g. To unstage changes for 'myfile.txt', run:
git restore --staged myfile.txt
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/
Cleanup unnecessary files and optimize the local repository.
E.g. To run garbage collection and optimize the repository, run:
git gc
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'
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
Remove a remote repository.
E.g. To remove a remote repository named "origin", run:
git remote rm origin
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
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
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
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
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
Show commit history since a specific date.
E.g. To show commit history since January 1, 2022, run:
git log --since=2022-01-01
Show commit history until a specific date.
E.g. To show commit history until December 31, 2022, run:
git log --until=2022-12-31
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
Summarize git log output by user in simple words.
E.g. To summarize git log output by user, run:
git shortlog
Display commit history in a more visual way.
E.g. To display commit history in a more visual way, run:
git log --graph
Show the differences between two branches.
E.g. To show the differences between the "main" and "feature" branches, run:
git diff main feature
Show branches, their commits, and their relationships.
E.g. To show branches, their commits, and their relationships, run:
git show-branch --all
Show the differences between two commits.
E.g. To show the differences between commits "abcd123" and "efgh456", run:
git diff abcd123 efgh456
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
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"
Filter commit history by author.
E.g. To filter commit history by author name "Majid Ali", run:
git log --author="Majid Ali"
Filter commit history by commit message.
E.g. To filter commit history by commit message containing "bug fix", run:
git log --grep="bug fix"
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
Show changes staged for commit.
E.g. To show changes staged for commit, run:
git diff --cached
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
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
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
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
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
Show branches and their commits.
E.g. To show branches and their commits, run:
git show-branch
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
Display information about a remote repository.
E.g. To display information about the remote repository named "origin", run:
git remote show origin
Rename or move a file.
E.g. To rename a file named "old.txt" to "new.txt", run:
git mv old.txt new.txt
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
Rename a remote repository.
E.g. To rename a remote repository named "origin" to "new-origin", run:
git remote rename origin new-origin
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
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
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
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
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
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
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
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
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
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
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