Git commands along with examples

Absolutely, documenting all of these Git commands with examples will be quite extensive, but I can certainly provide you with a brief description and examples for some of the key Git commands.

Here’s a start:

1. Core Commands:

git init

Initializes a new Git repository.

git clone

Clones a repository into a new directory.

git clone https://github.com/example/repo.git

git add

Adds changes in the working directory to the staging area.

git add file.txt

git commit

Records changes to the repository.

git commit -m "Commit message"

git status

Shows the status of changes as untracked, modified, or staged.

git diff

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

git diff file.txt

git checkout

Switches branches or restores working tree files.

git checkout branch_name

git reset

Resets current HEAD to the specified state.

git reset --hard HEAD~1

git log

Shows the commit logs.

git show

Displays information about a Git object.

git tag

Creates, lists, or deletes tags.

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

git push

Pushes changes to a remote repository.

git push origin master

git pull

Fetches from and integrates with another repository or a local branch.

git pull origin master

2. Branching:

git branch

Lists, creates, or deletes branches.

git checkout -b

Creates a new branch and switches to it.

git checkout -b new_branch

git merge

Merges branches.

git merge feature_branch

git rebase

Reapply commits on top of another base tip.

git rebase master

git cherry-pick

Apply the changes introduced by some existing commits.

git cherry-pick commit_hash

3. Merging:

git merge

Joins two or more development histories together.

git merge feature_branch

git rebase

Reapply commits on top of another base tip.

git rebase master

4. Stashing:

git stash

Stashes changes in the working directory.

git stash save "Work in progress"

git stash pop

Restores the most recently stashed files.

git stash list

Lists all stashed changes.

git stash apply

Applies a stashed change to the working directory.

git stash apply stash@{2}

git stash drop

Discards the most recently stashed changes.

git stash drop

5. Remotes:

git remote

Shows all remote URLs.

git remote add

Adds a remote repository.

git remote add origin https://github.com/example/repo.git

git remote remove

Removes a remote repository.

git remote remove origin

git fetch

Downloads objects and refs from another repository.

git pull

Fetches from and integrates with another repository or a local branch.

git pull origin master

git push

Pushes changes to a remote repository.

git push origin master

git clone --mirror

Clone a repository into a bare repository.

git clone --mirror https://github.com/example/repo.git

6. Configuration:

git config

Sets configuration values for your user name, email, etc.

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

git global config

Sets global configuration values.

git config --global core.editor "nano"

7. Plumbing:

Descriptions and examples for plumbing commands can be quite technical and are typically used for scripting. If you’re interested, I can provide more details.

8. Porcelain:

git blame

Shows what revision and author last modified each line of a file.

git blame file.txt

git bisect

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

git bisect start
git bisect bad
git bisect good a0c56e2

git grep

Print lines matching a pattern.

git grep "search_term"

git log

Shows the commit logs.

git merge

Joins two or more development histories together.

git merge feature_branch

git push

Pushes changes to a remote repository.

git push origin master

git rebase

Reapply commits on top of another base tip

.

git rebase master

git reset

Resets current HEAD to the specified state.

git reset --hard HEAD~1

git show

Displays information about a Git object.

git show commit_hash

git tag

Creates, lists, or deletes tags.

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

9. Alias:

git config --global alias.

Create a shortcut or alias for a Git command.

git config --global alias.co checkout

10. Hook:

git config --local core.hooksPath

Sets the path to the directory where scripts and hooks are kept.

git config --local core.hooksPath .githooks

This is a comprehensive list of Git commands with brief descriptions and examples where applicable.

Uncategorized