.. _l-cheatsheet-git: Cheat Sheet on Git ================== .. contents:: :local: Add a remote ++++++++++++ :: git remote add Example:: git remote add upstream_dmlc https://github.com/dmlc/xgboost.git Add a submodule +++++++++++++++ :: git submodule add -b https://.git Example:: git submodule add -b branchpy https://github.com/sdpython/machinelearning.git cscode/machinelearning Checkout a specific file from a remote ++++++++++++++++++++++++++++++++++++++ :: git checkout [-p|--patch] [] [--] ... Example:: git checkout origin/master -- include\xgboost\predictor.h Create a new local branch +++++++++++++++++++++++++ :: git checkout -b Example:: git checkout -b modif Create a new remote branch ++++++++++++++++++++++++++ :: git push -u Example:: git push -u upstream modif Push modification to remote repository ++++++++++++++++++++++++++++++++++++++ :: git push Example:: git push Remove a submodule ++++++++++++++++++ :: git rm The corresponding folder in ``.git/modules/`` must be removed too. Example:: git rm cscode/machinelearning -f Reset a branch ++++++++++++++ Reset to local branch :: git reset --hard Reset to a remote branch :: git reset --hard / Example: :: git reset --hard upstream/master Reset a submodule +++++++++++++++++ :: git submodule foreach git reset --hard The option ``--recursive`` does it for submodules included in submodules. Another to do it is to remove the submodule folder and to type ``git reset --hard`` which removes every modification made since the last pull. Update a branch +++++++++++++++ :: git pull Example:: git pull origin master You can also rebase the repository: :: git fetch git rebase / Example:: git fetch upstream git rebase upstream/master Update a submodule ++++++++++++++++++ :: git submodule update --remote --merge Example:: git submodule update --remote --merge Update a submodule to the remote branch +++++++++++++++++++++++++++++++++++++++ :: git submodule update --init Example:: git submodule update --init Option ``--recursive`` can be added to fetch submodules inside submodules. Fix submodules ++++++++++++++ :: git submodule sync Example:: git submodule sync Move multiple files +++++++++++++++++++ Assuming the reposity has no ongoing modification You can move files and then type right away: :: git add -A Rebase a branch to upsteam branch +++++++++++++++++++++++++++++++++ This instruction retains some part of the logs. :: git pull --rebase upstream master git push --force origin or :: git pull --rebase upstream main git push --force origin As it may seem that github renamed the default branch from *master* to *main (see `Renaming the default branch from master `_). Rebase a branch to upsteam branch and erase history +++++++++++++++++++++++++++++++++++++++++++++++++++ :: git rebase upstream/master git push -f origin master or :: git rebase upstream/main git push -f origin main If there are some commit of your own, they will be moved to the top of history. The following command deletes the last commit in the history. :: git reset --hard HEAD~1 The remote repository needs to be updated.