DEV Community

Cover image for A Git beginner should learn these basic git commands
ranjit shah
ranjit shah

Posted on

A Git beginner should learn these basic git commands

Every developer at the beginning of his journey always faces issues with Git & GitHub and most organizations use Git as version control for their source code. In this blog, we will learn basic commands of git which are used by developers in their daily life.

So, Lets Start


Git Commands

  • to initialize a git repository
git init
Enter fullscreen mode Exit fullscreen mode
  • it adds the file to the staging. (adds the file to your project)
git add .
Enter fullscreen mode Exit fullscreen mode
  • it makes all the changes in the staging file on your local repository. (commits the changes in the file)
git commit -m "<commit_message>"
ex:
git commit -m "first commit"

also, you can add and commit together by using this
git commit -a "first commit"
Enter fullscreen mode Exit fullscreen mode
  • command is used to upload local repository(local commits) content to a remote repository.
git push
Enter fullscreen mode Exit fullscreen mode
  • same as push command "-u" here represents an upstream sign which means you can upload your local commits to selected remote/branch
git push -u <remote_name> <branch_name>
ex:
git push -u origin main
Enter fullscreen mode Exit fullscreen mode
  • Don't use this command until you don't know its consequences.it forcibly pushes your local commits to the remote.
git push -f
Enter fullscreen mode Exit fullscreen mode
  • to update/fetch your local repository from remote ( your codebase now up to date with a remote repo)
git pull
Enter fullscreen mode Exit fullscreen mode
  • to check all the branches
git branch
Enter fullscreen mode Exit fullscreen mode
  • to change the branch or check out the branch
git checkout <branch_name>
ex:
git checkout development
Enter fullscreen mode Exit fullscreen mode
  • to cut new branch. always run this command from that branch you want to cut a new branch
git checkout -b <new_branch_name>
ex:
git checkout -b newbranch
Enter fullscreen mode Exit fullscreen mode
  • it shows all the changing logs including ( commits, author, time, branch)
git log
Enter fullscreen mode Exit fullscreen mode
  • to config git author which shows on remote repo who pushed the changes
git config user.name <git_username>
git config user.email <git_email>

Enter fullscreen mode Exit fullscreen mode
  • it shows the configuration list
git config --list
Enter fullscreen mode Exit fullscreen mode
  • to reset the changes in a specific branch
git reset
Enter fullscreen mode Exit fullscreen mode
  • to merge changes with the specific branch. ( it merges changes on your local from remote )
git merge <branch_name>

Enter fullscreen mode Exit fullscreen mode
  • it shows the remote URL of git on which remote repo it pushed or pulled
git remote -v
Enter fullscreen mode Exit fullscreen mode
  • it sets the remote URL( from which you pull or update local with remote )
git remote set-url <url_of_origin>
Enter fullscreen mode Exit fullscreen mode
  • in this file you add those file paths which you don't want to push on remote
.gitignore
Enter fullscreen mode Exit fullscreen mode
  • it reset a branch by 1 commit. means set back a branch by 1 commit. you can replace 1 by number (no. of commits revert)
git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode
  • it rebases your branch with a named branch
git rebase -i <branch>

for ex:
branch1: head
branch2: dev
git checkout dev
git rebase head

it looks like  head->dev
now head added as a base in dev branch
Enter fullscreen mode Exit fullscreen mode

I hope, this blog helped you as a beginner. thank you for reading
and all the best for the journey :)

Top comments (0)