LogIn
I don't have account.

Git Command to push changes on remote repository ( Github)

DevSniper

138 Views

Working with Git and GitHub involves three fundamental steps when updating your remote repository :- staging changes, committing changes and pushing changes. Understanding these steps ensures your code is properly tracked, organized, and shared with your team. Let’s go through each step in detail.

Steps to push chanegs on remote repository ( Github)

1. Stage Changes

2. Commit Changes

3. Push Changes

1. Stage Changes

Before you can commit your changes, Git needs to know which changes you want to include in the next commit. This is done using the git add command.

Commands :

Stage specific files

git add <file1> <file2> ...

to stage all changes

git add .

Note :- git add does not affect the remote repository. It only stages changes in your local repository for the next commit.

2. Commit Changes

Once changes are staged, you need to commit them. Committing saves a snapshot of your staged changes to the local repository. Each commit should include a meaningful commit message that describes what the changes do.

Copy
git commit -m "your commit message"
Example
git commit -m "Added authentication feature"
  • -m allows you to provide a commit message directly in the command line.
  • You can also run git commit without -m, which opens a text editor to write a detailed message.

Tip : Always write descriptive commit messages. A good commit message helps you and your team understand the purpose of changes later.

3. Push Changes

After committing your changes locally, you need to push them to the remote repository (e.g. GitHub) so others can access them. The git push command uploads your commits to the remote repository.

Basic Command
Copy
git push

If your remote repository and branch are set as the default upstream, this is sufficient.

Specifying Remote and Branch
Copy
git push <remote> <branch>
Example
git push origin main
  • origin is the default name for your remote repository.
  • main is the branch to which you want to push your changes.

If it’s your first push on a new branch, you may need to set the upstream branch using:

Copy
git push -u origin <branch>

This sets the branch as the default upstream, allowing future git push commands without specifying remote and branch.

Example Workflow

# Staging all changes
git add .

# Commiting changes with commit message
git commit -m "adding auth feature"

# Pushing changes to the remote repository
git push origin main

Following this workflow ensures that your code changes are properly tracked, documented and shared with your team on GitHub.

FAQs

1. What is the difference between git add and git commit?

  • git add stages changes in your local repository, telling Git which changes you want to include in the next commit.
  • git commit saves a snapshot of the staged changes into the local repository with a descriptive message.

2. Do I need to stage files before committing?

Yes. Only staged files are included in a commit. If you try to commit without staging, Git will not include any changes unless you use the -a option

Copy
git commit -am "Commit message"

This automatically stages and commits modified tracked files, but not new untracked files.

3. What happens if I push without committing?

Git will refuse to push because there are no commits to upload. You must commit changes before pushing.

4. What is origin in "git push origin main"?

origin is the default name given to your remote repository when you clone it. You can rename it, but by convention, origin points to your GitHub repository.

5. What if my branch does not exist on the remote?

If the branch does not exist, you need to set it as the upstream branch on your first push

git push -u origin <branch-name>

Future pushes can be done with just

git push

6. Can I push only specific files?

No. Git pushes commits, not individual files. To push specific files, stage and commit only those files, then push the commit.

7. What’s the difference between `git push` and `git push -u origin main`?

  • `git push` pushes commits to the default upstream branch.
  • `git push -u origin main` sets main on `origin` as the default upstream branch for future pushes.

8. Can I undo a push?

Yes, but it should be done carefully, especially if others are working on the branch. Commands like git reset or git revert can help:

Copy
# Undo last commit locally and push
git reset --hard HEAD~1
git push origin main --force

Note :- For shared branches, avoid --force unless necessary.

9. Do I need to push after every commit?

Not necessarily. You can make multiple commits locally and push them together. However, frequent pushes help collaborators stay up-to-date.

10. What if I accidentally staged a file I didn’t want to commit?

You can unstage it before committing using:

Copy
git reset <file>
Example
git reset secret_config.txt

This removes secret_config.txt from the staging area but keeps the changes in your working directory.

11. Can I cancel a push after it’s done?

Yes, but it’s tricky and may affect others if the branch is shared. You can use:

git reset --hard HEAD~1   # Remove last commit locally
git push origin main --force  # Overwrite remote

Note : Use --force with caution on shared branches, it can overwrite others’ work.