DEV Community

Klesta
Klesta

Posted on • Originally published at klescode.hashnode.dev

Git Basics: Git Commands and How to Use Them

If you’re a beginner in the world of development and you’re ready to begin using the terminal, these are some basic git commands that can get you started.

Git is a powerful tool for version control - it helps you keep track of changes in your code and easily collaborate with other developers. In my opinion, git is the coolest thing since sliced bread.

Git creates and stores snapshots of your code, called commits, in a repository. A repository (repo) is a folder that keeps track of your code and its changes. You can create a repo on your local machine, or on a remote server like GitHub.

To use git, open a terminal and navigate to the folder where your code is. Once you’re in the correct folder, you can run git commands to interact with your repository. Here are some of the most common ones along with a brief description of each command:

git init - makes the existing directory a git repository.

git status - shows you the current state of your repo. It lists which files have been modified, added, deleted, or are untracked (not part of the repository yet).

git add . or git add <file> - if you use the period (.) it adds all the files in the current folder  to the staging area. Add the <file> name if you just want to add a specific file to the staging area. The staging area is where you prepare your files for the next commit.

git commit -m “<message>” - commits the staged snapshot. Including a message in this command prevents a text editor from launching so you can do it directly from this command. The message should describe what changes you made to the code.

git push - sends your commits to GitHub, where they are stored in your repo. This is important if you want to collaborate by sharing your code with others or back it up online.

git log - shows you the history of your commits, beginning from the most recent one. You can see different types of information like the message, date, author, and ID of each commit. You can also add some options at the end of the command to filter or format the output, for example --oneline, --graph, or --pretty. So it would look like this git log --oneline.

git diff - shows the differences between two versions of the code. You can compare between your working directory (the files you are currently editing) with the staging area (the files you have added) or the last commit (the files you have committed).

git branch - shows you the list of branches in your repo. A branch is like a parallel version of your code where you can work on different features or experiments without affecting the main branch (usually called master or main). Branches are a lot of fun - read more about it here.

git checkout -b <branch> - creates a new branch (<branch>) and switches to that branch.

git checkout <branch> - switches to an existing branch (<branch>). When you switch branches, your code editor matches the current branch you’re on.

git pull - it pulls the latest changes from the remote server and merges them with your local branch. Use this command if someone else has pushed new commits to the same branch as you, and you want to sync your code with theirs. Also use it to update your main branch locally after someone else has merged their changes into the main branch.

Useful Resources

If you like to learn by playing games, Learn Girl Branching and Oh My Git! are fun ones to try.

When I first got started with command lines, I took this free Udacity course: Version Control with Git.

Top comments (0)