How to push code to GitHub
Today we will show you how to push code to GitHub. If you want to publish your work on github then you have to push your code on remote repository. In other words, push code to the github is used to keep the same work on remote and local repositories.
how to push code to github using cmd, how to publish your code on github, Adding an existing project to GitHub using the command line, Pushing commits to a remote repository, Step-By-Step Guide To Push Your First Project On GitHub, GitHub For Beginners: Commit, Push And Go, git commit and push, how to upload files to github from windows.
Checkout more articles on Git
Steps to push code to GitHub
- Create repository
- Check the status of working tree
- Add changes in local repository
- Commit the changes
- Push changes in remote repository
1. Create repository
Before we start, you have to create a repository / clone of your existing repository from GitHub. If you don’t know how to clone a repository in GitHub then please refer to the link below.
How to clone Git repository2. Check the status of working tree
You have to execute the code below to check the status of the working tree.
1 | git status |
Use the code below to discard all the changes in the working directory.
1 | git restore . |
Use the code below to restore a specific file.
1 | git restore <file> |
Note: Above command git restore
will work before adding changes to the local repository for modified files. It won’t work for local untracked files.
3. Add changes in local repository
If you made any changes then first you have to add these changes in the local repository and stage them for the commit. Below command will help you to do it.
1 | git add . |
Use the code below to add an individual file.
1 | git add <file> |
Use the code below to add the whole folder.
1 | git add <folder>/* |
Let’s say if you added the code by mistake then you can unstage it by the use of the code below.
1 | git reset HEAD <file> |
Use the code below to unstage all files.
1 | git reset HEAD |
4. Commit the changes
Now it’s time to commit the changes and prepare it for the remote repository so we can push it.
1 | git commit -m "new changes" |
If you want to remove the commit then use the code below.
1 | git reset --soft HEAD~1 |
If you want to remove the commit and discard the changes then use the code below.
1 | git reset --hard HEAD~1 |
5. Push changes in remote repository
At last, execute the command below to push the changes in remote repository.
1 | git push origin master |
Conclusion
Let’s combine all code together in one flow to push code in GitHub.
1 2 3 4 | git status // To check the status of working tree git add . // To add the changes in local repository git commit -m "new changes" // To commit the changes and prepare it for remote repository git push origin master // To push the changes in remote repository |
That’s it for today.
Thank you for reading. Happy Coding!