SHARE
TWEET

Untitled

a guest Apr 21st, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ### Amend misspelt commit message
  2. `git commit --amend`
  3.  
  4. ### Rename a branch
  5. `git branch -m feature-brunch feature-branch`
  6.  
  7. If you have already pushed this branch, there are a couple of extra steps required. We need to delete the old branch from the remote and push up the new one:
  8.  
  9. `git push origin --delete feature-brunch`  
  10. `git push origin feature-branch`
  11.  
  12.  
  13. ### Accidentally committed all changes to the master branch
  14.  
  15. So you are working on a new feature and in your haste, you forgot to open a new branch for it. You have already committed a load of files and now those commits are all sitting on the master branch.
  16.  
  17. So we can roll back all those changes to a new branch with the following three commands:
  18.  
  19. Note: Make sure you commit or stash your changes first, or all will be lost!
  20.  
  21. `git branch feature-branch`  
  22. `git reset HEAD~ --hard`  
  23. `git checkout feature-branch`  
  24.  
  25. This creates a new branch, then rolls back the master branch to where it was before you made changes, before finally checking out your new branch with all your previous changes intact.
  26. Forgot to add a file to that last commit
  27.  
  28.  
  29. ### Committing too early
  30. You missed a file, forgot to save it, or need to make a minor change for the last commit to make sense. --amend is your friend once again.
  31.  
  32. Add that missed file then run
  33.  
  34. `git add missed-file.txt`  
  35. `git commit --amend`
  36.  
  37. At this point, you can either amend the commit message or just save it to keep it the same.
  38. Added a wrong file in the repo
  39.  
  40. You added a file that you didn’t want to commit? A rogue ENV file, a build directory, a picture of your dog that you accidentally saved to the wrong folder? It’s all fixable.
  41.  
  42. If all you did was stage the file and you haven’t committed it yet, it’s as simple as resetting that staged file:
  43.  
  44. git reset /assets/img/misty-and-pepper.jpg
  45.  
  46. If you have gone as far as committing that change, no need to worry. You just need to run an extra step before:
  47.  
  48. git reset --soft HEAD~1
  49. git reset /assets/img/misty-and-pepper.jpg
  50. rm /assets/img/misty-and-pepper.jpg
  51. git commit
  52.  
  53. This will undo the commit, remove the image, then add a new commit in its place.
  54. Oops… I did it again
  55.  
  56. This command is for when everything has gone wrong. When you have copy-pasted one too many solutions from Stack Overflow and your repo is in a worse state than it was when you started. We have all been there.
  57.  
  58. git reflog shows you a list of all the things you've done. It then allows you to use Git's magical time-traveling skills to go back to any point in the past. I should note, this is a last resort thing and should not be used lightly. To get this list, type:
  59.  
  60. git reflog
  61.  
  62. Every step we took, every move we made, Git was watching us. Running that on our project gives us this:
  63.  
  64. 3ff8691 (HEAD -> feature-branch) HEAD@{0}: Branch: renamed refs/heads/feature-brunch to refs/heads/feature-branch
  65. 3ff8691 (HEAD -> feature-branch) HEAD@{2}: checkout: moving from master to feature-brunch
  66. 2b7e508 (master) HEAD@{3}: reset: moving to HEAD~
  67. 3ff8691 (HEAD -> feature-branch) HEAD@{4}: commit: Adds the client logo
  68. 2b7e508 (master) HEAD@{5}: reset: moving to HEAD~1
  69. 37a632d HEAD@{6}: commit: Adds the client logo to the project
  70. 2b7e508 (master) HEAD@{7}: reset: moving to HEAD
  71. 2b7e508 (master) HEAD@{8}: commit (amend): Added contributing info to the site
  72. dfa27a2 HEAD@{9}: reset: moving to HEAD
  73. dfa27a2 HEAD@{10}: commit (amend): Added contributing info to the site
  74. 700d0b5 HEAD@{11}: commit: Addded contributing info to the site
  75. efba795 HEAD@{12}: commit (initial): Initial commit
  76.  
  77. Take note of the left-most column, as this is the index. If you want to go back to any point in the history, run the below command, replacing {index} with that reference, e.g. dfa27a2.
  78.  
  79. git reset HEAD@{index}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top