{"id":19729830,"url":"https://github.com/vasanthk/git-tips-and-tricks","last_synced_at":"2025-02-27T20:19:01.303Z","repository":{"id":30923674,"uuid":"34481603","full_name":"vasanthk/git-tips-and-tricks","owner":"vasanthk","description":"Git Cheatsheet with Tips and Tricks","archived":false,"fork":false,"pushed_at":"2022-04-27T22:58:23.000Z","size":20,"stargazers_count":65,"open_issues_count":0,"forks_count":18,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-10T17:42:47.745Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/vasanthk.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":"2015-04-23T21:06:59.000Z","updated_at":"2024-12-03T17:38:33.000Z","dependencies_parsed_at":"2022-07-21T21:47:28.304Z","dependency_job_id":null,"html_url":"https://github.com/vasanthk/git-tips-and-tricks","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/vasanthk%2Fgit-tips-and-tricks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vasanthk%2Fgit-tips-and-tricks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vasanthk%2Fgit-tips-and-tricks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vasanthk%2Fgit-tips-and-tricks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vasanthk","download_url":"https://codeload.github.com/vasanthk/git-tips-and-tricks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241055737,"owners_count":19901645,"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":[],"created_at":"2024-11-12T00:13:50.992Z","updated_at":"2025-02-27T20:19:01.281Z","avatar_url":"https://github.com/vasanthk.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Git: Tips And Tricks\nGit Cheatsheet with a few nifty tips and tricks\n\n* [Git log (Pretty graph view)](#git-log-pretty-graph-view)\n* [Stash](#stash)\n* [Cherry pick and apply to current branch](#cherry-pick-and-apply-to-current-branch)\n* [Forget added files in git](#forget-added-files-in-git)\n* [Git Aliases](#git-aliases)\n* [Deleting a branch both locally and remotely](#deleting-a-branch-both-locally-and-remotely)\n* [Keeping a forked repo in sync with the main repo](#keeping-a-forked-repo-in-sync-with-the-main-repo-command-linegithub)\n* [Different branch name for local and remote](#different-branch-name-for-local-and-remote)\n* [Squash PR commits into one](#squash-pr-commits-into-one)\n* [Bumping version and publishing upstream's branch (when you already have a fork)](#bumping-version-and-publishing-upstreams-branch-when-you-already-have-a-fork)\n* [Test a pull request in your local before merging](#test-a-pull-request-in-your-local-before-merging)\n* [Deleting last commit from git](#deleting-last-commit-from-git)\n* [Fetch all Git branches from remote](#fetch-all-git-branches-from-remote)\n* [Fetch a branch from someone else's fork](#fetch-a-branch-from-someone-elses-fork)\n* [Deleting branch from fork and then re-creating it with same from upstream](#deleting-branch-from-fork-and-then-re-creating-it-with-same-from-upstream)\n\n### Git log (Pretty graph view)\n```git log --graph ```\nThis displays your commits in a nice tree form.\n\nBy making the following alias you get:\n* colors\n* graph of commits\n* one commit per line\n* abbreviated commit IDs\n* dates relative to now\n* commit references\n* author of the commit\n* And the alias is:\n\n```git config --global alias.lg \"log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)\u003c%an\u003e%Creset' --abbrev-commit --date=relative\"```\n\nAnd every time you need to see your log, just type in\n\n```git lg```\n\nor, if you want to see the lines that changed\n\n```git lg -p```\n\n### Stash\n```git stash```\t\t\t\t\t\t\t\t                  : Stash current changes\n\n```git stash save \"appropriate caption here\"```\t: Name stashed changes\n\n```git stash apply```\t\t\t\t\t\t\t              : Apply the last stashed item\n\n```git stash pop```\t\t\t\t\t\t\t                : Apply and drop most recent stash\n\n```git stash list```\t\t\t\t\t\t\t              : List all stashes\n\n```git stash clear```\t\t\t\t\t\t\t              : Clear all entries in stash\n\n### Cherry pick and apply to current branch\n\n```git cherry-pick ###SHA-1##```\n\nIn cases picking one single commit is not enough and you need, let's say three consecutive commits - rebase is the right tool, not cherry-pick.\n\n### Forget added files in git\n\n```git rm -r --cached .```\n\n```git add .```\n\n### Git Aliases\n\n```git config --global alias.\u003chandle\u003e \u003ccommand\u003e```\n\n```git config --global alias.st status```\n\n```git config --global alias.l \"log --oneline --graph\"```\n\n\n### Deleting a branch both locally and remotely\n\n##### Locally:\n\n```git branch -D local_branch_name```\n\n```-D``` force deletes, ```-d``` will give you a warning if it’s not already merged in.\n\n##### Remotely:\n\nWe also delete the remote branch by simply adding the \"-r\" flag to the \"-d\" option\n\n```git branch -dr origin/remote_branch_name```\n\n(or)\n\n```git push origin --delete remote_branch_name```\n\n(or)\n\n```git push origin :remote_branch_name```\n\n### Keeping a forked repo in sync with the main repo ([Command line](http://stackoverflow.com/questions/7244321/how-do-i-update-a-github-forked-repository/7244456#7244456)/[Github](http://stackoverflow.com/questions/20984802/how-can-i-keep-my-fork-in-sync-without-adding-a-separate-remote/21131381#21131381))\n\nAdd the remote, call it \"upstream\":\n\n```git remote add upstream \u003cpath-to-the-main-repo\u003e```\n\nFetch all the branches of that remote into remote-tracking branches, such as upstream/master:\n\n```git fetch upstream```\n\nMake sure that you're on your master branch:\n\n```git checkout master```\n\nRewrite your master branch so that any commits of yours that aren't already in upstream/master are replayed on top of that other branch:\n\n```git rebase upstream/master```\n\nIf you've rebased your branch onto upstream/master you may need to force the push in order to push it to your own forked repository.\n\n```git push -f origin master```\n\n### Different branch name for local and remote\n\nBranch out locally:\n\n```git checkout -b local_branch_name```\n\nCheckout branch with an easy to remember local_branch_name\n\nPush to remote \u0026 set upstream:\n\n```git push -u origin local_branch_name:remote_branch_name```\n\nPush the local branch to remote repo with a different descriptive name remote_branch_name\n\n```git branch --set-upstream-to=origin/remote_branch_name```\n\nSet your push.default to upstream to push branches to their upstreams (which is the same that pull will pull from), rather than pushing branches to ones matching in name (which is the default setting for push.default, matching).\n\n```git config push.default upstream```\n\n### Squash PR commits into one\n\nFetch from upstream (when merging clone to main upstream branch) or origin (when merging branch to master)\n\n```git fetch upstream```\n\n```git checkout mybranch```\n\n```git merge upstream/master```\n\nIf necessary, resolve conflicts and git commit...\n\n```git reset --soft upstream/master```\n\n```git commit -am 'Some cool description for a single commit'```\n\n```git push --force-with-lease```\n\nMore on `--force-with-lease` option [here](https://developer.atlassian.com/blog/2015/04/force-with-lease/). Note that it's super important that you merge before resetting, and that the argument is the same master branch. Otherwise you risk messing up your local history.\n\nThe --soft parameter tells Git to reset HEAD to another commit, but that’s it. If you specify --soft Git will stop there and nothing else will change. What this means is that the index and working copy don’t get touched, so all of the files that changed between the original HEAD and the commit you reset to appear to be staged.\n\n*Extra:* To squash last few commits (eg. last 2 commits) use ```git reset --soft HEAD~2```\n\n### Bumping version and publishing upstream's branch (when you already have a fork)\n\nFetch all the upstream branches\n\n```git fetch upstream \u0026\u0026 git reset --hard upstream/master```\n\nLists all the remote branches\n\n```git branch -v -a```\n\nFrom that list choose whichever branch you need to checkout\n\n```git checkout remotes/upstream/master```\n\nIn the message being shown - verify to make sure the HEAD is at the most recent commit in that branch. Example shown below\n\n```\nHEAD is now at 8c9d3eb... 6.3.1\n➜  user git:(8c9d3eb)\n```\n\nNow bump the package.json version\n\n```npm version patch```\n\nYou'll need to push it to remote upstream along with the tags.\n\n```git push upstream HEAD:master \u0026\u0026 git push upstream --tags```\n\nFinally, publish the package\n\n```npm publish```\n\n**IMPORTANT:** HEAD is now in a detatched state. So without fail switch back to your working branch!\n\n```git checkout develop```\n\n### Test a pull request in your local before merging\n\n```git fetch origin pull/{pull-request-id}/head:{local-branch-name-to-test}```\n\n```git checkout {local-branch-name-to-test}```\n\n``{pull-request-id}`` can be obtained from the pull request url ``repo-url/pull/pull-request-id`` format\n\n### Deleting last commit from git\n\nIf you already pushed, use ``git revert``\n\nIf no one else is using your branch:\n\n``git reset —hard HEAD~1 (or) git reset —hard \u003csha1-commit-id\u003e``\n\n``git push origin HEAD —force``\n\n### Fetch all Git branches from remote\n\nYou will need to create local branches tracking remote branches.\n\nAssuming that you've got only one remote called origin, this snippet will create local branches for all remote tracking ones:\n\n``for b in `git branch -r | grep -v -- '-\u003e'`; do git branch --track ${b##origin/} $b; done``\n\nAfter that, ``git fetch --all`` will update all local copies of remote branches.\n\nAlso, ``git pull --all`` will update your local tracking branches, but depending on your local commits and how the 'merge' configure option is set it might create a merge commit, fast-forward or fail.\n\n### Fetch a branch from someone else's fork\n\n``git remote add theirusername https://github.com/theirusernam/reponame.git``\n\n``git fetch theirusername``\n\n``git checkout -b mynamefortheirbranch theirusername/theirbranch``\n\n### Deleting branch from fork and then re-creating it with same from upstream\n\nDelete Local Branch\n\n``git branch -D \u003cbranch_name\u003e``\n\nDelete Remote Branch\n\n``git push origin --delete \u003cbranch_name\u003e``\n\nCreate branch from upstream\n\n``git checkout -b \u003cbranch_name\u003e upstream/\u003cbranch_name\u003e``\n\nPush branch to remote\n\n``git push -u origin \u003cbranch_name\u003e``\n\n\n### Keep \u0026 Manage different project sources in the current repository using `git subtree`\n\nYou are in the your project directory which need use external sources!\n\nAdd / map external sources to folder `\u003calias_name\u003e`:\n```\ngit subtree add --prefix=\u003calias_name\u003e \u003croot_project_https_url\u003e \u003cproject_branch\u003e\n```\nor with minimal history log:\n```\ngit subtree add --prefix=\u003calias_name\u003e --squash \u003croot_project_https_url\u003e \u003cproject_branch\u003e\n```\n\nto revert some commits back (for example we need `2358acd7` commit which is behind current one in the subtree project)\n\n```\ngit subtree merge -P \u003calias_name\u003e --squash 2358acd7\n```\n\nto update old code in subtree from original repository\n\n```\ngit subtree pull -P \u003calias_name\u003e --squash \u003croot_project_https_url\u003e \u003cproject_branch\u003e\n```\n\n\n### Keep \u0026 Manage different project sources in the current repository externally using `git submodule`\n\nYou are in the your project directory which need use external sources!\n\nAdd / map external sources:\n```\ngit submodule add \u003cproject_repo\u003e\n```\n\nPull updated code from submodule repository\n```\ngit submodule update --remote\n```\n\nReset submodule code to initial state\n```\ngit pull --update --resource-submodules\n```\n\nTo clone project with submodule sources\n```\ngit clone --recurse \u003cmain_project_repo\u003e\n```\n\nIn case you've cloned project without `--recurse` flag you will not have submodule data in your repository.\nTo load required data you need try next instruction:\n```\ngit submodule update --init --recursive\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvasanthk%2Fgit-tips-and-tricks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvasanthk%2Fgit-tips-and-tricks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvasanthk%2Fgit-tips-and-tricks/lists"}