DEV Community

Collins Mutai
Collins Mutai

Posted on

Basic Git Commands

In this brief intro, we'll be diving into Git and learn basic git commands. Git is a version control system which comes handy when you're writing code as a team or independently. Some features that git allows includes; creation, merging, and deletion of multiple local branches that can be entirely independent of each other.

installation

[https://git-scm.com/downloads]
Use the git installer and follow the steps.
Check for successful installation and version

git --version

Set global configurations

Now lets configure git by running the following global commands.

git config --global user.name "user name"
git config --global user.email "email address"

git init

Initializes your local git repository as your master.

git add . or git add "file name"

To add all the files to staging area, run

git add .

or add specific file using

git add <file name>

git commit -m "commit message"

To save the changes to your repository with a message which describes the commit.

git remote add origin "repository URL"

When you have an online repository and would like to link it to your local repository, you can do so by running.

git remote add origin <repository URL>

git push -u origin master

To push the local repository to the online repository.

git remote rename origin upstream

Running this command will rename the online repository. Then add the new URL using "git remote add origin <repository URL" and push the local to the online using "git push -u origin master"

git clone "repository URL"

To clone an online repository to your local machine.

git revert

Will reverse changes made by an earlier commit.

git status

To view the current status of the repository.

git checkout

View file from last commit and add it to staging area.

git restore --staged

Unstage files from last commit.

git restore

Restore file to original last commit.

git log --online

Show all commits.

git log

Display detail information about all commits.

git -b "branch name"

Running this command will create a new branch.

git branch -a

Show all branches.

git checkout "branch name"

Will move git from the master branch to the new branch.

git merge "branch name"

If there are new changes that needs to be added to the master, you can do so by switching back to the master branch.

git branch -a

To list branches.

git pull

To match the remote repository to the local.

Top comments (0)