CodeX
Published in

CodeX

Git — All the basic commands you need to know

Git, it is notorious for managing project files and tracking changes made by you and the collaborators among developers, programmers, and designers who code.

Git — All the basic commands you need to know
Image Credit: Author

The biggest advantage Git provides is the ability to duplicate the project also termed as ‘branches’ in Git, and then save the changes to the main file once you’re sure about the edit. I’m glad I’m onboard now as it saves me from regrets and my mind from figuring out the right file among all the files I duplicated.

I sat down and took a beginner-friendly Git course and tried to sum up the commands, I learned, here. There’s a little surprise for you at the end of this post.

Here it goes!

git config --global user.name YourName

↪ This command is used by the user for registering itself to get accounted for the commits (explained) the developer has made.

git config --global user.email YourEmail

↪ This command is used by the user for making commits and registering it under the user’s email.

​​git config --global user.name

↪ This command lets you recheck the name under which the changes are being made.

git config --global user.email

↪ This command is used by the user to recheck the email under which the changes are being made.

git init

↪ This command is used by the user to initialize an empty git repository.

git status

↪ This command is used to show the status of the files in a repository. It helps the user track the no. of ‘untracked’ files in a repo.

git add Yourfile.html

↪ This command lets you add/move that particular file to the staging area.

git add -A

↪ This command lets you add/move all untracked files to the staging area.

git commit

↪ This command commits the staged file. Commit simply means saving the changes or taking a snapshot of the files so that it can be compared or used to recover.

git commit -m “Yourmessage”

↪ This command is used to commit but also avoid vim editors.

git commit -a -m “Yourmessage”

↪ This command is used to add the project to staging area and commit simultaneously.

touch Yourfile.html

↪ This command creates a file.

git checkout Yourfile.html

↪ This command helps you retrieve the last commit made to the particular file.

git checkout -f

↪ This command helps you retrieve the last commit made to all particular files. Useful when the amount of files need to be recovered is massive.

git log

↪ This command shows you the logs of when the commits are made and by who the commits are made.

git log -p -”no. of last commits you want to see the logs of. For eg: 2"

↪ This command shows the logs of the last “requested no. of” commits.

git diff

↪ This command lets you see the changes made or difference between the committed version and untracked or uncommitted version.

git diff --staged

↪ This command lets you see the changes made or difference between the committed/unmodified version and the staged version.

git rm --cached Yourfile.html

↪ This command lets you remove the particular file just from the staging area.

git rm Yourfile.html

↪ This command removes the said file from the hard drive itself.

.gitignore

.gitignore is used when the user wants to ignore a particular file or a particular type of file within its folder and within the directory.

Git Branches

Branches are the most amazing features of the git. It lets the user create a branch/copy of a project so that any changes that are to be made and tested can be done without comprising the main one. Any changes made on the branched project have no effect on the main/master one.

That’s not even the best part, once the changes are made and tested successfully, you can merge the branched project with the main one and continue with the project as it was one.

git branch branchname

↪ This command lets you create a new branch with that particular branch name.

git branch

↪ This command lets you see which branch is selected. It will be marked in green color

git checkout branchname

↪ This command will let you switch between branches.

git merge Yourbranchname

↪ This command is used to merge the branched project with the main/master one.

Note: You must checkout to the master branch before performing this command.

git checkout -b branchname

↪ This command lets you create a new branch with that particular branch name and switch you to that branch simultaneously.

git remote add origin “Repository’s URL”

↪ This command lets you add your local files/ host your files to Github to be accessed remotely.

Note: That repository URL can also be referred to by the name “origin”.

“origin” is basically a name given to your GitHub repository’s URL, so that you don’t have to mention the URL every time you make a pull/push/fetch request.

git remote

↪ This command shows you the names of repositories you’ve added. For me, it shows origin.

git remote -v

↪ This command shows you the ‘fetch’ and ‘push’ URL for that repository.


git push origin master

↪ This command pushes/uploads your repository that’s in the master branch, to the origin.

git push

↪ This command pushes your repository to the last default branch.

git push -u origin master

↪This command lets you change the default branch that should get pushed to GitHub.

Note: I hope you understand the difference among the above three commands, the first one is basically you, telling/mentioning which branch is to be pushed. Second, it automatically pushes to the default branch (which is set by the third command). And, third, it let’s change the default branch.

git remote set-url origin “repository’s new url”

↪ This command changes the URL under “origin’s” name. You check this change by typing this command.

git clone “Repository’s URL you wish to clone” <folder name>

↪ This command clones the repository you wish to work on, and saves it in a folder you specified.

git clone “Repository’s URL you wish to clone”

↪ This also clones the repository you wish to work on but saves it in a folder with the same name as the repository’s.

Well, that was it. Clap if you found this helpful and follow for more amazing content on tech and programming stuff.

Git — All the basic commands you need to know
Image Credit: Author

Oh, and remember the gift I was talking about in the start. Well, you don’t have to bookmark this page. Instead, you can get this eBook published by me. You can get it for FREE or pay whatever you want and make me feel grateful.

Cheers!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bhavesh Rawat

Bhavesh Rawat

153 Followers

20 • Aspiring Web Developer • Tech Enthusiast • Blogger • Curator