Your basic guide to learn Git commands
Originally published atmedium.com
15 Git commands to make you a know-it-all Git pro!
You’re here to learn Git commands? You’ve landed on the correct page!
If you are a developer and don’t know anything about Git, I advice you to have a read through this article — New Developer? You should’ve learned Git yesterday.
Before going ahead, I assume you know why we use Git. As this article will guide you on how to use Git on the terminal.
Many people come to me and ask me to teach them Git. But first things first, according to my experience, Git can only be learned by actually managing a repository.
Just like how we cannot learn how to ride a bicycle by reading or listening about how to cycle — We actually, have to physically ride the cycle and that’s how we learn cycling.Just like how we cannot learn git by just reading or listening about how to use git — We actually have to create a test Repository, play around and that’s how we learn git.
My point is, DO!
While cycling, you may go off balance, you will fall down, get hurt. But things go on. That is a process what makes you learn how to cycle.While using git, you may bump into complex merge conflicts, you may have to rebase all over again. But things go on. That is a process what makes you a pro in Git.
My point is, FAIL!
If you use Git GUI like GitHub Desktop, Kraken, etc, I’ll recommend you to switch to using Git on terminal. In the long run, when you will look for efficiency while developing. You’d never want to leave your keyboard and switch to a mouse and do a git pull and git push on the GUI.
While writing code, time is always very crucial and you have to be smart enough to do the mundane work faster. You’ll fit in time to learn newer things or maybe just some extra family time
Keeping the above points in mind, Let’s dive into Learning Git, the-terminal-way.
Using Git on the command line is a very efficient and clear way of managing your repository.
GETTING STARTED
Assuming you have your Github account, Create a new repository.
Using git is about push (pushing your code to the remote server) and pull (pulling latest changes of your team / colleagues from the server).
- git init (Creating a Git repository on local machine)
- git add . (You are adding (staging) ALL your changes for a commit. This is also called staging your changes)
- git commit -m ‘Feature added’ (Here, we are giving the commit a description. The changes you have staged in the command #2 will be commited)
- git remote add origin https://github.com/userName/repoName (If the existing folder on your computer does not point to a Github repo, you can use the command to sync it to the intended git repo)
- git push origin branch-name (Pushes your local branch to remote branch, to be used if branch doesn’t exist on remote yet)
- git push (Pushes your commits in the local branch to the remote branch, only if the branch is already created on remote)
Git Branch Naming
It is best to name the Git branch based on what feature you will be working on, on that particular branch.
Hence, If I am working on implementing a Login UI feature, I would name my branch login-ui
Carrying on …
git pull (Pulls the latest changes from remote to local along with all other newly created branches on remote)
8. git checkout -b test (If you’re on master branch or any other branch, then this command clones the working branch and creates a new branch called test)
9. git branch -a (Displays all remote as well as local branches)
10. git stash (Say if you’re adding code to your newly created branch and all of a sudden you realise it’s all wrong and need reverse back to previous state of the branch, git stash will temporarily shelve all your changes and you’ll be back to where you started)
11. git stash pop (You can bring back the changes which you have stashed using git stash, the changes can be brought back to any branch)
12. git stash drop (Drop / Remove / Delete the stashed changes)
13. git clone -b branch-name https://giturl/userName/repoName (Allows you to clone specific branch [branch-name] to local)
14. git branch -D branch-name (Delete local branch)
15. git push origin --delete remote-branch-name (Delete remote branch)
These are the 15 commands I used the most when I started using git on the command line, I felt I should document and share them online as they’re quite basic and good to start off with.
To add on, you’d like to read about git merge and git rebase here — https://hackernoon.com/git-merge-vs-rebase-whats-the-diff-76413c117333