DEV Community

Blue
Blue

Posted on

Basic Git Useages

This article is about using Git, the version control system that we are most currently using in our developer journey.

Basic Git Commands

: git init , git clone, git status, git add, git commit, git push, git fetch, git pull

git init = to create a git repository for your project.

For the first time when you create your project, and, in that project, you will use git, you need to use that command to use the other git commands for version control.

git clone = copying repository in your local

if you want to copy existing repository, git clone command is for you.

git status = checking the status of your files

If you edit or delete anything from your current stage, this command can show the status of the current stage. So, you can check which files you changed.

git add = adding modified files to your current stage

You modified or deleted some files in your current stage. To saved that changed files in your current stage, you need to use git add command.

git add . ⇒ adding all changed files to current stage.

git add ⇒ adding specific changed file to current stage

git commit = saving your changed files, which you have added in your stage

After you have added your modified files, you need to save that stage. In that time, you have to use git commit commands.

git commit -m “{message you want to show}

git commit -a -m “{message you want others to know}”

if you use -a in git commit command, you can skip using git add command. In git commit -a -m “{Message}”, -a is adding to stage and -m is for message.

git push = to push your committed stage to remote repository from your local

If you want to push your committed files to GitHub repository, you need to use this command.

git push ⇒ push from local to remote main or master repository

git push -f ⇒ flag -f means you want to push by force.

git push -u [remote repository name] [branch name that you want to push from local] ⇒ flag -u is upstream, like tracking branch with your local branch.

PS: The best practice is you may need to git pull or git fetch before you push anything to the repository in order to reduce merge conflicts later.

git pull and git fetch ⇒ grabbing updates from your remote repository.

Between these two commands, git fetch is used for checking the latest update of your repository, but git pull merges the latest update downloaded to your local.

git fetch ⇒ fetch update from remote repository

git fetch -a ⇒ fetching updated information from all remote repositories.

git fetch ⇒ fetching specific repository.

git pull = git fetch + git merge

PS: After your git fetch, you will know which files got updated, and then you can use git diff to see the differences. You can fix and you can git merge.

You can take reference from this YouTube video for educational purposes. They explain pretty well.

(19) Git Fetch | What is Git Fetch and How to Use it | Learn Git - YouTube

Top comments (0)