Everyday git commands

December 14th, 2022 by Rijad Husic

Git is a popular version control system that allows developers to manage and track changes to their codebase. While there are many Git commands available, there are a few that are particularly useful for everyday use. These include:

git clone: This command is used to create a local copy of a remote repository. For example, if you want to clone a repository from GitHub, you can use the following command:

git clone https://github.com/user/repo.git

This will create a local copy of the repo repository in a directory named repo.

git status: This command allows you to see the current state of your local repository. For example, you can use the following command to see the status of your repository:

git status

This will display a list of modified, untracked, and staged files in your repository.

git add: This command is used to stage files for commit. For example, if you've made changes to a file named myfile.txt, you can stage it for commit using the following command:

git add myfile.txt

This will add the file to the staging area, where it can be committed.

git commit: This command is used to save your staged changes to the local repository. For example, you can commit your staged changes using the following command:

git commit -m "My commit message"

This will save your changes to the local repository, and attach the commit message "My commit message" to the commit.

git push: This command is used to upload your local commits to a remote repository. For example, if you want to push your local commits to the master branch of a remote repository named origin, you can use the following command:

git push origin master

This will upload your local commits to the master branch of the origin repository.

In addition to the core Git commands, there are a few other Git commands that are worth mentioning. These include:

git stash: This command is used to temporarily save changes that you've made to your working copy, but that you're not quite ready to commit. For example, if you want to stash your changes, you can use the following command:

git stash

This will save your changes to a temporary area, allowing you to switch to a different branch without committing your changes.

git pop: This command is used to restore stashed changes to your working copy. For example, if you want to restore the changes you stashed earlier, you can use the following command:

git pop

This will restore your stashed changes to your working copy, allowing you to continue working on them.

git rebase: This command is used to integrate changes from one branch into another. For example, if you want to rebase your feature branch onto the master branch, you can use the following command:

git rebase master feature

This will apply the changes from the master branch to the feature branch, allowing you to incorporate the latest changes from the main branch into your own work.

git merge: This command is used to combine changes from multiple branches into a single branch. For example, if you want to merge the feature1 and feature2 branches into the master branch, you can use the following command:

git merge feature1 feature2 master

This will combine the changes from the feature1 and feature2 branches into the master branch, allowing you to combine the work from multiple feature branches into the main branch.

Overall, Git is a powerful tool for managing and tracking changes to your codebase. By using the commands discussed above, you can effectively collaborate with others, keep a log of your changes, and maintain a clear and organized codebase.