{"id":21758944,"url":"https://github.com/anshul-sonpure/basic-git-commands","last_synced_at":"2026-05-15T13:03:28.999Z","repository":{"id":43864774,"uuid":"436905886","full_name":"Anshul-Sonpure/Basic-Git-Commands","owner":"Anshul-Sonpure","description":"Basic Git Commands and Workflow","archived":false,"fork":false,"pushed_at":"2022-10-10T11:43:22.000Z","size":12594,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-10T13:38:39.237Z","etag":null,"topics":["git","git-cheatsheet","git-cheatsheets","git-commands","git-diff","git-merge","github","version-control"],"latest_commit_sha":null,"homepage":"","language":null,"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/Anshul-Sonpure.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":"2021-12-10T08:31:41.000Z","updated_at":"2023-04-05T15:53:53.000Z","dependencies_parsed_at":"2023-01-19T19:33:51.561Z","dependency_job_id":null,"html_url":"https://github.com/Anshul-Sonpure/Basic-Git-Commands","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/Anshul-Sonpure/Basic-Git-Commands","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anshul-Sonpure%2FBasic-Git-Commands","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anshul-Sonpure%2FBasic-Git-Commands/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anshul-Sonpure%2FBasic-Git-Commands/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anshul-Sonpure%2FBasic-Git-Commands/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Anshul-Sonpure","download_url":"https://codeload.github.com/Anshul-Sonpure/Basic-Git-Commands/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Anshul-Sonpure%2FBasic-Git-Commands/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33067476,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-15T11:35:32.926Z","status":"ssl_error","status_checked_at":"2026-05-15T11:35:31.362Z","response_time":103,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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","git-cheatsheet","git-cheatsheets","git-commands","git-diff","git-merge","github","version-control"],"created_at":"2024-11-26T11:24:41.660Z","updated_at":"2026-05-15T13:03:28.980Z","avatar_url":"https://github.com/Anshul-Sonpure.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"A Complete List of curated Git Commands\n----\n\nThis tutorial will help you get started with Git.\n\n\u003e **Audience of this tutorial are beginners so don't expect advanced concepts.**\n\nBefore we start make sure `git --version` command successfully returns. If the output is something like **command not found** then please make sure Git is properly installed on your machine. My machine output is shown below. **Output on your machine might be different.**\n\n```bash\n$ git --version\n```\n```\ngit version 2.5.4 (Apple Git-61)\n```\n\n\u003e **Commands that a user should type on his/her terminal are prefixed with the shell prompt symbol `$`.  The output of command follows the command. Also, you don't have to type `$` on your terminal.**\n\nMake sure you have configured your user with Git.  This information will be used by Git.\n\n```bash\n$ git config --global user.name \"Your name\"\n$ git config --global user.email \"Your email\"\n```\n\nThe command shown above populate a file named `.gitconfig` in your user home directory with configuration information.\n\n```bash\n$ cat ~/.gitconfig\n```\n```\n[user]\n\tname = Anshul Sonpure\n\temail = anshul.sonpure@outlook.com\n[push]\n\tdefault = simple\n```\n\n## Table of Contents\n\n* Why version control system?\n* What the heck is Git?\n* Git basics\n  * git init\n  * git status\n  * git add\n  * git commit\n  * git rm\n  * git log\n  * .gitignore files\n  * git diff\n  * git diff commits\n* Working with branches\n  * git branch\n  * git checkout\n  * git merge\n* Working with remote repositories\n\t* git remote\n\t* git pull\n\t* git push\n\t* git clone\n* Github basics\n\t* Why and What Pull Requests?\n* Advanced topics\n\t* git rebase\n\t* git cherry-pick\n\t* git stash\n  * git alias\n* Using Git from within your IDE(Eclipse)\n* Using a Git GUI tool\n* Useful Git commands\n\n\nVCS: Version Control System\n----\n\nA version control system is a kind of database for storing your software project source code. It lets you save a snapshot of your complete project at any time you want. When you later take a look at an older snapshot (let's start calling it \"version\").\n\nYou can use version control system to store:\n\n1. Software source code\n2. Text files\n3. Writing books\n4. Sharing datasets\n\n\n## Why version control system?\n\nYou should use version control because:\n\n1. Collaboration\n2. Storing Versions\n3. Restoring Previous Versions\n4. Understanding What Happened\n5. Backup\n\n\n## What the heck is Git?\n\nGit is a distributed version control system. It was developed by Linus Torvalds in 2005 for linux developers.\n\nEvery Git working directory is a full-fledged repository with complete history and full version-tracking capabilities, independent of network access or a central server.\n\n\u003cimg src=\"https://upload.wikimedia.org/wikipedia/commons/e/e0/Git-logo.svg\" width=\"150\" height=\"150\"\u003e\n\nDistributed or Decentralized VCS allows many software developers to work on a project without requiring them to share a common network.\n\n## Git basics\n\nLet's start by creating an empty directory `git-playground`. Navigate to a convenient location on your local file system and create a new directory.\n\n```bash\n$ mkdir git-playground \u0026\u0026 cd git-playground\n```\n\n### git init\n\nTo make any repository a Git managed repository, you will type the following command. These commands should be typed from inside the `git-playground` directory.\n\n```bash\n$ git init\n```\n```\nInitialized empty Git repository in ~/git-playground/.git/\n```\n\nThis will create a directory named `.git` inside the `git-playground` directory.\n\nThe `.git` directory structure would look like as shown below.\n\n\u003e **To use `tree` command, you have to install it on your machine. The `tree` command is not required for this tutorial. If you are on mac, then you can use package manager like brew `brew install tree`.**\n\n```bash\n$ tree -a .git\n```\n```\n.git\n|-- HEAD\n|-- config\n|-- hooks\n|   `-- post-commit\n|-- objects\n|   |-- info\n|   `-- pack\n`-- refs\n    |-- heads\n    `-- tags\n\n7 directories, 3 files\n```\n\n`.git` is a directory where Git stores all the data. **Don't mess up with `.git` directory.**  The only file that you should change is `config`.\n\n```bash\n$ cat .git/config\n```\n```\n[core]\n\trepositoryformatversion = 0\n\tfilemode = true\n\tbare = false\n\tlogallrefupdates = true\n\tignorecase = true\n\tprecomposeunicode = true\n```\n\nIf you want to override user for this repository then you can add user section by either editing `.git/config` file or using the `git config` command.\n\n```bash\n$ git config --local user.name \"anshul-sonpure\"\n```\n\nIf you view contents of the `.git/config` file now, then you will see your change.\n\n```bash\n$ cat .git/config\n```\n\n```\n[core]\n\trepositoryformatversion = 0\n\tfilemode = true\n\tbare = false\n\tlogallrefupdates = true\n\tignorecase = true\n\tprecomposeunicode = true\n[user]\n\tname = anshul-sonpure\n```\n\n\u003e **Please revert this change else your commits will be committed using this user name.**\n\n### git status\n\nTime and again we will need to know status of our Git repository. By status it means, what needs to be added to the index, what needs to be committed, is there any change that we have to commit, etc. This is the command that you will use most.\n\n```bash\n$ git status\n```\n\n```\nOn branch master\n\nInitial commit\n\nnothing to commit (create/copy files and use \"git add\" to track)\n```\n\n### git add\n\nLet's create a new file `README.md` and add a simple stupid message.\n\n```bash\n$ echo \"# Git Playground\" \u003e\u003e README.md\n```\n\n\u003e **It is a good practice to create a file named README.md in your repository root that tells purpose of the repository.**\n\nNow, check the status of the Git repository.\n\n```bash\n$ git status\n```\n```\nOn branch master\n\nInitial commit\n\nUntracked files:\n  (use \"git add \u003cfile\u003e...\" to include in what will be committed)\n\n\tREADME.md\n\nnothing added to commit but untracked files present (use \"git add\" to track)\n```\n\n**git add** stage the changes so they are picked by the next commit. In Git, you can't commit something until you make it trackable.\n\n```bash\n$ git add README.md\n```\n\nNow, check the status of your Git repository.\n\n```bash\n$ git status\n```\n\n```\nOn branch master\n\nInitial commit\n\nChanges to be committed:\n  (use \"git rm --cached \u003cfile\u003e...\" to unstage)\n\n\tnew file:   README.md\n```\n\n\u003e **If you have multiple files then you can use `git add \u003cfile1\u003e \u003cfile2\u003e .. \u003cfilen\u003e` or `git add --all` to stage them in one command.**\n\n### git commit\n\nOnce you have a smallest working change, you should commit it to your version control system. Smallest working change could be a test case, a small piece of functional code, a line or paragraph in your text file, etc. **You should commit often and daily.**\n\nAfter staging your changes, next step is to commit them into your local Git repository. To do that, we will use commit command as shown below.\n\n```bash\n$ git commit -m \"first commit\"\n```\n```\n[master (root-commit) 26afc7f] first commit\n 1 file changed, 1 insertion(+)\n create mode 100644 README.md\n```\n\nStaging and committing are two different steps in Git. You can't commit a change until it has been staged. If you are working with a tracked file, then you can do both of these steps in a single command.\n\n```bash\n$ echo \"This is my Git playground.\"  \u003e\u003e README.md\n```\n\nCheck the status of your repository. This time it will say it is a modified change.\n\n```bash\n$ git status\n```\n```\nOn branch master\nChanges not staged for commit:\n  (use \"git add \u003cfile\u003e...\" to update what will be committed)\n  (use \"git checkout -- \u003cfile\u003e...\" to discard changes in working directory)\n\n\tmodified:   README.md\n\nno changes added to commit (use \"git add\" and/or \"git commit -a\")\n```\n\nTo stage and commit a change in a tracked file, you can use following command.\n\n```bash\n$ git commit -am \"second commit\"\n```\n\n### git rm\n\nLet's suppose we added a bad file to the Git index that we want to remove.\n\n```bash\n$ echo \"my stupid file\" \u003e\u003e stupid.txt\n$ git add stupid.txt\n```\n\nTo remove a bad file from the Git index, we can use Git `rm` command.\n\n```bash\n$ git rm -f stupid.txt\n```\n```\nrm 'stupid.txt'\n```\n\n### git log\n\nTo view history of your commit logs, use the following command.\n\n```bash\n$ git log\n```\n```\ncommit abae2503d91f6ac2907bd0193ba323e10e73077c\nAuthor: Shekhar Gulati \u003cshekhargulati84@gmail.com\u003e\nDate:   Thu Jan 14 05:11:19 2016 +0530\n\n    second commit\n\ncommit bb2b56961e5f2a52e74af316fdb074592b0dbf16\nAuthor: Shekhar Gulati \u003cshekhargulati84@gmail.com\u003e\nDate:   Thu Jan 14 05:11:06 2016 +0530\n\n    first commit\n```\n\nIf you want to see a commit in one line, then use `oneline` option.\n\n```bash\n$ git log --oneline\n```\n\nTo make it look a bit better\n\n```bash\n$ git log --oneline --decorate\n```\n\nSearching through logs\n\n```bash\n$ git log --oneline --grep \"first\"\n```\n\nSearching by author\n\n```bash\n$ git log --oneline --decorate --author Shekhar\n```\n\nYou can format the way you want by using a formatter. Learn more about formatter from here [https://git-scm.com/docs/pretty-formats](https://git-scm.com/docs/pretty-formats)\n\n```bash\n$ git log --pretty=format:\"%h %aN %ar\" --date=short\n```\n\nThere are many more options. You can refer to help `git help log` for more details.\n\n### .gitignore files\n\nEvery Git repository should have another configuration file `.gitignore` present in the root i.e. directly inside the `git-playground` directory. This file is used to specify files and file patterns that you want to ignore.\n\n```bash\n$ echo \"*.log\" \u003e\u003e .gitignore\n$ echo \"target/\" \u003e\u003e .gitignore\n$ echo \"dummy.txt\" \u003e\u003e .gitignore\n```\n\n1. The first pattern says any file with extension `log` will ignored.\n2. The second pattern says directory with name `target` will be ignored.\n3. The third pattern means file with name `dummy.txt` will be ignored.\n\nNow add and commit the `.gitignore` to your Git repository.\n\n```bash\n$ git add .gitignore\n$ git commit -m \"third commit. Adding .gitignore file\"\n```\n\n### git diff\n\nTo view unstaged changes since last commit.\n\n```bash\n$ echo \"I will learn Git today.\" \u003e\u003e README.md\n```\n\n```bash\n$ git diff\n```\n```bash\ndiff --git a/README.md b/README.md\nindex 6b8025a..fcb7db5 100644\n--- a/README.md\n+++ b/README.md\n@@ -1,2 +1,3 @@\n # Git Playground\n This is my Git playground.\n+I will learn Git today.\n```\n\nLet's commit the change.\n\n```bash\n$ git commit -am \"fourth commit\"\n```\n\nNow, if you execute `git diff` command, you will see nothing.\n\n### git diff commits\n\nTo compare head with the previous commit you can use the following command.\n\n```bash\n$ git diff HEAD~1..HEAD\n```\n\nYou can also use commit ids.\n\n```bash\n$ git diff \u003ccommit_id_1\u003e \u003ccommit_id_2\u003e\n```\n\n### git clean\n\n`git clean` command helps us to remove untracked files and directories from the working tree.\n\n```bash\n$ touch abc.txt\n$ mkdir tmp\n$ touch tmp/def.txt\n```\n\nForcefully remove untracked files\n\n```bash\n$ git clean -f\n```\n\nForcefully remove directory\n\n```bash\ngit clean -d\n```\n\nForcefully remove untracked file and directory\n\n```bash\n$ git clean -df\n```\n\nTo also remove files in the `.gitignore` file, you can specify `-x` option.\n\n```bash\n$ echo \"logging\" \u003e\u003e abc.log\n```\n\n`git clean` will not do anything\n\n```bash\n$ git clean -f\n```\n\n```bash\n$ → ll\ntotal 16\n-rw-r--r--  1 shekhargulati  staff    83B Jan 14 05:59 README.md\n-rw-r--r--  1 shekhargulati  staff     8B Jan 14 06:30 abc.log\n```\n\nYou can clean up `.gitignore` files using the `-x` option.\n\n```bash\n$ git clean -f -x\nRemoving abc.log\n```\n\n## Working with branches\n\nA branch represents an independent line of development. You use branch for following:\n\n1. Develop a new feature. Once you are done with the feature it will be merged into your main branch.\n2. Fix a bug.\n3. Your experiment playground.\n\nBy default, every git repository has one branch called **master**. When you create a new branch, you get a new development workspace. Any change that you make to the new working directory has no impact on your previous working directory.\n\n### git branch\n\n**git branch** command lets you work with Git branches.\n\nTo view all the branches, you execute following command.\n\n```bash\n$ git branch\n* master\n```\n\n\u003e **branch marked with a * is the current branch.**\n\nLet's suppose we have to implement some new functionality. To work on new functionality, we create a new branch called `feature1`.\n\n```bash\n$ git branch feature1\n```\n\nYou can view branch list again.\n\n```\n$ git branch\n  feature1\n* master\n```\n\nOne thing that is important to understand here is Git branches are just pointers to commits. When you create a branch, all Git needs to do is create a new pointer—it doesn’t change the repository in any other way.\n\n```bash\n$ git log --oneline --decorate\n```\n```\n79a81e4 (HEAD -\u003e master, feature1) fourth commit\nc9a8f2d third commit. Added .gitignore file\nabae250 second commit\nbb2b569 first commit\n```\n\n### git checkout\n\nTo switch to a branch, you use checkout command.\n\n```bash\n$ git checkout feature1\n```\n\nThe `git checkout` command lets you navigate between the branches created by git branch.\n\nTo create a new branch and checkout it in one command\n\n```bash\n$ git checkout -b feature1\n```\n\nLet's add our new feature now.\n\n```bash\n$ echo \"I will read a Git tutorial today to make sure I understand Git thoroughly.\" \u003e\u003e README.md\n```\n\nCommit it to the `feature1` branch.\n\n```bash\n$ git commit -am \"fifth commit. Read tutorial\"\n```\n\n### git merge (ff)\n\nOnce we are done with our feature, we can merge it back to master.\n\n```bash\n$ git checkout master\n```\n\n**git merge** command allows you to merge an independent development line created by `git branch` into a single branch.\n\n\u003e git merge will merge into the current branch.\n\nTo merge `feature1` branch into `master` branch, execute the following command.\n\n```bash\n$ git merge feature1\n```\n\n```bash\n$ git log --oneline --decorate\n```\n```\n5ce434c (HEAD -\u003e master, feature1) fifth commit. Read tutorial\n79a81e4 fourth commit\nc9a8f2d third commit. Added .gitignore file\nabae250 second commit\nbb2b569 first commit\n```\n\n\u003e the default merge algorithm is ff i.e fast forward. When the merge resolves as a fast-forward, only update the branch pointer, without creating a merge commit. This is the default behavior.\n\nA **fast-forward** merge can occur when there is a linear path from the current branch tip to the target branch.\n\nNow that we are done with `feature1` branch, let's delete it.\n\n```bash\n$ git branch -d feature1\n```\n\n### git merge (--no-ff)\n\nCreate another new branch `feature2` and add a new commit.\n\n```bash\n$ git checkout -b feature2\n$ echo \"I will watch a Git video by Linus Torvalds https://www.youtube.com/watch?v=4XpnKHJAok8\" \u003e\u003e README.md\n$ git commit -am \"sixth commit. Watch video.\"\n```\n\nNow checkout master again.\n\n```bash\n$ git checkout master\n```\n\nThe `--no-ff` algorithm always generate a merge commit (even if it was a fast-forward merge)\n\n```bash\n$ git merge --no-ff feature2\n```\n\nView logs of master\n\n```bash\n$ git log --oneline --decorate\n```\n```\n0039442 (HEAD -\u003e master) Merge branch 'feature2'\n904ab35 (feature2) sixth commit. Watch video.\n5ce434c fifth commit. Read tutorial\n79a81e4 fourth commit\nc9a8f2d third commit. Added .gitignore file\nabae250 second commit\nbb2b569 first commit\n```\n\nThe interesting bit is that it created a merge commit. Git uses Three-way merge algorithm.\n\n1. the `MERGE_HEAD` commit i.e. the modification that we want to merge\n2. the `HEAD` commit i.e. the branch in which the `MERGE_HEAD` will be merged i.e. the branch on which the git merge command is called\n3. the `ORIG_HEAD` commit i.e. the best common ancestor of `MERGE_HEAD` and `HEAD` that will serve as the reference.\n\nDelete the feature branch `feature2`\n\n```bash\n$ git branch -d feature2\n```\n\n### git merge (commits in both master and branch)\n\nCreate another new branch `feature2` and add a new commit.\n\n```bash\n$ git checkout -b feature3\n$ echo \"I will try Git using tutorial https://try.github.io/\" \u003e\u003e README.md\n$ git commit -am \"seventh commit. Try Git.\"\n```\n\nCheckout master branch and edit an existing line.\n\n```bash\n$ git checkout master\n```\n\nChange **I will read a Git tutorial today to make sure I understand Git thoroughly.** to **I will read a Git tutorial https://www.atlassian.com/git/tutorials/.**\n\n```bash\n$ cat README.md\n```\n```\n# Git Playground\nThis is my Git playground.\nI will learn Git today during the XKE.\nI will read a Git tutorial https://www.atlassian.com/git/tutorials/.\nI will watch a Git video by Linus Torvalds https://www.youtube.com/watch?v=4XpnKHJAok8\n```\n\nNow, commit the change to master branch.\n\n```bash\n$ git commit -am \"fixed Git tutorial bullet point\"\n```\n\nNow merge the `feature3` branch. This will create a merge commit.\n\n```bash\n$ git merge feature3\n```\n\nView the log graph.\n\n```bash\n$ git log --oneline --graph\n```\n```\n*   b182cc7 Merge branch 'feature3'\n|\\\n| * 6f04457 seventh commit. Try Git.\n* | fd759a7 fixed Git tutorial bullet point\n|/\n*   0039442 Merge branch 'feature2'\n|\\\n| * 904ab35 sixth commit. Watch video.\n|/\n* 5ce434c fifth commit. Read tutorial\n* 79a81e4 fourth commit\n* c9a8f2d third commit. Added .gitignore file\n* abae250 second commit\n* bb2b569 first commit\n```\n\n### git merge (conflict)\n\nIn this section, we will look at scenario where there is a merge conflict. Merge conflict happens when you‘re trying to merge a branch that has changed the same part of the same file as master. Let's create a new branch `feature4`, make a change, and commit.\n\n```bash\n$ git checkout -b feature4\n$ echo \"I will create a Github account today.\" \u003e\u003e README.md\n$ git commit -am \"eighth commit. Signup for Github.\"\n```\n\nNow, checkout master, make a change, and then commit it.\n\n```bash\n$ git checkout master\n$ echo \"I will create my first repository today.\" \u003e\u003e README.md\n$ git commit -am \"eighth commit. Create repository on Github.\"\n```\n\nIf you try to merge the `feature4` branch, you will get merge conflict as shown below.\n\n```bash\n$ git merge feature4\n```\n```\nAuto-merging README.md\nCONFLICT (content): Merge conflict in README.md\nAutomatic merge failed; fix conflicts and then commit the result.\n```\n\nTo merge the conflict, open the file in your favorite editor. I like Atom.\n\n```bash\n$ cat README.md\n```\n```\n# Git Playground\nThis is my Git playground.\nI will learn Git today during the XKE.\nI will read a Git tutorial https://www.atlassian.com/git/tutorials/.\nI will watch a Git video by Linus Torvalds https://www.youtube.com/watch?v=4XpnKHJAok8\nI will try Git using tutorial https://try.github.io/\n\u003c\u003c\u003c\u003c\u003c\u003c\u003c HEAD\nI will create my first repository today.\n=======\nI will create a Github account today.\n\u003e\u003e\u003e\u003e\u003e\u003e\u003e feature4\n```\n\nThe merged output is shown below.\n\n```\n# Git Playground\nThis is my Git playground.\nI will learn Git today during the XKE.\nI will read a Git tutorial https://www.atlassian.com/git/tutorials/.\nI will watch a Git video by Linus Torvalds https://www.youtube.com/watch?v=4XpnKHJAok8\nI will try Git using tutorial https://try.github.io/\nI will create a Github account today.\nI will create my first repository today.\n```\n\nTo make the merge resolved, execute `git add` command.\n\n```bash\n$ git add README.md\n```\n\nCommit it\n\n```bash\n$ git commit -am \"Resolved merged conflict with feature4 branch\"\n```\n\nOutput of `git log` command is shown below as well.\n\n```bash\n$ git log --oneline --decorate --graph\n```\n```\n*   4da527c (HEAD -\u003e master) Resolved merged conflict with feature4 branch\n|\\\n| * 84ee6f1 (feature4) eighth commit. Signup for Github.\n* | 0af5877 eighth commit. Create repository on Github.\n|/\n*   b182cc7 Merge branch 'feature3'\n|\\\n| * 6f04457 seventh commit. Try Git.\n* | fd759a7 fixed Git tutorial bullet point\n|/\n*   0039442 Merge branch 'feature2'\n|\\\n| * 904ab35 sixth commit. Watch video.\n|/\n* 5ce434c fifth commit. Read tutorial\n* 79a81e4 fourth commit\n* c9a8f2d third commit. Added .gitignore file\n* abae250 second commit\n* bb2b569 first commit\n```\n\n## Working with remote repositories\n\nIn Git, every developer has their own local copy of the repository. It has all the repository history and branches. A developer works on his local copy and when she is done with her work, she pushes her changes to a remote repository. A local repository can point to 0 or n remote repository. A remote repository could be on Github or any other cloud VCS provider or even it could be on your own machine.\n\nLet's start by creating a bare repository. Navigate to any convenient location on your filesystem and run the following command.\n\n```bash\n$ git init --bare git-playground-remote.git\n```\n\nThis will create a directory `git-playground-remote.git` on your filesystem.\n\n### git remote\n\nThe **git remote** command lets you create, view, and delete connections to other repositories.\n\nTo view all the remotes, execute the following command.\n\n```bash\n$ git remote\n```\n\nAs we have not added any remote yet so command will not print any output.\n\nTo add a new remote, execute the following command.\n\n```bash\n$ git remote add local ../git-playground-remote.git\n```\n\n\u003e\t**Please use correct path to your remote repository.**\n\nTo push our repository changes to this remote repository we can run the following command. This command will be executed from inside the `git-playground` directory.\n\n```bash\n$ git push local master\n```\n```\nCounting objects: 37, done.\nDelta compression using up to 8 threads.\nCompressing objects: 100% (32/32), done.\nWriting objects: 100% (37/37), 3.41 KiB | 0 bytes/s, done.\nTotal 37 (delta 11), reused 0 (delta 0)\nTo ../git-playground-remote.git\n * [new branch]      master -\u003e master\n```\n\nNow, you can create multiple local copies of the remote repository using `clone` command.\n\n```bash\n$ git clone git-playground-remote.git git-playground-1\n```\n```\nCloning into 'git-playground-1'...\ndone.\n```\n\nChange directory and view the contents.\n\n```bash\n$ cd git-playground-1\n```\n\nYou can see history of the repository using `git log --oneline --decorate` command.\n\nLet's make a change in `git-playground-1` repository.\n\n```bash\n$ echo \"I will read Git in Practice book\" \u003e\u003e README.md\n$ git commit -am \"ninth commit. Read book.\"\n$ git push local master\n```\n\nTo get changes in our first repository i.e. `git-playground`, you will use `pull` command.\n\n```bash\n$ git pull local master\n```\n```\nremote: Counting objects: 3, done.\nremote: Compressing objects: 100% (3/3), done.\nremote: Total 3 (delta 1), reused 0 (delta 0)\nUnpacking objects: 100% (3/3), done.\nFrom ../git-playground-remote\n * branch            master     -\u003e FETCH_HEAD\n   4da527c..b38de01  master     -\u003e local/master\nUpdating 4da527c..b38de01\nFast-forward\n README.md | 1 +\n 1 file changed, 1 insertion(+)\n```\n\n## Github basics\n\nGithub is a collaborative code management platform for open source and private projects. It has social features just like any other social website like Facebook, Twitter, etc.\n\nYou use Github to store your code on a remote Git repository.  You will use Github for following reasons:\n\n1. Storing your data in a cloud solution which will make sure code is backed up and available 24X7.\n\n2. Collaborative code reviews\n\n3. Easily manage teams within organizations.\n\n4. Syntax highlighted code \u0026 rendered data\n\nRead more on Github [https://github.com/features](https://github.com/features).\n\nYou can sign up for Github at [https://github.com/join](https://github.com/join).\n\nThere are alternatives to Github like Bitbucket but Github is the leader.\n\n### Setup SSH keys\n\nIf you don't already have setup SSH keys then you should refer to https://help.github.com/articles/generating-ssh-keys/\n\n\n### Create a repository\n\nCreate a new repository by clicking [https://github.com/new](https://github.com/new).\n\nGive it name `git-playground` and choose defaults.\n\n### add remote\n\nFrom inside your `git-playground`, execute the following command.\n\n```bash\n$ git remote add origin git@github.com:shekhargulati/git-playground.git\n```\n\n### git push\n\nPush the changes to Github.\n\n### Create pull request\n\nFork the repository [https://github.com/shekhargulati/git-the-missing-tutorial](https://github.com/shekhargulati/git-the-missing-tutorial). Forking repository give you your own copy of the repository.\n\n1. Clone your clone on your local machine.\n2. Write a message in the `testimonials.md`\n3. Commit and push the changes to your fork\n4. Create a pull request\n\n## Advanced topics\n\nThis section will talk about some of the advanced concepts that might be useful.\n\n### git rebase\n\nRebasing is the process of moving a branch to a new base commit. It is used to keep Git history linear so you will not have merge commits.\n\n```bash\n$ git checkout -b feature5\n$ echo \"I will prefer rebase over merge to avoid merge commit.\" \u003e\u003e README.md\n$ git commit -am \"tenth commit. Prefer rebase over merge\"\n```\n\nWork on master branch.\n\n```bash\n$ git checkout master\n$ echo \"I will prefer merge over rebase as it is easy to understand.\" \u003e\u003e README.md\n$ git commit -am \"tenth commit. Merge over Rebase\"\n```\n\nRebase command\n\n```bash\n$ git rebase feature5 master\n```\n\nFix merge conflicts.\n\n```bash\n$ git add README.md\n$ git rebase --continue\n```\n\nNow view the logs using\n\n```bash\n$ git log --oneline --decorate --graph\n```\n\nDelete the branch\n\n```bash\n$ git branch -d feature5\n```\n\n### git cherry-pick\n\nApplying a commit from one branch to another\n\n### git stash\n\nTemporarily save your current state without creating a commit.\n\n1. Make a change to README.md\n2. Run `git stash` command\n3. List all stashes `git stash list`\n4. Apply the stash using `git stash apply` or `git stash pop`\n\n### git alias\n\nAllows you write your own commands or shortcuts. Like we have been using `git log --oneline --decorate --graph`.\n\n```bash\n$ git config --global alias.mylog \"log --oneline --decorate --graph\"\n```\n\n\n## Using Git from within your IDE(Eclipse)\n\nTODO\n\n## Git Gui\n\nI mostly use command-line terminal. For Mac, I find gitx useful [http://gitx.frim.nl/](http://gitx.frim.nl/)\n\n## Useful Git Commands\n\n1. Most useful 20 git commands `git help everyday`\n\n2. To view help of any git command `git help \u003ccommand\u003e` for example `git help commit`\n\n3. To refer to all the Git terms `git help glossary`\n\n4. Overwrite your changes with the changes in the remote `git fetch --all \u0026\u0026 git reset --hard origin/master`\n\n5. Viewing unstaged changes since last commit `git diff`\n\n6. List all branches that are already merged into master `git branch --merged`\n\n7. List all branches and their upstreams, as well as last commit on branch `git branch -vv`\n\n8. Deleting a branch `git branch -d \u003cbranch_name\u003e`\n\n9. Delete a remote branch `git push origin --delete \u003cremote_branch_name\u003e`\n\n10. Undo local changes with last content in head `git checkout -- .` or `git checkout -- \u003cfilename\u003e`\n\n11. Revert last commit `git revert HEAD` or `git revert HEAD~n` or `git revert \u003ccommit_id\u003e`. This creates a new commit with undo\n\n12. Reset: Discard last commit. Last commit content is lost.  `git reset --hard HEAD` or `git reset --hard HEAD~n`\n\n13. Reset: Discard last commit. Last commit is unstaged. `git reset --soft HEAD` or `git reset --soft HEAD~n`\n\n14. List all remotes `git remote`\n\n15. List all local and remote branches `git branch -a`\n\n16. See all commits made since forking from master `git log --no-merges --stat --reverse master..`\n\n17. Saving current state of tracked files without committing `git stash`\n\n18. Show list of all saved stashes `git stash list`\n\n19. Apply last stashed state and delete it from stashed list `git stash pop`\n\n20. Forcefully remove untracked files `git clean -f`\n\n21. Forcefully remove untracked directory `git clean -d`\n\n22. Forcefully remove untracked file and directory `git clean -df`\n\n23. Rename a branch `git branch -m \u003cnew_branch_name\u003e`\n\n24. Always rebase instead of merge on pull `git config --global branch.autosetuprebase always`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanshul-sonpure%2Fbasic-git-commands","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fanshul-sonpure%2Fbasic-git-commands","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fanshul-sonpure%2Fbasic-git-commands/lists"}