Basic git commands that you need to know as a Software Engineer
Updated on: March 16, 2020 ·
6 mins read
Git is the open source distributed version control system that facilitates GitHub activities on your laptop or desktop. The commonly used Git command line instructions are:-
Create Repositories: Basic git initialization commands
Start a new repository or obtain from an existing URL$ git init [ project-name ]
1. Create Repositories: Basic git initialization commands
2. Make Changes
3. Group Changes
4. Synchronize Changes
5. Some advanced git commands
6. Stash commands
7. Reverting git commits
Creates a new local repository with the specified name
2. Make Changes
3. Group Changes
4. Synchronize Changes
5. Some advanced git commands
6. Stash commands
7. Reverting git commits
$ git clone [ url ]
Make Changes
Review edits and craft a commit transaction.$ git status
$ git diff
$ git add [ file ]
$ git add .
$ git commit -m "[descriptive message]"
`
To add all files and commit them together
```bash
$ git commit -am "[descriptive message]"
Group Changes
Name a series of commits and combine completed efforts.$ git branch
$ git branch [ branch-name ]
$ git checkout [ branch-name ]
$ git branch -d [ branch-name ]
Synchronize Changes
Register a repository bookmark and exchange version history.$ git fetch [ bookmark/ branch ]
bookmark/branch
$ git merge [bookmark /[branch]]
$ git push [alias [branch]]
$ git pull
Some advanced git commands
Now let’s talk about some of the advanced commands. So lately I have been working on some of the open source projects. Which gave me a lot of idea about the way the things happen in the open source industry. As much time you spend in this part, more will you learn about the things happening. From a long time, I was working on a single user project where all the development is done by the single user. So your code remains intact and no damage is done to your commits. But the case is totally different when it comes to the big projects. There are constant commits that are happening in each second. So by the time you are ready to push the code to a repository for merging you are behind the main repository by at least three to four commits. So the idea is to get the code from the main branch merge the commits into your code and then push the changes back into the main repository. Also in the case big repository you can’t give the excuses like that you are new to the GitHub. If you don’t evolve early, get ready to be ignored by the open-source industry.Stash commands
So whenever you want to commit a change that someone have done to the remote repository and you want to merge them to your local, put the changes into the stash by using these commands and then pull the latest remote.$ git stash
# read more about git stash by using the following command
$ git stash --help
# The following command will list the saved data in the stash
$ git stash list
$ git remote add upstream http://github.com/remote_link_to_the_repository
$ git fetch upstream/branch_name
$ git merge upstream
$ git pull --rebase origin [ branch_name ]
Once all the changes are done, you can reapply the top stash.
$ git stash pop
# Apply the stash and delete the stash from stash list
$ git stash apply
# Apply the stash but keep in the history
$ git stash apply stash@{0}
# Apply the particular stash, can be found using the list command
Reverting git commits
Reverting commits can be real tough. There are a lot of options from which you can choose. One of the way is to usegit reset hard <commit_hash>
but when I was trying to revert changes on production due to some bug, I was not able to do it this way.
This happens because in general most of the production branches in big companies are protected branches and git reset
changes the commit history which is not allowed.
Another idea is to create a revert commit. Here is the command for that.
git revert -m 1 <pr_commit_hash>
Please share your Feedback:
Did you enjoy reading or think it can be improved? Don’t forget to leave your thoughts in the comments section below! If you liked this article, please share it with your friends, and read a few more!