As you might know, learning git is very important but it can be overwhelming for beginners because they feel like they have to learn every single command, that’s why I will show you the most important git commands.
And again you don’t have to learn them all, I know by memory all the commands I’m gonna show you cause I use them all the time, but it’s enought to know that the commands exist so when you need them you can just google them.
Git clone
git clone https://example_repo_url.org/path_to_repo/project_app.git
This command is important but, you don’t have to learn this one because you only need to use it if you don’t have the repo you want to work on, just keep it in mind.
Git pull
git pull
Git pull is a command you want to run before pushing anything if you are working in a team of developers so you have the latest changes and can resolve conflict issues if you get any.
Git add
The following way to use git add will add files individualy.
git add file_name_here
If you want to add all the files you made changes to at the same time just add a dot after as shown below.
git add .
You want to run this command whenever you need to push changes to remote, with this command you’re basically choosing what files to commit.
Git status
git status
The git status command gives you information about the branch you’re on, it let’s you know if your code is up to date, files ready to commit, commited files, and basically any change you make to your branch.
Git commit
git commit -m "your commit message here in between the quotation marks"
This command saves changes locally and gets them ready to be pushed and as you noticed you also need to write a message of the changes you made.
Git push
git push
After commiting your changes locally, now you are ready to push them to the remote repo and you only need to run the command as shown previusly, but if it’s your first commit run the following
git push --set-upstream origin branch_name_here
or
git push -u origin branch_name_here
Git branch
git branch branch_name_here
Creating a branch is the ideal thing to do when you are working on a project and don’t want to push changes to master utill the features are ready.
The command above will create a branch locally but now you have to push it to your remote repository, and for that you need the following command.
git push -u origin branch_name_here
Git checkout
git checkout branch_name_here
This command is used to switch in between branches, just make sure you’ve got no uncommited changes.
You can also create a new branch by running the command bellow.
git checkout -b branch_name_here
Git Merge
This command is ussually used to push/merge a change you’ve been working on in a different branch or to pull the latest changes from master into your branch.
1.- the first step is to go to the branch you want to merge the changes to, so if you want to merge your branch to master, you have to go to master first.
git checkout master
git checkout branch_name_here
Then I would recommend pulling changes so your main branch is up to date locally.
git pull
Then you can merge your branch into master.
git merge your_branch_name_here
Tip: if you are going to merge changes to the master branch I recommend mergin master into your branch first so you have time to resolve mergin issues and make sure your code still works as intended.
Also, to avoid issues like that you can simply merge the master branch into yours frecuently so you are certain that there’s no other changes interfiring with yours.
Git revert
There’s gonna be situations where you overlooked something and you need to revert your changes so for this we are going to use the following command.
git revert commit_hash
As shown above you only have to type git revert and then the commit hash which you can see in the commit history.
After you run the command press the following keys Esc :wq Enter
Then you revert commit will be ready to push so just do a git push.
These are the most used commands I get to use almost on the daily basis, I hope this helped you.
If you are a self-taught developer you might be interested on “The Best Way to Learn Programming”.
What are your thoughts on this, leave your most used commands in the commets below.