How can I see what I am about to push with Git

How can I see what I am about to push with Git

Git is a small yet very efficient version control tool. It helps developers keep track of the history of their code files by storing them in different versions on its own server repository, i.e., GitHub.

Sometimes, while pushing our changes to the repository, it is a good practice to review the changes made in the project. This practice helps us to push complete and correct changes to the repository.

In this blog, we will learn how we can see what we are going to push on git.

Table of Contents:

Steps to See What You’re About to Push with Git

Here is the step-by-step process to see what we are going to push to our Git repository:

1. Check the Status of Your Repository

Before committing the changes to our repository, we should check its status. This will let us know what changes have been made, such as added, deleted, modified files, etc.

Git command:

git status

This command will give us a summary of our current repository state, such as:

  • Unstaged changes
  • Staged changes (ready to be committed)
  • Untracked files
  • Current branch

2. Check What Branch You’re On

Ensure you’re on the correct branch before pushing by using git branch.

git branch

3. Review the Commit History

The next step is to review the commit history of the repository. We can see the local commits made to the repository, before pushing it to git to avoid wrong or incorrect push.

Git command:

git log origin/<branch>..<branch>

This command shows us all the commits that are in our local branch but not in the remote branch (origin/<branch>).

4. Compare Your Local Changes to Remote

Now, we will see how our local branch is different from the remote. We can use the git diff command to see the differences between the local and the latest commit on the remote branch.

Git command:

git diff origin/<branch>

This command compares our current working directory to the last commit on the remote.

5. Push to Remote

After going through the above steps, we are ready to push our code to git.

Git command:

git push origin <branch>

Why Should You Check Before Pushing?

We should check what we are going to push for the following reasons:

  • Uncommitted Changes: It might be possible that our repository has some changes, but not yet committed. So it’s a good practice to check if the changes are correct or not, before pushing them on Git.
  • Unpushed Commits: It might be possible that our repository has some unpushed commits, that are reflected in our local repository, but not in remote repositories.
  • Differences Between Local and Remote: it is important to check the differences between our local repository and remote repository to ensure that the required changes are pushed to the remote repository.

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

By reviewing the changes before pushing them to the remote repository, we can ensure that we are pushing the correct changes. By frequently checking the status and commit history of our Git repository, we can avoid incorrect git pushes. If you want to learn more about Git/Github, you may refer to our advanced DevOps course.

FAQs

1. How do I check which repo I am in git?

To check which repository we’re currently in, we can use the command:

  git remote -v  
2. How do I check my git status?

To see the current state of your repository (like which files have been changed, added, or deleted), we can use the command:

 git status
3. How to git push command?

To push our local changes (commits) to the remote repository, we use the git push command:

 git push origin
4. What is git commit?

It is a git command that saves our changes (added or modified files) to the local repository, making it a permanent part of our project’s history

About the Author

Senior Cloud Computing Associate

Rupinder is a distinguished Cloud Computing & DevOps associate with architect-level AWS, Azure, and GCP certifications. He has extensive experience in Cloud Architecture, Deployment and optimization, Cloud Security, and more. He advocates for knowledge sharing and in his free time trains and mentors working professionals who are interested in the Cloud & DevOps domain.

EPGC Cloud