DEV Community

Cover image for mastering git: the only essential commands you need to know to be a productive developer [pt 1] 🔥
 Khem Sok
Khem Sok

Posted on

mastering git: the only essential commands you need to know to be a productive developer [pt 1] 🔥

Image description

intro

git is something all programmers have used or will use one way or another. with its vast number of commands and options, it can be overwhelming for someone that is just starting out. learn these few essential commands and you'll be 80% there with your journey with git.

1 - git init

this command is used initialize a new git repository. it creates a .git folder wehre all of the version control information is stored.

git init # to initialize a repository
Enter fullscreen mode Exit fullscreen mode

2 - git clone

this command is used to clone an existing repository from a remote server to your local machine.

git clone https://github.com/user/repo.git # to clone a repository

git clone https://github.com/user/repo.git another-repo-name # to clone a repository and give it a different local name
Enter fullscreen mode Exit fullscreen mode

3 - git add

this command is used to add changes to the staging area.

git add . # to add all changes in the current directory

git add file.txt # to add changes in a specific file
Enter fullscreen mode Exit fullscreen mode

4 - git commit

this command is used to save changes to the repository. the changes can be a single file or multiple files. when you run git commit, you'll be prompted to write a commit message to describe the changes you've made.

git commit -m "commit message" # to commit changes with a message directly from the command line

git commit -a # to commit all changes in the repository
Enter fullscreen mode Exit fullscreen mode

5 - git push

this command is used to push changes from your local to a remote repository.

git push origin main # to push changes to the remote repository named "origin" and the branch named "main"

git push -u origin main # to set the upstream branch for "origin/main" to "main"
Enter fullscreen mode Exit fullscreen mode

6 - git pull

this command is used to retrieve changes from a remote repository and merge them into your local repository.

git pull origin main # to get changes from the remote repository named "origin" and the branch named "main"

git pull --rebase # to reapply your local changes on top of the changes in the remote repository
Enter fullscreen mode Exit fullscreen mode

7 - git checkout

this command is used to switch between different branches in the repository. branches allow you to work on different version of the project.

git checkout -b new-branch # to create a new branch named "new-branch" and switch to it

git checkout branch-name # to switch to an existing branch named "branch-name"
Enter fullscreen mode Exit fullscreen mode

conclusion

these are the seven essential git commands that you need to get started. mastering these command, and you'll be on your way to become a git expert.

Top comments (0)