-
-
Save mamedinfo/ddf6ed1036d13df1ae42fc47035054e8 to your computer and use it in GitHub Desktop.
Revisions
-
jedmao revised this gist
Jun 26, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -44,7 +44,7 @@ git fetch upstream When working on a fork, you could be switching between different branches quite commonly. As such, you generally want to stay off the master branch and work on your own feature branches so that master is always clean and you can base new branches off of it. ```shell git switch -c <new-branch-name> ``` If upstream has a special develop branch or something, you can checkout that branch separately, but setup tracking so you can sync it up from time to time. Like the master branch, don't work directly on this one. Try to keep it clean. @@ -130,7 +130,7 @@ git commit -m "Updated README" Want to automatically stage files that have been modified and deleted, but new files you haven't told git about will be unaffected? Pass the `-a` or `--all` option flag: ```shell git commit -am "Updated README" ``` -
jedmao revised this gist
Jun 26, 2020 . 1 changed file with 61 additions and 52 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ Create an empty git repo or reinitialize an existing one ```shell git init ``` ### [Fork a repo](https://help.github.com/articles/fork-a-repo#step-1-fork-the-spoon-knife-repository) @@ -19,21 +19,21 @@ Click the "Fork" button at the top-right of any repository's GitHub page. Clone the foo repo into a new directory called foo: ```shell git clone https://github.com/<username>/foo.git foo ``` ### [Setup Remotes](https://git-scm.com/docs/git-remote) First, let's see a list of the repositories (remotes) whose branches you track: ```shell git remote -v ``` Oh, it looks like we haven't setup upstream. Now is the time: ```shell git remote add upstream https://github.com/<upstream_username>/<repo_name>.git git fetch upstream ``` @@ -49,17 +49,17 @@ $ git switch -c <new-branch-name> If upstream has a special develop branch or something, you can checkout that branch separately, but setup tracking so you can sync it up from time to time. Like the master branch, don't work directly on this one. Try to keep it clean. ```shell git switch -c <new-branch-name> --track upstream/<remote-branch-to-track> ``` Maybe you made some progress on a branch at work, but now you want to continue work at home. In that case, you're dealing with your own fork's branch, so you'll checkout from origin. ```shell git switch -c <new-branch-name> --track origin/<remote-branch-to-track> ``` If you're using the same name as a remote branch name, this can be simplified: ```shell git switch -c <branch-name> ``` Use the `-C` option flag to force it. @@ -69,27 +69,36 @@ Use the `-C` option flag to force it. First, you'll want to know what branches are available in your working directory: ```shell git branch develop feature_x master ``` Now, you can easily switch between branches with `git switch`: ```shell git switch master git switch develop git switch feature_x git config --global alias.sw 'switch' git sw master ``` #### Switch to previous branch ```shell git switch @{-1} ``` _credit: [@MattNewbill](#gistcomment-3354581)_ ### [Status](https://git-scm.com/docs/git-status) Not sure if you're working on a clean branch? Want to see what files have changed? Git status will show you a report. ```shell git status ``` ### [Staging Changes](https://git-scm.com/docs/git-add) @@ -98,25 +107,25 @@ Now that you've added or modified some files, you need to stage those commits in You can stage inidividual files or all files at once. ```shell git add foo.js git add . ``` ### [Unstaging Changes / Restoring Files](https://git-scm.com/docs/git-restore) Maybe you accidentally staged some files that you don't want to commit. ```shell git restore foo.js git restore . ``` ### [Commits](https://git-scm.com/docs/git-commit) Commit often. You can always squash down your commits before a push. ```shell git commit -m "Updated README" ``` Want to automatically stage files that have been modified and deleted, but new files you haven't told git about will be unaffected? Pass the `-a` or `--all` option flag: @@ -129,20 +138,20 @@ $ git commit -am "Updated README" The following command will undo your most recent commit and put those changes back into staging, so you don't lose any work: ```shell git reset --soft HEAD~1 ``` The next one will completely delete the commit and throw away any changes. Be absolutely sure this is what you want: ```shell git reset --hard HEAD~1 ``` ### [Squashing Commits](https://git-scm.com/docs/git-rebase) Maybe you have 4 commits, but you haven't pushed anything yet and you want to put everything into one commit so your boss doesn't have to read a bunch of garbage during code review. ```shell git rebase -i HEAD~4 ``` An interactive text file is displayed. You'll see the word "pick" to the left of each commit. Leave the one at the top alone and replace all the others with "s" for squash, save and close the file. This will display another interactive window where you can update your commit messages into one new commit message. I like to use "f" instead of "s", because I usually work in such a way that I name my first commit appropriately from the get-go. "f" just skips the 2nd interactive file and uses the first commit message. @@ -152,18 +161,18 @@ An interactive text file is displayed. You'll see the word "pick" to the left of Push a local branch for the first time: ```shell git push --set-upstream origin <branch> git push ``` Configure Git to [always push using the current branch name](https://git-scm.com/docs/git-config#Documentation/git-config.txt-pushdefault): ```shell git config --global push.default current ``` Push a local branch to a different remote branch: ```shell git push origin <local_branch>:<remote_branch> ``` Use the `-f` option flag to force it. @@ -173,20 +182,20 @@ Use the `-f` option flag to force it. Some would say this is bad practice. Once you push something you shouldn't overwrite those changes. Instead, you're supposed to create a new commit that reverts the changes in the last one. So, technically, you shouldn't do this, but... you can? ```shell git reset --hard HEAD~1 && git push -f origin master ``` ### [Fetching](https://git-scm.com/docs/git-fetch) Fetch changes from upstream: ```shell git fetch upstream ``` Fetch changes from both origin and upstream in the same shot: ```shell git fetch --multiple origin upstream ``` @@ -196,40 +205,40 @@ To be honest, I haven't used this command in quite some time. In my experience, Nonetheless, here's how you merge-in changes from origin's `feature_x` branch: ```shell git fetch origin git merge origin/feature_x ``` ### [Pulling](https://www.kernel.org/pub/software/scm/git/docs/git-pull.html) Pulling is just doing a fetch followed by a merge. If you know your branch is clean (e.g., master branch), go ahead and get the latest changes. There will be no merge conflicts as long as your branch is clean. ```shell git pull origin/feature_x ``` ### [Rebasing](https://git-scm.com/docs/git-rebase) Rebasing is a way of rewriting history. In place of merge, what this does is stacks your commits on top of commits that are already pushed up. In this case, you want to stack your commits on top of `origin/feature_x`: ```shell git fetch origin git rebase origin/feature_x ``` If you already have a local branch set to track `feature_x` then just do: ```shell git rebase feature_x ``` Would you like to fetch, merge and then stack your changes on top, all in one shot? You can! If you have tracking setup on the current branch, just do: ```shell git pull --rebase ``` Another great use of rebasing is just rewriting commit messages. To get an interactive text editor for the most recent commit, do: ```shell git rebase -i HEAD~1 ``` Now, you can replace "pick" with "r" and just change the commit message. @@ -239,85 +248,85 @@ Now, you can replace "pick" with "r" and just change the commit message. Perhaps you forgot to setup tracking when you pulled down a remote branch. No worries: ```shell git config branch.<local_branch>.remote origin git config branch.<local_branch>.merge refs/heads/<remote_branch> ``` ### [Deleting Branches](https://git-scm.com/docs/git-branch#Documentation/git-branch.txt--d) Delete a local branch: ```shell git branch -d <local_branch> ``` Use the `-D` option flag to force it. Delete a remote branch on origin: ```shell git push origin :<remote_branch> ``` ### [Stashing](https://git-scm.com/docs/git-stash) Sometimes you need to stash your changes so you can be on a clean branch or maybe because you want to go back and try something before you make a commit with these changes. Here's how you do a stash: ```shell git stash ``` Now, if you want to unstash those changes and bring them back into your working directory: ```shell git stash pop ``` Or maybe you want to unstash your changes without popping them off the stack. In other words, you might want to apply these stashed changes multiple times. To do this: ```shell git stash apply ``` For a list of stashes: ```shell git stash list ``` And to apply a specific stash from that list (e.g., stash@{3}): ```shell git stash apply stash@{3} ``` ## Tagging ### [Fetch](https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt--t) ```shell git fetch --tags ``` ### [Create](https://git-scm.com/docs/git-tag) ```shell git tag -a v1.0.0 9fceb02 -m "Initial release" ``` Where `9fceb02` represents the commit hash. ### [Push](https://git-scm.com/docs/git-push#Documentation/git-push.txt---tags) ```shell git push --tags ``` ### [Delete](https://git-scm.com/docs/git-tag#Documentation/git-tag.txt--d) ```shell git tag -d v1.0.0 ``` #### [Remote](https://git-scm.com/docs/git-push) ```shell git push origin :refs/tags/v1.0.0 ``` ## Git Bash -
jedmao revised this gist
May 5, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -322,6 +322,6 @@ $ git push origin :refs/tags/v1.0.0 ## Git Bash Once you're comfortable with what the above commands are doing, you might want to create some shortcuts for [Bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell)) or [Zsh](https://en.wikipedia.org/wiki/Z_shell) or whatever shell you're using. This allows you to work a lot faster by doing complex tasks in really short commands. Refer to my [.zshrc Gist](https://gist.github.com/jedmao/1deb357e54b3daf5856c#file-bashrc) for a good list of useful shortcuts. -
jedmao revised this gist
Apr 13, 2020 . 1 changed file with 21 additions and 28 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,7 +3,7 @@ ## Initial Setup ### [Initialize a repo](https://git-scm.com/docs/git-init) Create an empty git repo or reinitialize an existing one ```shell @@ -15,15 +15,15 @@ $ git init Click the "Fork" button at the top-right of any repository's GitHub page. ### [Clone a repo](https://git-scm.com/docs/git-clone) Clone the foo repo into a new directory called foo: ```shell $ git clone https://github.com/<username>/foo.git foo ``` ### [Setup Remotes](https://git-scm.com/docs/git-remote) First, let's see a list of the repositories (remotes) whose branches you track: ```shell @@ -85,14 +85,14 @@ $ git sw master ``` ### [Status](https://git-scm.com/docs/git-status) Not sure if you're working on a clean branch? Want to see what files have changed? Git status will show you a report. ```shell $ git status ``` ### [Staging Changes](https://git-scm.com/docs/git-add) Now that you've added or modified some files, you need to stage those commits into "the staging area." Think of git commits like an array of airlock hatches on a space ship. On this space ship, you can only open the door to one airlock at a time. When you open the hatch, you can put stuff in or take stuff out at will. Not until you've closed the door have you committed those changes (git commit) and not until you hit the red button do all those hatches open up into space (git push). @@ -112,7 +112,7 @@ $ git restore . ``` ### [Commits](https://git-scm.com/docs/git-commit) Commit often. You can always squash down your commits before a push. ```shell @@ -125,7 +125,7 @@ $ git commit -am "Updated README" ``` ### [Undoing Commits](https://git-scm.com/docs/git-reset) The following command will undo your most recent commit and put those changes back into staging, so you don't lose any work: ```shell @@ -138,7 +138,7 @@ $ git reset --hard HEAD~1 ``` ### [Squashing Commits](https://git-scm.com/docs/git-rebase) Maybe you have 4 commits, but you haven't pushed anything yet and you want to put everything into one commit so your boss doesn't have to read a bunch of garbage during code review. ```shell @@ -148,7 +148,7 @@ $ git rebase -i HEAD~4 An interactive text file is displayed. You'll see the word "pick" to the left of each commit. Leave the one at the top alone and replace all the others with "s" for squash, save and close the file. This will display another interactive window where you can update your commit messages into one new commit message. I like to use "f" instead of "s", because I usually work in such a way that I name my first commit appropriately from the get-go. "f" just skips the 2nd interactive file and uses the first commit message. ### [Pushing](https://git-scm.com/docs/git-push) Push a local branch for the first time: ```shell @@ -169,15 +169,15 @@ $ git push origin <local_branch>:<remote_branch> Use the `-f` option flag to force it. ### [Undo Last Push](https://git-scm.com/docs/git-reset) Some would say this is bad practice. Once you push something you shouldn't overwrite those changes. Instead, you're supposed to create a new commit that reverts the changes in the last one. So, technically, you shouldn't do this, but... you can? ```shell $ git reset --hard HEAD~1 && git push -f origin master ``` ### [Fetching](https://git-scm.com/docs/git-fetch) Fetch changes from upstream: ```shell @@ -190,7 +190,7 @@ $ git fetch --multiple origin upstream ``` ### [Merging](https://git-scm.com/docs/git-merge) To be honest, I haven't used this command in quite some time. In my experience, it has created merge bubbles that have overwritten mine or others' code. For a better workflow, refer to rebasing, below. @@ -209,7 +209,7 @@ $ git pull origin/feature_x ``` ### [Rebasing](https://git-scm.com/docs/git-rebase) Rebasing is a way of rewriting history. In place of merge, what this does is stacks your commits on top of commits that are already pushed up. In this case, you want to stack your commits on top of `origin/feature_x`: ```shell @@ -235,7 +235,7 @@ $ git rebase -i HEAD~1 Now, you can replace "pick" with "r" and just change the commit message. ### [Manually Set Tracking](https://git-scm.com/docs/git-config#Documentation/git-config.txt-branchltnamegtremote) Perhaps you forgot to setup tracking when you pulled down a remote branch. No worries: ```shell @@ -244,7 +244,7 @@ $ git config branch.<local_branch>.merge refs/heads/<remote_branch> ``` ### [Deleting Branches](https://git-scm.com/docs/git-branch#Documentation/git-branch.txt--d) Delete a local branch: ```shell @@ -259,7 +259,7 @@ $ git push origin :<remote_branch> ``` ### [Stashing](https://git-scm.com/docs/git-stash) Sometimes you need to stash your changes so you can be on a clean branch or maybe because you want to go back and try something before you make a commit with these changes. Here's how you do a stash: ```shell @@ -286,41 +286,35 @@ And to apply a specific stash from that list (e.g., stash@{3}): $ git stash apply stash@{3} ``` ## Tagging ### [Fetch](https://git-scm.com/docs/git-fetch#Documentation/git-fetch.txt--t) ```shell $ git fetch --tags ``` ### [Create](https://git-scm.com/docs/git-tag) ```shell $ git tag -a v1.0.0 9fceb02 -m "Initial release" ``` Where `9fceb02` represents the commit hash. ### [Push](https://git-scm.com/docs/git-push#Documentation/git-push.txt---tags) ```shell $ git push --tags ``` ### [Delete](https://git-scm.com/docs/git-tag#Documentation/git-tag.txt--d) ```shell $ git tag -d v1.0.0 ``` #### [Remote](https://git-scm.com/docs/git-push) ```shell $ git push origin :refs/tags/v1.0.0 @@ -331,4 +325,3 @@ $ git push origin :refs/tags/v1.0.0 Once you're comfortable with what the above commands are doing, you might want to create some shortcuts for Git Bash. This allows you to work a lot faster by doing complex tasks in really short commands. Refer to my [.bashrc Gist](https://gist.github.com/jedmao/1deb357e54b3daf5856c#file-bashrc) for a good list of useful shortcuts. -
jedmao revised this gist
Apr 13, 2020 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -156,6 +156,11 @@ $ git push --set-upstream origin <branch> $ git push ``` Configure Git to [always push using the current branch name](https://git-scm.com/docs/git-config#Documentation/git-config.txt-pushdefault): ```shell $ git config --global push.default current ``` Push a local branch to a different remote branch: ```shell $ git push origin <local_branch>:<remote_branch> -
jedmao revised this gist
Sep 24, 2019 . 1 changed file with 33 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -287,6 +287,39 @@ $ git stash apply stash@{3} $ git config --global core.autocrlf true ``` ## Tagging ### Fetch ```shell $ git fetch --tags ``` ### Create ```shell $ git tag -a v1.0.0 9fceb02 -m "Initial release" ``` Where `9fceb02` represents the commit hash. ### Push ```shell $ git push --tags ``` ### Delete ```shell $ git tag -d v1.0.0 ``` #### Remote ```shell $ git push origin :refs/tags/v1.0.0 ``` ## Git Bash -
jedmao revised this gist
Aug 17, 2019 . 1 changed file with 3 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -103,14 +103,12 @@ $ git add . ``` ### [Unstaging Changes / Restoring Files](https://git-scm.com/docs/git-restore) Maybe you accidentally staged some files that you don't want to commit. ```shell $ git restore foo.js $ git restore . ``` -
jedmao revised this gist
Aug 17, 2019 . 1 changed file with 18 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -40,27 +40,32 @@ $ git fetch upstream ## Every-day Workflow ### [Branching](https://git-scm.com/docs/git-switch) When working on a fork, you could be switching between different branches quite commonly. As such, you generally want to stay off the master branch and work on your own feature branches so that master is always clean and you can base new branches off of it. ```shell $ git switch -c <new-branch-name> ``` If upstream has a special develop branch or something, you can checkout that branch separately, but setup tracking so you can sync it up from time to time. Like the master branch, don't work directly on this one. Try to keep it clean. ```shell $ git switch -c <new-branch-name> --track upstream/<remote-branch-to-track> ``` Maybe you made some progress on a branch at work, but now you want to continue work at home. In that case, you're dealing with your own fork's branch, so you'll checkout from origin. ```shell $ git switch -c <new-branch-name> --track origin/<remote-branch-to-track> ``` If you're using the same name as a remote branch name, this can be simplified: ```shell $ git switch -c <branch-name> ``` Use the `-C` option flag to force it. ### [Switching Branches](https://git-scm.com/docs/git-switch) First, you'll want to know what branches are available in your working directory: ```shell @@ -70,13 +75,13 @@ $ git branch master ``` Now, you can easily switch between branches with `git switch`: ```shell $ git switch master $ git switch develop $ git switch feature_x $ git config --global alias.sw 'switch' $ git sw master ``` -
jedmao revised this gist
Jun 25, 2018 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,9 +17,9 @@ Click the "Fork" button at the top-right of any repository's GitHub page. ### [Clone a repo](https://www.kernel.org/pub/software/scm/git/docs/git-clone.html) Clone the foo repo into a new directory called foo: ```shell $ git clone https://github.com/<username>/foo.git foo ``` @@ -32,7 +32,7 @@ $ git remote -v Oh, it looks like we haven't setup upstream. Now is the time: ```shell $ git remote add upstream https://github.com/<upstream_username>/<repo_name>.git $ git fetch upstream ``` @@ -113,12 +113,12 @@ $ git unstage . Commit often. You can always squash down your commits before a push. ```shell $ git commit -m "Updated README" ``` Want to automatically stage files that have been modified and deleted, but new files you haven't told git about will be unaffected? Pass the `-a` or `--all` option flag: ```shell $ git commit -am "Updated README" ``` -
jedmao revised this gist
Jan 15, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -289,5 +289,5 @@ $ git config --global core.autocrlf true Once you're comfortable with what the above commands are doing, you might want to create some shortcuts for Git Bash. This allows you to work a lot faster by doing complex tasks in really short commands. Refer to my [.bashrc Gist](https://gist.github.com/jedmao/1deb357e54b3daf5856c#file-bashrc) for a good list of useful shortcuts. -
jednano revised this gist
Jul 2, 2013 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -65,6 +65,9 @@ Use the `-B` option flag to force it. First, you'll want to know what branches are available in your working directory: ```shell $ git branch develop feature_x master ``` Now, you can easily switch between branches with git checkout: -
jednano revised this gist
Jul 2, 2013 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -59,15 +59,15 @@ $ git checkout -b <new_branch_name> --track origin/<remote_branch_to_track> Use the `-B` option flag to force it. ### [Switching Branches](https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html) First, you'll want to know what branches are available in your working directory: ```shell $ git branch ``` Now, you can easily switch between branches with git checkout: ```shell $ git checkout master $ git checkout develop -
jednano revised this gist
Jul 2, 2013 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -59,6 +59,11 @@ $ git checkout -b <new_branch_name> --track origin/<remote_branch_to_track> Use the `-B` option flag to force it. To see a list of available branches on your working directory: ```shell $ git branch ``` ### [Switching Branches](https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html) -
jednano revised this gist
Jul 1, 2013 . 1 changed file with 28 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -243,6 +243,34 @@ $ git push origin :<remote_branch> ``` ### [Stashing](https://www.kernel.org/pub/software/scm/git/docs/git-stash.html) Sometimes you need to stash your changes so you can be on a clean branch or maybe because you want to go back and try something before you make a commit with these changes. Here's how you do a stash: ```shell $ git stash ``` Now, if you want to unstash those changes and bring them back into your working directory: ```shell $ git stash pop ``` Or maybe you want to unstash your changes without popping them off the stack. In other words, you might want to apply these stashed changes multiple times. To do this: ```shell $ git stash apply ``` For a list of stashes: ```shell $ git stash list ``` And to apply a specific stash from that list (e.g., stash@{3}): ```shell $ git stash apply stash@{3} ``` ### [Checkout as CRLF, Commit as LF](https://www.kernel.org/pub/software/scm/git/docs/git-config.html) ```shell $ git config --global core.autocrlf true -
jednano revised this gist
Jul 1, 2013 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -247,3 +247,11 @@ $ git push origin :<remote_branch> ```shell $ git config --global core.autocrlf true ``` ## Git Bash Once you're comfortable with what the above commands are doing, you might want to create some shortcuts for Git Bash. This allows you to work a lot faster by doing complex tasks in really short commands. Refer to my [.bashrc Gist](https://gist.github.com/jedhunsaker/1deb357e54b3daf5856c) for a good list of useful shortcuts. -
jednano revised this gist
Jul 1, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -37,7 +37,7 @@ $ git fetch upstream ``` ## Every-day Workflow ### [Branching](https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html) -
jednano revised this gist
Jul 1, 2013 . 1 changed file with 14 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -114,6 +114,19 @@ $ git commit -am "Fixed IE issues" ``` ### [Undoing Commits](https://www.kernel.org/pub/software/scm/git/docs/git-reset.html) The following command will undo your most recent commit and put those changes back into staging, so you don't lose any work: ```shell $ git reset --soft HEAD~1 ``` The next one will completely delete the commit and throw away any changes. Be absolutely sure this is what you want: ```shell $ git reset --hard HEAD~1 ``` ### [Squashing Commits](https://www.kernel.org/pub/software/scm/git/docs/git-rebase.html) Maybe you have 4 commits, but you haven't pushed anything yet and you want to put everything into one commit so your boss doesn't have to read a bunch of garbage during code review. @@ -140,7 +153,7 @@ $ git push origin <local_branch>:<remote_branch> Use the `-f` option flag to force it. ### [Undo Last Push](https://www.kernel.org/pub/software/scm/git/docs/git-reset.html) Some would say this is bad practice. Once you push something you shouldn't overwrite those changes. Instead, you're supposed to create a new commit that reverts the changes in the last one. So, technically, you shouldn't do this, but... you can? ```shell -
jednano revised this gist
Jul 1, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -206,7 +206,7 @@ $ git rebase -i HEAD~1 Now, you can replace "pick" with "r" and just change the commit message. ### [Manually Set Tracking](https://www.kernel.org/pub/software/scm/git/docs/git-config.html) Perhaps you forgot to setup tracking when you pulled down a remote branch. No worries: ```shell @@ -230,7 +230,7 @@ $ git push origin :<remote_branch> ``` ### [Checkout as CRLF, Commit as LF](https://www.kernel.org/pub/software/scm/git/docs/git-config.html) ```shell $ git config --global core.autocrlf true ``` -
jednano revised this gist
Jul 1, 2013 . 1 changed file with 44 additions and 16 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,31 +2,44 @@ ## Initial Setup ### [Initialize a repo](https://www.kernel.org/pub/software/scm/git/docs/git-init.html) Create an empty git repo or reinitialize an existing one ```shell $ git init ``` ### [Fork a repo](https://help.github.com/articles/fork-a-repo#step-1-fork-the-spoon-knife-repository) Click the "Fork" button at the top-right of any repository's GitHub page. ### [Clone a repo](https://www.kernel.org/pub/software/scm/git/docs/git-clone.html) Clone the codepainter repo into a new directory called codepainter: ```shell $ git clone https://github.com/jedhunsaker/codepainter.git codepainter ``` ### [Setup Remotes](https://help.github.com/articles/fork-a-repo#step-3-configure-remotes) First, let's see a list of the repositories (remotes) whose branches you track: ```shell $ git remote -v ``` Oh, it looks like we haven't setup upstream. Now is the time: ```shell $ git remote add upstream https://github.com/username/codepainter.git $ git fetch upstream ``` ## Every-day Commands ### [Branching](https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html) When working on a fork, you could be switching between different branches quite commonly. As such, you generally want to stay off the master branch and work on your own feature branches so that master is always clean and you can base new branches off of it. @@ -76,6 +89,7 @@ $ git add foo.js $ git add . ``` ### [Unstaging Changes](http://stackoverflow.com/questions/348170/undo-git-add-before-commit) Maybe you accidentally staged some files that you don't want to commit. @@ -126,6 +140,14 @@ $ git push origin <local_branch>:<remote_branch> Use the `-f` option flag to force it. ### Undo Last Push Some would say this is bad practice. Once you push something you shouldn't overwrite those changes. Instead, you're supposed to create a new commit that reverts the changes in the last one. So, technically, you shouldn't do this, but... you can? ```shell $ git reset --hard HEAD~1 && git push -f origin master ``` ### [Fetching](https://www.kernel.org/pub/software/scm/git/docs/git-fetch.html) Fetch changes from upstream: @@ -162,6 +184,7 @@ $ git pull origin/feature_x Rebasing is a way of rewriting history. In place of merge, what this does is stacks your commits on top of commits that are already pushed up. In this case, you want to stack your commits on top of `origin/feature_x`: ```shell $ git fetch origin $ git rebase origin/feature_x ``` @@ -170,30 +193,29 @@ If you already have a local branch set to track `feature_x` then just do: $ git rebase feature_x ``` Would you like to fetch, merge and then stack your changes on top, all in one shot? You can! If you have tracking setup on the current branch, just do: ```shell $ git pull --rebase ``` Another great use of rebasing is just rewriting commit messages. To get an interactive text editor for the most recent commit, do: ```shell $ git rebase -i HEAD~1 ``` Now, you can replace "pick" with "r" and just change the commit message. ### Manually Set Tracking Perhaps you forgot to setup tracking when you pulled down a remote branch. No worries: ```shell $ git config branch.<local_branch>.remote origin $ git config branch.<local_branch>.merge refs/heads/<remote_branch> ``` ### [Deleting Branches](https://www.kernel.org/pub/software/scm/git/docs/git-branch.html) Delete a local branch: ```shell @@ -206,3 +228,9 @@ Delete a remote branch on origin: ```shell $ git push origin :<remote_branch> ``` ### Checkout as CRLF, Commit as LF ```shell $ git config --global core.autocrlf true ``` -
jednano revised this gist
Jul 1, 2013 . 1 changed file with 42 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -128,9 +128,51 @@ Use the `-f` option flag to force it. ### [Fetching](https://www.kernel.org/pub/software/scm/git/docs/git-fetch.html) Fetch changes from upstream: ```shell $ git fetch upstream ``` Fetch changes from both origin and upstream in the same shot: ```shell $ git fetch --multiple origin upstream ``` ### [Merging](https://www.kernel.org/pub/software/scm/git/docs/git-merge.html) To be honest, I haven't used this command in quite some time. In my experience, it has created merge bubbles that have overwritten mine or others' code. For a better workflow, refer to rebasing, below. Nonetheless, here's how you merge-in changes from origin's `feature_x` branch: ```shell $ git fetch origin $ git merge origin/feature_x ``` ### [Pulling](https://www.kernel.org/pub/software/scm/git/docs/git-pull.html) Pulling is just doing a fetch followed by a merge. If you know your branch is clean (e.g., master branch), go ahead and get the latest changes. There will be no merge conflicts as long as your branch is clean. ```shell $ git pull origin/feature_x ``` ### [Rebasing](https://www.kernel.org/pub/software/scm/git/docs/git-rebase.html) Rebasing is a way of rewriting history. In place of merge, what this does is stacks your commits on top of commits that are already pushed up. In this case, you want to stack your commits on top of `origin/feature_x`: ```shell $ git rebase origin/feature_x ``` If you already have a local branch set to track `feature_x` then just do: ```shell $ git rebase feature_x ``` ### Manually set tracking. `git config branch.<local_branch>.remote origin`<br> `git config branch.<local_branch>.merge refs/heads/<remote_branch>` -
jednano revised this gist
Jul 1, 2013 . 1 changed file with 36 additions and 26 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,19 +31,22 @@ $ git fetch upstream When working on a fork, you could be switching between different branches quite commonly. As such, you generally want to stay off the master branch and work on your own feature branches so that master is always clean and you can base new branches off of it. ```shell $ git checkout -b <new_branch_name> ``` If upstream has a special develop branch or something, you can checkout that branch separately, but setup tracking so you can sync it up from time to time. Like the master branch, don't work directly on this one. Try to keep it clean. ```shell $ git checkout -b <new_branch_name> --track upstream/<remote_branch_to_track> ``` Maybe you made some progress on a branch at work, but now you want to continue work at home. In that case, you're dealing with your own fork's branch, so you'll checkout from origin. ```shell $ git checkout -b <new_branch_name> --track origin/<remote_branch_to_track> ``` Use the `-B` option flag to force it. ### [Switching Branches](https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html) Now, you can easily switch between branches with git checkout. @@ -55,6 +58,7 @@ $ git config --global alias.co 'checkout' $ git co master ``` ### [Status](https://www.kernel.org/pub/software/scm/git/docs/git-status.html) Not sure if you're working on a clean branch? Want to see what files have changed? Git status will show you a report. @@ -107,34 +111,24 @@ An interactive text file is displayed. You'll see the word "pick" to the left of ### [Pushing](https://www.kernel.org/pub/software/scm/git/docs/git-push.html) Push a local branch for the first time: ```shell $ git push --set-upstream origin <branch> $ git push ``` Push a local branch to a different remote branch: ```shell $ git push origin <local_branch>:<remote_branch> ``` Use the `-f` option flag to force it. ### [Fetching](https://www.kernel.org/pub/software/scm/git/docs/git-fetch.html) ### Manually set tracking. `git config branch.<local_branch>.remote origin`<br> @@ -153,4 +147,20 @@ $ git push origin master `git remote -v` ### Checkout as CRLF, Commit as LF `git config --global core.autocrlf true` ``` ### [Deleting Branches]() Delete a local branch: ```shell $ git branch -d <local_branch> ``` Use the `-D` option flag to force it. Delete a remote branch on origin: ```shell $ git push origin :<remote_branch> ``` -
jednano revised this gist
Jul 1, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,7 @@ Clone the codepainter repo into a new directory called codepainter: $ git clone https://github.com/jedhunsaker/codepainter.git codepainter ``` ### [Setup Remotes](https://help.github.com/articles/fork-a-repo#step-3-configure-remotes) ```shell $ git remote -v $ git remote add upstream https://github.com/username/codepainter.git -
jednano revised this gist
Jul 1, 2013 . 1 changed file with 19 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -87,9 +87,26 @@ $ git unstage . Commit often. You can always squash down your commits before a push. ```shell $ git commit -m "Fixed IE issues" ``` Want to automatically stage files that have been modified and deleted, but new files you haven't told git about will be unaffected? Pass the `-a` or `--all` option flag: ```shell $ git commit -am "Fixed IE issues" ``` ### [Squashing Commits](https://www.kernel.org/pub/software/scm/git/docs/git-rebase.html) Maybe you have 4 commits, but you haven't pushed anything yet and you want to put everything into one commit so your boss doesn't have to read a bunch of garbage during code review. ```shell $ git rebase -i HEAD~4 ``` An interactive text file is displayed. You'll see the word "pick" to the left of each commit. Leave the one at the top alone and replace all the others with "s" for squash, save and close the file. This will display another interactive window where you can update your commit messages into one new commit message. I like to use "f" instead of "s", because I usually work in such a way that I name my first commit appropriately from the get-go. "f" just skips the 2nd interactive file and uses the first commit message. ### [Pushing](https://www.kernel.org/pub/software/scm/git/docs/git-push.html) ```shell $ git push origin master -
jednano revised this gist
Jul 1, 2013 . 1 changed file with 10 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -51,6 +51,8 @@ Now, you can easily switch between branches with git checkout. $ git checkout master $ git checkout develop $ git checkout feature_x $ git config --global alias.co 'checkout' $ git co master ``` ### [Status](https://www.kernel.org/pub/software/scm/git/docs/git-status.html) @@ -70,9 +72,16 @@ $ git add foo.js $ git add . ``` ### [Unstaging Changes](http://stackoverflow.com/questions/348170/undo-git-add-before-commit) Maybe you accidentally staged some files that you don't want to commit. ```shell $ git reset HEAD foo.js $ git reset HEAD . $ git config --global alias.unstage 'reset HEAD' $ git unstage . ``` ### [Commits](https://www.kernel.org/pub/software/scm/git/docs/git-commit.html) -
jednano revised this gist
Jul 1, 2013 . 1 changed file with 12 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -53,16 +53,27 @@ $ git checkout develop $ git checkout feature_x ``` ### [Status](https://www.kernel.org/pub/software/scm/git/docs/git-status.html) Not sure if you're working on a clean branch? Want to see what files have changed? Git status will show you a report. ```shell $ git status ``` ### [Staging Changes](https://www.kernel.org/pub/software/scm/git/docs/git-add.html) Now that you've added or modified some files, you need to stage those commits into "the staging area." Think of git commits like an array of airlock hatches on a space ship. On this space ship, you can only open the door to one airlock at a time. When you open the hatch, you can put stuff in or take stuff out at will. Not until you've closed the door have you committed those changes (git commit) and not until you hit the red button do all those hatches open up into space (git push). You can stage inidividual files or all files at once. ```shell $ git add foo.js $ git add . ``` ### [Unstaging Changes]() Maybe you accidentally staged some files that you don't want to commit. ### [Commits](https://www.kernel.org/pub/software/scm/git/docs/git-commit.html) Commit often. You can always squash down your commits before a push. -
jednano revised this gist
Jul 1, 2013 . 1 changed file with 15 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -39,7 +39,7 @@ If upstream has a special develop branch or something, you can check that branch $ git checkout -b develop --track upstream/develop ``` Maybe you made some progress on a branch at work, but now you want to continue work at home. In that case, you're dealing with your own fork's branch, so you'll checkout from origin. ```shell $ git checkout -b feature_x --track origin/feature_x ``` @@ -53,7 +53,21 @@ $ git checkout develop $ git checkout feature_x ``` ### [Staging Changes](https://www.kernel.org/pub/software/scm/git/docs/git-add.html) Now that you've added or modified some files, you need to stage those commits into "the staging area." Think of the staging area like an airlock on a space ship. You put your stuff in there and close the door on it, but it doesn't actually go out into space until you hit the red button. You can stage inidividual files or all files at once. ```shell $ git add foo.js $ git add . ``` ### [Commits](https://www.kernel.org/pub/software/scm/git/docs/git-commit.html) Commit often. You can always squash down your commits before a push. ```shell $ git commit ### [Push](file:///C:/Users/Jed/AppData/Local/GitHub/PortableGit_ca477551eeb4aea0e4ae9fcd3358bd96720bb5c8/doc/git/html/git-push.html) ```shell -
jednano revised this gist
Jun 30, 2013 . 1 changed file with 36 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,6 +24,42 @@ $ git remote add upstream https://github.com/username/codepainter.git $ git fetch upstream ``` ## Every-day Commands ### [Branching](https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html) When working on a fork, you could be switching between different branches quite commonly. As such, you generally want to stay off the master branch and work on your own feature branches so that master is always clean and you can base new branches off of it. ```shell $ git checkout -b feature_x ``` If upstream has a special develop branch or something, you can check that branch out separately, but setup tracking so you can sync it up from time to time. Like the master branch, don't work directly on this one. Try to keep it clean. ```shell $ git checkout -b develop --track upstream/develop ``` Maybe you made some progress on a branch at home, but now you want to check it out at work. In that case, you're dealing with your own fork's branch, so you'll checkout from origin. ```shell $ git checkout -b feature_x --track origin/feature_x ``` ### [Switching Branches](https://www.kernel.org/pub/software/scm/git/docs/git-checkout.html) Now, you can easily switch between branches with git checkout. ```shell $ git checkout master $ git checkout develop $ git checkout feature_x ``` ### [Push](file:///C:/Users/Jed/AppData/Local/GitHub/PortableGit_ca477551eeb4aea0e4ae9fcd3358bd96720bb5c8/doc/git/html/git-push.html) ```shell $ git push origin master ### Checkout a remote branch with tracking. `git checkout --track -b <branch> origin/<branch>` -B to force it. -
jednano revised this gist
Jun 30, 2013 . 1 changed file with 8 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,17 @@ # Git Cheat Sheet ## Initial Setup ### [Initialize a repo](https://www.kernel.org/pub/software/scm/git/docs/git-init.html) Create an empty git repo or reinitialize an existing one ```shell $ git init ``` ### [Fork a repo](https://help.github.com/articles/fork-a-repo#step-1-fork-the-spoon-knife-repository) Click the "Fork" button at the top-right of any repository's GitHub page. ### [Clone a repo](https://www.kernel.org/pub/software/scm/git/docs/git-clone.html) Clone the codepainter repo into a new directory called codepainter: ```shell $ git clone https://github.com/jedhunsaker/codepainter.git codepainter -
jednano revised this gist
Jun 30, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ $ git clone https://github.com/jedhunsaker/codepainter.git codepainter ``` ### [setup remotes](https://help.github.com/articles/fork-a-repo#step-3-configure-remotes) ```shell $ git remote -v $ git remote add upstream https://github.com/username/codepainter.git $ git fetch upstream -
jednano revised this gist
Jun 30, 2013 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ $ git clone https://github.com/jedhunsaker/codepainter.git codepainter ``` ### [setup remotes](https://help.github.com/articles/fork-a-repo#step-3-configure-remotes) ```command line $ git remote -v $ git remote add upstream https://github.com/username/codepainter.git $ git fetch upstream
NewerOlder