This repository serves as a comprehensive guide to using Git. It includes step-by-step instructions for basic Git operations such as cloning repositories, managing branches, and performing pull & push actions with Remote.
- Setting Up Git
- Initializing and Cloning Repositories
- Branch Management
- Working with Files (status, add, and commit)
- Git Stash
- Git Show
- Git Diff
- Working with Remote Repositories
- Git Pull and Push On Remote
- Git Cherry-Pick
- Gitk (GUI)
Before starting with Git, you need to configure your global Git settings such as your username and email. These settings will be used for all repositories on your local machine unless overridden by repository-specific settings.
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"To set username and email for the current repository only (overriding global settings), omit --global.
To initialize a new Git repository in the current directory:
git initTo clone a repository from Remote:
git clone <remote-url>To clone a specific subfolder from the repository:
git clone <remote-url> <folder-name>This will create a new directory with the repository name, or clone only the specified folder.
To view all files, including hidden ones:
ls -laTo create a new branch:
git branch <branch-name>To rename the current branch:
git branch -m <new-branch-name>To list all branches:
git branch # Lists only local branches, showing the currently active branch with `*`
git branch -l # Lists local branches only (same as `git branch`)
git brahcn -r # Lists only remote-tracking branches, prefixed by the remote name (e.g., `origin/`)
git branch -a # Lists both local branches and remote-tracking branchesTo switch to an existing branch:
git switch <branch-name>
# or
git checkout <branch-name>To create and switch to a new branch in one step:
git switch -c <new-branch-name>
# or
git checkout -b <new-branch-name>To create a new branch or reset an existing branch and switch to it.
git switch -C <new-branch-name>
# or
git checkout -B <new-banch-name>To merge changes from another branch into the current branch:
-
Switch to the branch you want to merge into (e.g.,
mainormaster):git checkout master
-
Merge the desired branch:
git merge <branch-name>
To delete a branch that is no longer needed:
git branch -d <branch-name> # only if it has been fully merged to it's target branchgit branch -D <branch-name> # force delete a branch, regardless of whether it has been merged or notTo see the current status of your working directory (untracked, modified, staged files):
git statusFor a brief, summary view:
git status --shortTo stage a specific file:
git add <file-name>To stage all changes:
git add -AAfter staging, you can check the status again to confirm the changes are staged for commit.
To commit the staged changes with a custom message:
git commit -m "Your commit message"To combine both staging and committing into one step:
git commit -am "Your commit message"To view the commit history of the repository:
git logTo view the last n commits:
git log -n <number>Press q to exit the log view.
git stash is used to temporarily save changes in your working directory that are not yet ready to commit. This is useful when you need to switch branches but don’t want to commit partial work.
-
Purpose: Save your local changes (including tracked files) without committing them, but not untracked files.
-
Command:
git stash
-
If you want to stash both tracked and untracked files, use the
-uoption:git stash -u
-
Purpose: See a list of all stashes you've saved.
-
Command:
git stash list # list all saved stashes, along with reference ID
-
Purpose: Reapply the changes from a stash to your working directory.
-
Command:
git stash apply [stash@{n}][stash@{n}]is optional. If not specified, the most recent stash will be applied. If you want to apply a specific stash, use the stash ID fromgit stash list.
-
Purpose: Apply a stash and remove it from the stash list.
-
Command:
git stash pop [stash@{n}]
-
Purpose: Delete a stash from the stash list.
-
Command:
git stash drop [stash@{n}]Use this to delete a specific stash by its ID.
-
Purpose: Remove all stashes.
-
Command:
git stash clear
git show displays detailed information about a commit, object, or tag. It’s often used to view the content of a specific commit.
-
Purpose: Show detailed information about a commit or object.
-
Command:
git show <commit-hash> # show the commit details, including the commit message, author, date, and the changes made (diff)
-
Additional Options:
git show --stat <commit-hash> # Shows a summary of the files modified in the commit. git show --patch <commit-hash> # Shows the patch (diff) introduced by the commit.
The git diff command is used to show the differences between various states of your repository. You can use it to compare changes in your working directory (not staged), staging area, , between commits, or between branches. By using different options like --cached, --color, and --word-diff, you can customize the output to fit your needs.
To view the changes you have made in your working directory (but not yet staged), use:
git diff
git diff --color # display the diff with color-coded changes (additions in green and deletions in red)
git diff --word-diff # view word-level differences.This will show the differences between your working directory and the index (staging area), i.e., what changes you have made that are not yet staged for commit.
Note: you can use the flags like
--color,--word-diff, etc... with othergit diffcommands as well.
To see what changes are staged for the next commit (i.e., what you have added to the index), use:
git diff --cached # shows differences between the staging area and the last commit.Useful for seeing what changes have been made on different branches before merging them. To compare the differences between two branches, use:
git diff <branch-1>..<branch-2> # compare differences between branchesgit diff <commit-hash-1> <commit-hash-2> # compare differences between commitsgit diff <tag-1> <tag-2> # compare differences between tagsYou can limit the git diff output to a specific file or directory by specifying it after the command. For example, to see the changes in a particular file:
git diff <file-path>This will show the differences for the file at <file-path> in your working directory or staging area.
When working with remote repositories, you'll often need to add, remove, or update remotes.
To link your local repository to a remote:
git remote add origin <remote-url>You can add multiple remotes with different names, e.g., origin, upstream, etc.
To update the URL for a remote repository:
git remote set-url origin <remote-url>To remove a remote repository:
git remote rm origin
# or
git remote remove originTo view all remotes associated with the repository:
git remote -vTo fetch all changes from the remote repository without merging:
git fetch origin # get all the latest changes from the remote repository (e.g., new branches, tags, or updates to existing branches)git fetch origin <branch-name> # fetch a specific branch (e.g., a feature branch)To fetch and merge changes from the remote repository into your local branch:
git pull origin <branch-name>This is equivalent to running git fetch followed by git merge.
To push your local commits to the remote repository:
git push origin <branch-name>This will push your changes to the corresponding branch on Remote.
To fetch and checkout a new branch from the remote repository:
git pull origin <branch-name>To create a new branch locally, make changes, commit, and push to Remote:
git branch -b <branch-name> # create a new branch on local
git push origin <branch-name> # push new-branch to remoteThe git cherry-pick command is used to apply the changes introduced by a specific commit from one branch onto another. This is useful when you want to bring over a commit (or multiple commits) without merging the entire branch.
To cherry-pick a single commit, use the following syntax:
git cherry-pick <commit-hash>Where <commit-hash> is the hash of the commit you want to apply to your current branch.
Let's say you are on the feature-branch, and you want to apply a commit from the main branch to your current branch. First, you need to identify the commit hash of the commit you want to cherry-pick. You can do this by running:
git log mainOnce you have the commit hash, cherry-pick the commit:
git cherry-pick abc1234This will apply the changes from commit abc1234 to the current branch. If there are no conflicts, Git will automatically create a new commit for this change.
If there are conflicts during the cherry-pick, Git will stop and allow you to resolve them. After resolving the conflicts, you can continue the cherry-pick process with:
git add <resolved-file>
git cherry-pick --continueIf you want to abort the cherry-pick operation, you can run:
git cherry-pick --abortYou can also cherry-pick multiple commits at once by providing a commit range:
git cherry-pick <start-commit>^..<end-commit>This will apply all commits from <start-commit> to <end-commit> (inclusive) to your current branch.
gitk is a graphical user interface (GUI) for Git that allows you to visualize and explore the history of a repository.
-
Purpose: Open a GUI to view commit history, branches, and tags.
-
Command:
gitk
-
Usage: Great for visualizing your commit history and performing complex operations like merges or rebase in a more intuitive way.
This guide covers essential Git commands for interacting with local and remote repositories. By following these steps, you'll be able to effectively manage your projects, collaborate with others, and keep your work organized.