In the beginning, working with git can be quite daunting - things get lost and can’t seem to be found in the depth of the terminal and all this talk about rewriting history and every developer's personal git horror stories are not making things easier.
That being said, git is also a super powerful tool. It’s absolutely essential to everyday work as a developer and while it might cause you some headaches it’s also very likely to save your butt at some point.
For me personally, working with git brings up a whole mix of emotions: Sometimes it feels like having superpowers, sometimes like going a little too fast without headlights on.
To make this experience a little less scary, I have a list of basic terminal commands I come back to almost every day - if you are a beginner like me, maybe these are a good starting point.
Knowing where you are at
git status - gives you an overview of your file status: are there modified changes? Is there anything you didn’t commit yet? Also very useful to check whether there were changes that you do not want to push.
git log - returns all commits made to your repository. For example useful if you want to get the commit ID for a rebase. To get out of the vim editor (a whole other story) where these logs are shown, use the command ‘wq’ (write & quit) to get back to your console.
pwd - not exclusively git related, but helpful either way. Prints your working directory, that is the path of the file you are in.
Adding things
git checkout -b creates a new branch for you to add your changes to. If you are contributing to an existing project, checkout their naming conventions before going wild with your branch name. See below how you can checkout already existing branches.
git add . - adds all changes you made to the stage. You can also just add specific files by using git add <filename>
git commit -m “commit message” - commits your staged changes and adds a new commit message. If you just made minor changes, you might want to use git commit --amend
git commit --amend - adds your staged changes to the last commit you pushed without adding an additional commit message.
git push - pushes your local changes to a remote repository.
Git pull vs. git fetch
git fetch - retrieves changes and additions from a remote repository, however doesn’t change any of your local branches.
git pull - pulls changes from a remote repository into your current branch. Basically doing a git fetch
followed by a git merge
.
git checkout - checking out an existing branch from a remote repository by name. You can also check out specific commits with git checkout <SHA>
.
Abort, abort!
Ctrl + C - stops whatever process is currently running in your terminal.
git reset --hard origin/ - You can also reset your current branch to a remote branch by specifying the branch name.
This of course, is a very basic and not at all extensive list - I hope it helps you either way. Is there anything existential missing from your point of view? Feel free to share it in the comments - same goes for git horror- and love stories or helpful resources! <3