The git commands I learned in my first two weeks of coding.
Hello everyone, I’m here to share with you the git commands that I learned/used in my first two weeks of coding and how one of them saved me from myself.
This list won’t be going much into the intricacies of certain commands and should just be looked at as a simple how-to list of very basic commands. Still here? Let’s do this…**is being used in the code blocks below to insert comments**
- git init — First things first, this starts up a local (on your computer) repository. Make your way into the directory you want and enter this command.
2. git remote add origin firstname.lastname@example.org:your-github-username/your-github-repository — This connects your newly created local repository to your Github!
git remote add origin email@example.com:your-github-username/your-github-repository
3. git add — Adds files to the repository.
git add README.md **README.md is our file**
git add . **Don't overlook that (.)! This adds all files**
4. git commit -m “Useful message inside quotes” — These messages will be useful later, especially if you’re making multiple commits of the same file. These commits can be looked at like save states so the messages can give you an idea of where you are. The messages also appear next to the most recent add files. Commits are stored locally until pushed.
git commit -m "Useful message inside quotes"
git commit --amend -m "Change your commit message with amend"
5. git push — This pushes updates to the Github repository (in the cloud) that you have connected.
git push -u origin main **used when you first push a new branch**
git push **shorter command that can be used after above command**
6. git status — This gives you all the information about the current status of the branch.
7. git stash — This is what saved me! See the screenshot above? The git stash command essentially takes all uncommitted changes and stores them to be reapplied for a later use. It then reverts to the most recent commit. I was able to use git stash for a quick revert and save myself some headache after realizing I incorrectly updated a local database. This was also without having a remote repository initiated, so I had nothing to pull from remotely.
These next steps show you how to revert to an earlier state after multiple commits.
8. git log — This is a log that keeps track of all commits and the messages attached to each commit, the image below only shows the commits of your local repository. There’s also an option on Github that shows commits as well. Hit “Q” on your keyboard to get back to the command line if you’re having issues typing.
Look a little messy or long? Give this line a try instead…
git log --oneline **gives a simplified version of git log**
9. git checkout — Using the commit messages as indicators of where to revert from, insert the commit id and you’ll be working from that old save state in no time. You can then add, commit, and push from here just like normal. Don’t forget that period (.) at the end there either! Happy coding!
git checkout <commit id here> . **again don't forget that (.)!**