18 git commands you should know

18 git commands you should know

Open source collaboration brings amazing ways of contributing to a diverse range of projects from the comfort of your space, through the internet on code collaboration platforms like github, gerrit, and gitlab using a version control system named "Git".

Welcome to this article, where I will show you 18 Git commands used regularly when contributing to open source projects.

Here is what we will discuss;

What is Git

According to Wikipedia, Git is software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Simply put, Git is the tool for accessing projects, tracking changes, deleting, and updating changes to a project residing in a remote computer which is accessed through the internet.

18 Git Commands

  1. Git clone: downloads a copy of a project to your local computer.
  2. Git init: sets your local project to be ready for operations with git.
  3. Git branch: creates, lists, renames and deletes branches.

    • git branch : create a new branch. This does not switch to the new branch automatically.
    • git branch --list: list all branches this works the same way as git branch, delete, or rename a branch.
    • git branch -d : delete a specific branch. This prevents you from deleting a branch with unmerged changes, if you want to delete the branch anyway, use git branch -D, to forcefully delete the branch.
    • git branch -m : rename a branch
  4. Git checkout: moves from one to another existing branch.

  5. Git status: checks and display details of the current state of your local repository.
  6. Git add: includes file changes to the local repository.
  7. Git commit: saves included file changes for publishing to the remote repository.

    • Git commit -m"": attaches the commit message to all files saved for publishing.
    • Git commit -a: launches the terminal where the commit message is written.After entering the commit message, press ctrl+x then Enter to save the commit message. commita.png
    • Git commit --amend: launches the terminal and allows you to edit your previous commit message and updates the commit message across all affected files to be published.
    • Git commit --amend --no-edit: automatically applies previous commit message to the current commit made.
  8. Git push: publishes your committed changes from your local repository to the remote repository.

  9. Git pull: downloads content from the remote repository to your local repository.
  10. Git revert: creates a new commit with rolled back changes of the old commit without deleting the old commit.
  11. Git review: commonly used on gerrit, git review does the following:

    • it looks up which branch to push to in the .git review file. If it can't find this information, it pushes to master.
    • it figures out what "topic" to put on the revision (you can set the topic manually with -t).
    • if you're resubmitting a downloaded change, it will reuse the tag of the original change.
    • if your commit summary contains a bug number like bug T12345, the tag will be bug/12345 else, the tag will be the name of your local branch.
    • it rebases your change against the HEAD of the branch you're pushing to (you can use git review -R to skip this).
    • if you are submitting more than one change at once, or submitting a change that's based on another unmerged change, it will ask you whether you really meant to do that (use -y to skip this).
    • finally, it pushes the change for review.
  12. Git merge: is used to integrate changes from another branch into your branch.

  13. Git fetch: only downloads new data from a remote repository but doesn't integrate this new data into your working files.
  14. Git remote: manages connections to other remote repositories tracked on your local repository.
  15. Git reset: used to undo uncommitted changes.
  16. Git rm: removes tracked file(s) from a Git repository.
  17. Git log: shows all commits in your local repository. log.png
  18. Git show: displays more details on commits made in your local repository. show.png

Resources

Thanks for reading. Hope you found this useful. Feel free to share this article with your network, you can also signup to my newsletter to get updates of new articles published on my blog.

Let's connect on Twitter