git is installed:
git add .
This will add all files in the current working directory to the Git index. Files that were already added won't be added again.
git commit -m "Summary of my changes"
git add, you still need to commit them. If you want to combine
git add and
git commit, you can use:
git commit -am "Summary of my changes"
git remote add origin https://github.com/username/repository.git
You only have to do this once. Then, you can publish your committed changes by running:
git push -u origin main
In subsequent pushed, you can shorten the command:
main branch, which is where your production code is hosted. But in software development, features are often worked on in parallel and iteratively. That's why it's recommended to create an isolated branch for your feature:
git checkout -b my-feature
my-feature and check it out. To switch back to
main, simply run:
git checkout main
And to switch back to feature, run:
git checkout my-feature
my-feature branch. The feature is done, and you want to take it to production. Here's how you can do it:
git status # on branch my-feature, working tree cleangit checkout main # switch to maingit merge my-feature # merge my-feature into maingit push origin main # push the my-feature merge to maingit branch -d my-feature # delete the my-feature branch
First, make sure that you are on a clean working tree:
git status # on branch my-feature, working tree clean
If it says "working tree clean", you can safely continue. If not, you need to commit your changes first, or stash them:
git commit -am "Finish my current step" # commit your changes# orgit stash # save your changes for later
git status again, until your working tree is clean. Then, pull the latest changes from the remote:
git pull # pull the changes of my co-workers
git stash before, you can now "drop your changes" on top of the freshly pulled changes:
git stash apply # add my changes on top of the pulled changesgit commit -am "Finish my current step" # commit your changesgit push # push your changes to the remote, so that others can pull it
Continue my work later
git stash # save your changes for latergit pull # pull the changes of my co-workersgit stash apply # add my changes on top of the pulled changes
Undo the last local commit
git commit -m "Something terribly misguided" # your mistakegit reset HEAD~ # undo the last commit# ... make correctionsgit add . # add your changes againgit commit -c ORIG_HEAD # commit again, keeping the original message