If you are a software engineer, you’re most likely have interacted and used a version control system such as Git or SVN. I have used both, but for the past 5 years or so I have been mostly using Git for work and personal projects. I think it provides a simpler and easier way to version controlling and there are a lot of resources online for it if you ever got stuck doing something.
In this post, I will outline the 10 most commands I used in terminal for Git. And while you can do most of these commands using apps like SourceTree or the GitHub app, or sometimes it comes built in in apps like Xcode (see photo above), I still believe that developers should remember how to do these commands in Terminal alone. The 10 commands I’m outlining in this post are the essentials ones that I use on daily basis.
1. Git Clone [repo url]
This command is used when you want to clone a remote repo to your local machine. For example, if we want to clone Alamofire from Github, we go to the repo on Github, click the green button “Code”, and then copy the repo url. Then in Terminal we type the following:
$ git clone https://github.com/Alamofire/Alamofire.git
2. Git Checkout
Git checkout can be used in two different ways. The first one is used to to checkout a local or remote branch.
$ git checkout BranchName
While the second one is used to create a new local branch.
$ git checkout -b BranchName
3. Git Fetch
Git fetch is used to fetch the contents of remote repo, that could include new branches, tags, or any updates to the repo. It is important to call the following before you try to checkout a new branch, pull, or pretty much any other major git commands.
$ git fetch
4. Git Branch
Calling git branch is used to get the list of all available branches local, and can also be used to create a new branch.
$ git branch
Creating new branch:
$ git branch BranchName
5. Git Add
After you finish your edits and wants to commit your files, you have to stage the files for the commit. You can call git add like the following with either the file name you changes or with all the files.
$ git add fileName.ext
or all files like
$ git add .
6. Git Commit
After you staged your files, you call this command to commit the staged files. You have to include a message with your commit indicating what did you do in those files.
$ git commit -m "This is my commit message"
7. Git Status
Before you push, or even commit, you can call git status to get the status of your local branch, including the branch name you’re on, all the files that you have changed, staged, or untracked.
$ git status
8. Git Push
This command is used to push new changes to your branch, and also can be used to push a branch to the remote repo. If your local branch is tracking a remote branch then calling git push alone will send the local files to the remote branch, just like this:
$ git push
But if you’re local branch hasn’t been pushed to the remote repo, and you do/don’t have any commits, then calling the following will ensure to push your local branch to the repo along with all the changes on it.
$ git push <remote> <branch>
9. Git Merge
This command is used to merge branches together. For example, you branched off the master branch, and want to merge your changes back into it after you have made some changes. First you need to checkout the master branch, then call the merge command, such as:
$ git checkout master
$ git merge myBranchOffMaster
10. Git Rest
We all do mistakes while using git, if you want to reset your local branch and basically reset it back to how the remote branch is then calling the following command will do so. This will basically throw away any uncommitted changes you have and reset the branch to the latest pushed commit.
$ git reset --hard origin/HEAD
There are many other git commands or sub-commands that can be used, and even the commands I mentioned in this list has other sub-commands that be also used, but this list focuses on the daily ones that I use mostly during my work.
To learn more about Git commands there are many online resources for more complicated git problems, and here are some of them:
- First Aid Git: http://firstaidgit.io/
- Cheat Sheets by Github: https://training.github.com/downloads/github-git-cheat-sheet.pdf and https://education.github.com/git-cheat-sheet-education.pdf
- Cheat Sheet by Atlassian: https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet
I hope you found this post helpful. If you have any questions about it please feel free to leave a comment below.