{"id":22031463,"url":"https://github.com/afirestriker/git-guide","last_synced_at":"2025-03-23T11:27:20.482Z","repository":{"id":46870499,"uuid":"514883089","full_name":"Afirestriker/git-guide","owner":"Afirestriker","description":"A comprehensive guide to the most commonly used Git commands, workflows, and best practices—covering about 70-80% of the commands you’ll use in day-to-day work at the organizational level. Whether you're a beginner or looking to deepen your Git knowledge, this repo will help you efficiently manage code, collaborate with teams, and navigate key Git.","archived":false,"fork":false,"pushed_at":"2024-11-07T04:58:56.000Z","size":64,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-28T18:13:37.241Z","etag":null,"topics":["git"],"latest_commit_sha":null,"homepage":"","language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Afirestriker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-17T15:34:26.000Z","updated_at":"2024-11-19T13:53:54.000Z","dependencies_parsed_at":"2023-02-14T14:30:38.749Z","dependency_job_id":null,"html_url":"https://github.com/Afirestriker/git-guide","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Afirestriker%2Fgit-guide","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Afirestriker%2Fgit-guide/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Afirestriker%2Fgit-guide/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Afirestriker%2Fgit-guide/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Afirestriker","download_url":"https://codeload.github.com/Afirestriker/git-guide/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245093942,"owners_count":20559816,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["git"],"created_at":"2024-11-30T08:18:31.180Z","updated_at":"2025-03-23T11:27:20.456Z","avatar_url":"https://github.com/Afirestriker.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Git Tutorial Repository\n\nThis 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 \u0026 push actions with **Remote**.\n\n## Table of Contents\n\n1. [Setting Up Git](#setting-up-git)\n2. [Initializing and Cloning Repositories](#initializing-and-cloning-repositories)\n3. [Branch Management](#branch-management)\n4. [Working with Files (status, add, and commit)](#working-with-files)\n5. [Git Stash](#git-stash)\n6. [Git Show](#git-show)\n7. [Git Diff](#git-diff)\n8. [Working with Remote Repositories](#working-with-remote-repositories)\n9. [Git Pull and Push On Remote](#git-pull-and-push-on-remote)\n10. [Git Cherry-Pick](#git-cherry-pick)\n11. [Gitk (GUI)](#gitk)\n\n\u003c!-- updated table of content\n## Table of contents \n- [Git Commands and Their Use](#git-commands-and-their-use)\n- [Initialized Git for New Repository](#initialized-git-for-new-repository)\n- [Clone Repository from GitHub](#clone-repository-from-github)\n- [Status, Add, Commit](#status-add-commit)\n- [Git Stash](#git-stash)\n  - [Stash Changes](#stash-changes)\n  - [List Stashes](#list-stashes)\n  - [Apply Stash](#apply-stash)\n  - [Pop Stash](#pop-stash)\n  - [Drop Stash](#drop-stash)\n  - [Clear All Stashes](#clear-all-stashes)\n- [Create a New Branch](#create-a-new-branch)\n- [Merge Branches](#merge-branches)\n- [Working with Remote Repository](#working-with-remote-repository)\n  - [Git Pull from GitHub](#git-pull-from-github)\n  - [Git Push to GitHub](#git-push-to-github)\n  - [Push Branch to GitHub](#push-branch-to-github)\n--\u003e\n\n---\n\n## Setting Up Git\n\nBefore 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.\n\n### Set Global Username and Email\n\n```bash\ngit config --global user.name \"Your Name\"\ngit config --global user.email \"your-email@example.com\"\n```\n\nTo set username and email for the current repository only (overriding global settings), omit `--global`.\n\n---\n\n## Initializing and Cloning Repositories\n\n### Initialize a New Git Repository\n\nTo initialize a new Git repository in the current directory:\n\n```bash\ngit init\n```\n\n### Clone an Existing Remote Repository\n\nTo clone a repository from Remote:\n\n```bash\ngit clone \u003cremote-url\u003e\n```\n\nTo clone a specific subfolder from the repository:\n\n```bash\ngit clone \u003cremote-url\u003e \u003cfolder-name\u003e\n```\n\nThis will create a new directory with the repository name, or clone only the specified folder.\n\n### Check Files in the Directory\n\nTo view all files, including hidden ones:\n\n```bash\nls -la\n```\n\n---\n\n## Branch Management\n\n### Create and Switch Branches\n\nTo create a new branch:\n\n```bash\ngit branch \u003cbranch-name\u003e\n```\n\nTo rename the current branch:\n\n```bash\ngit branch -m \u003cnew-branch-name\u003e\n```\n\nTo list all branches:\n\n```bash\ngit branch # Lists only local branches, showing the currently active branch with `*`\n\ngit branch -l # Lists local branches only (same as `git branch`)\n\ngit brahcn -r # Lists only remote-tracking branches, prefixed by the remote name (e.g., `origin/`)\n\ngit branch -a # Lists both local branches and remote-tracking branches\n```\n\nTo switch to an existing branch:\n\n```bash\ngit switch \u003cbranch-name\u003e \n# or\ngit checkout \u003cbranch-name\u003e\n```\n\nTo create and switch to a new branch in one step:\n\n```bash\ngit switch -c \u003cnew-branch-name\u003e\n# or\ngit checkout -b \u003cnew-branch-name\u003e\n```\n\nTo create a new branch or reset an existing branch and switch to it.\n\n```bash\ngit switch -C \u003cnew-branch-name\u003e\n# or\ngit checkout -B \u003cnew-banch-name\u003e\n```\n\n### Merge Branches\n\nTo merge changes from another branch into the current branch:\n\n1. Switch to the branch you want to merge into (e.g., `main` or `master`):\n\n   ```bash\n   git checkout master\n   ```\n\n2. Merge the desired branch:\n\n   ```bash\n   git merge \u003cbranch-name\u003e\n   ```\n\n### Delete a Branch\n\nTo delete a branch that is no longer needed:\n\n```bash\ngit branch -d \u003cbranch-name\u003e # only if it has been fully merged to it's target branch\n```\n```bash\ngit branch -D \u003cbranch-name\u003e # force delete a branch, regardless of whether it has been merged or not\n```\n\n---\n\n## Working with Files\n\n### Check the Status of Your Repository\n\nTo see the current status of your working directory (untracked, modified, staged files):\n\n```bash\ngit status\n```\n\nFor a brief, summary view:\n\n```bash\ngit status --short\n```\n\n### Add Files to Staging Area\n\nTo stage a specific file:\n\n```bash\ngit add \u003cfile-name\u003e\n```\n\nTo stage all changes:\n\n```bash\ngit add -A\n```\n\nAfter staging, you can check the status again to confirm the changes are staged for commit.\n\n### Commit Changes\n\nTo commit the staged changes with a custom message:\n\n```bash\ngit commit -m \"Your commit message\"\n```\n\nTo combine both staging and committing into one step:\n\n```bash\ngit commit -am \"Your commit message\"\n```\n\n### View Commit History\n\nTo view the commit history of the repository:\n\n```bash\ngit log\n```\n\nTo view the last `n` commits:\n\n```bash\ngit log -n \u003cnumber\u003e\n```\n\nPress `q` to exit the log view.\n\n---\n\n## Git Stash\n\n`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.\n\n### 1. Stash Changes\n\n- **Purpose**: Save your local changes (including tracked files) without committing them, but **not** untracked files.\n- **Command**: \n  ```bash\n  git stash\n  ```\n\n- If you want to **stash both tracked and untracked files**, use the `-u` option:\n  ```bash\n  git stash -u\n  ```\n\n### 2. List Stashes\n\n- **Purpose**: See a list of all stashes you've saved.\n\n- **Command**:\n  ```bash\n  git stash list # list all saved stashes, along with reference ID\n  ```\n\n### 3. Apply Stash\n\n- **Purpose**: Reapply the changes from a stash to your working directory.\n\n- **Command**:\n  ```bash\n  git stash apply [stash@{n}]\n  ```\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 from `git stash list`.\n\n### 4. Pop Stash\n\n- **Purpose**: Apply a stash and **remove it** from the stash list.\n\n- **Command**:\n  ```bash\n  git stash pop [stash@{n}]\n  ```\n\n### 5. Drop Stash\n\n- **Purpose**: Delete a stash from the stash list.\n\n- **Command**:\n  ```bash\n  git stash drop [stash@{n}]\n  ```\n  Use this to delete a specific stash by its ID.\n\n### 6. Clear All Stashes\n\n- **Purpose**: Remove all stashes.\n\n- **Command**:\n  ```bash\n  git stash clear\n  ```\n\n---\n\n## Git Show\n\n`git show` displays detailed information about a commit, object, or tag. It’s often used to view the content of a specific commit.\n\n- **Purpose**: Show detailed information about a commit or object.\n\n- **Command**:\n  ```bash\n  git show \u003ccommit-hash\u003e # show the commit details, including the commit message, author, date, and the changes made (diff)\n  ```\n\n- **Additional Options**:\n  ```bash\n  git show --stat \u003ccommit-hash\u003e # Shows a summary of the files modified in the commit.\n  git show --patch \u003ccommit-hash\u003e # Shows the patch (diff) introduced by the commit.\n  ```\n\n---\n\n## Git Diff\n\nThe `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.\n\n### Viewing Changes in the Working Directory\n\nTo view the changes you have made in your working directory (but not yet staged), use:\n\n```bash\ngit diff\ngit diff --color # display the diff with color-coded changes (additions in green and deletions in red)\ngit diff --word-diff # view word-level differences.\n```\n\nThis 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.\n\n\u003e Note: you can use the flags like `--color`, `--word-diff`, etc... with other `git diff` commands as well. \n\n### Viewing Staged Changes\n\nTo see what changes are staged for the next commit (i.e., what you have added to the index), use:\n\n```bash\ngit diff --cached # shows differences between the staging area and the last commit.\n```\n\n### Comparing Two Branches/ Commits/ Tags\n\nUseful for seeing what changes have been made on different branches before merging them. \nTo compare the differences between two branches, use:\n\n```bash\ngit diff \u003cbranch-1\u003e..\u003cbranch-2\u003e # compare differences between branches\n```\n\n```bash\ngit diff \u003ccommit-hash-1\u003e \u003ccommit-hash-2\u003e # compare differences between commits\n```\n\n```bash\ngit diff \u003ctag-1\u003e \u003ctag-2\u003e # compare differences between tags\n```\n\n### Viewing Specific Files or Directories\n\nYou 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:\n\n```bash\ngit diff \u003cfile-path\u003e\n```\n\nThis will show the differences for the file at `\u003cfile-path\u003e` in your working directory or staging area.\n\n---\n\n## Working with Remote Repositories\n\nWhen working with remote repositories, you'll often need to add, remove, or update remotes.\n\n### Add a Remote Repository\n\nTo link your local repository to a remote:\n\n```bash\ngit remote add origin \u003cremote-url\u003e\n```\n\nYou can add multiple remotes with different names, e.g., `origin`, `upstream`, etc.\n\n### Update a Remote URL\n\nTo update the URL for a remote repository:\n\n```bash\ngit remote set-url origin \u003cremote-url\u003e\n```\n\n### Remove a Remote Repository\n\nTo remove a remote repository:\n\n```bash\ngit remote rm origin\n# or\ngit remote remove origin\n```\n\n### List All Remote Repositories\n\nTo view all remotes associated with the repository:\n\n```bash\ngit remote -v\n```\n\n### Fetch Updates from Remote Repository\n\nTo fetch all changes from the remote repository without merging:\n\n```bash\ngit fetch origin # get all the latest changes from the remote repository (e.g., new branches, tags, or updates to existing branches)\n```\n\n```bash\ngit fetch origin \u003cbranch-name\u003e # fetch a specific branch (e.g., a feature branch)\n```\n\n---\n\n## Git Pull and Push On Remote\n\n### Pull Changes from Remote\n\nTo fetch and merge changes from the remote repository into your local branch:\n\n```bash\ngit pull origin \u003cbranch-name\u003e\n```\n\nThis is equivalent to running `git fetch` followed by `git merge`.\n\n### Push Changes to Remote\n\nTo push your local commits to the remote repository:\n\n```bash\ngit push origin \u003cbranch-name\u003e\n```\n\nThis will push your changes to the corresponding branch on Remote.\n\n### Pull a Branch from Remote\n\nTo fetch and checkout a new branch from the remote repository:\n\n```bash\ngit pull origin \u003cbranch-name\u003e\n```\n\n### Push a New Branch to Remote\n\nTo create a new branch locally, make changes, commit, and push to Remote:\n\n```bash\ngit branch -b \u003cbranch-name\u003e # create a new branch on local\ngit push origin \u003cbranch-name\u003e # push new-branch to remote\n```\n\n---\n\n## Git Cherry-Pick\n\nThe `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.\n\n### Usage\n\nTo cherry-pick a single commit, use the following syntax:\n\n```bash\ngit cherry-pick \u003ccommit-hash\u003e\n```\n\nWhere `\u003ccommit-hash\u003e` is the hash of the commit you want to apply to your current branch.\n\n### Example\n\nLet'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:\n\n```bash\ngit log main\n```\n\nOnce you have the commit hash, cherry-pick the commit:\n\n```bash\ngit cherry-pick abc1234\n```\n\nThis 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.\n\n### Handling Conflicts\n\nIf 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:\n\n```bash\ngit add \u003cresolved-file\u003e\ngit cherry-pick --continue\n```\n\nIf you want to abort the cherry-pick operation, you can run:\n\n```bash\ngit cherry-pick --abort\n```\n\n### Cherry-Picking Multiple Commits\n\nYou can also cherry-pick multiple commits at once by providing a commit range:\n\n```bash\ngit cherry-pick \u003cstart-commit\u003e^..\u003cend-commit\u003e\n```\n\nThis will apply all commits from `\u003cstart-commit\u003e` to `\u003cend-commit\u003e` (inclusive) to your current branch.\n\n---\n\n## Gitk\n\n`gitk` is a graphical user interface (GUI) for Git that allows you to visualize and explore the history of a repository.\n\n- **Purpose**: Open a GUI to view commit history, branches, and tags.\n\n- **Command**:\n  ```bash\n  gitk\n  ```\n  \n- **Usage**: Great for visualizing your commit history and performing complex operations like merges or rebase in a more intuitive way.\n\n---\n\n## Conclusion\n\nThis 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.\n****\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fafirestriker%2Fgit-guide","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fafirestriker%2Fgit-guide","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fafirestriker%2Fgit-guide/lists"}