Basic git commands: Learn how to use git.

Sahil Pandya
4 min readDec 6, 2021

--

GIT Commands

Git is used for source control of your project with multiple contributors.

If you are using Mac, then run below command to install git

brew install git

or follow this link https://git-scm.com/downloads

  1. git config The command for the set your user or Email id, which should be added in the remote repo.
a. git config –global user.name "[name]"b. git config –global user.email "[email address]"

2. Now add your remote repo, Run below command

git remote add origin git@github.com:User/UserRepo.git

or If you want to change the url then run below command

git remote set-url origin git@github.com:User/UserRepo.git

3. Now you can check your remote url, Run below command

git remote -v

4. Initialize your local repo git run git init Command.

5. If we want to clone already existing repo then use

git clone https://github.com:User/yourRepo.git

6. Once git setup done in your local repo, Now we will check what are the commands required to add new files and push to the remote and other most use commands.

7. git checkout -b yourBranchNameCheckout command using -b will create the new branch in your local.

8. If want to check out the existing branch where we want to push the code then we need to run git checkout BranchName

9. While running git checkout if branch name is not existing then we need to pull all the branches in our local using git fetch It will fetch all the branches.

10. git status Git status will show all the files list wherever we have changed the code.

11. git add (The command uses for adding the untracked files which we want to push to the remote). There are 2–3 ways we can use above command lets discuss more:

a. git add . or git add -ait will add all the modified + untracked files

b. git add -u It will add all the files except new created files

12. git commit (The Command uses for the committing with the message, It will help to track that which commit id has what are the changes.) We can use like below:

a. git commit -m ‘here commit related message needs to add’ It will create the commit id with the message.

13. Once commit is done, We need to push commit to the remote repo.

git push ( The command uses for pushing the commit to remote)

git push origin branchName We need to define branchName where we want to push our code.

14 .While pushing the code, possible same repo some other developer also pushes the code, then We need to pull the code. For that we need to run

git pull origin brachName

15 .While pulling the code possible we will get conflicts if we work on the same files. For that We need to resolve the conflicts, Resolving conflicts there are certain ways to resolve:

a. Whatever files we are getting the conflict we need to open the file one by one in our editor.

b. Compare code there and our code, There will be <<<From and <<<To code, whichever do we want to keep, just take it and other we can remove if it’s not required.

c. So once all the file’s code resolved, we need to push again all the modified files that you need to follow all the above steps.

Other use full commands are:

  1. git stash or git stash 'stashName' It will hide your local change in the cache if sometime want to pull some other code or want to change branch that time this command will be useful.
  2. If we want to pop again then git stash apply {stashName or stashNumber}
  3. If we want to reset added file then we need to run git reset fileName or all the files want to reset, then rungit reset
  4. If we want to hard reset all our local changes, then run git reset --hard HEAD~1 It will reset till previous commit.
  5. git diff It will show the difference in the code of each file, what is the change we did in the code, it will help before pushing the code we can cross check once.
  6. git log It will show all the previous commits which we did.
  7. git branch It will show all the branches list which you created or you checkout in your local.
  8. git branch -D branchName It will delete the branch in your local.

If want to know more about git commands, then go through the git official document which will explain everything in the details.

or Some other useful tutorials:

Thank you for reading, Please let me know in the below comment box if any feedback or queries.

--

--

Sahil Pandya
Sahil Pandya

Written by Sahil Pandya

Learning, Exploring, Implementing and then Writing things after experience.

No responses yet