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
- it adds the file to the staging. (adds the file to your project)
git add .
- 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"
- command is used to upload local repository(local commits) content to a remote repository.
git push
- 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
- Don't use this command until you don't know its consequences.it forcibly pushes your local commits to the remote.
git push -f
- to update/fetch your local repository from remote ( your codebase now up to date with a remote repo)
git pull
- to check all the branches
git branch
- to change the branch or check out the branch
git checkout <branch_name>
ex:
git checkout development
- 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
- it shows all the changing logs including ( commits, author, time, branch)
git log
- 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>
- it shows the configuration list
git config --list
- to reset the changes in a specific branch
git reset
- to merge changes with the specific branch. ( it merges changes on your local from remote )
git merge <branch_name>
- it shows the remote URL of git on which remote repo it pushed or pulled
git remote -v
- it sets the remote URL( from which you pull or update local with remote )
git remote set-url <url_of_origin>
- in this file you add those file paths which you don't want to push on remote
.gitignore
- 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
- 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
I hope, this blog helped you as a beginner. thank you for reading
and all the best for the journey :)
Top comments (0)