Clone an existing repo Add . at the end to clone into the current directory
| git clone <REMOTE_REPO_URL>
git clone <REMOTE_REPO_URL> .
|
Show a list of all remote git repos (e.g. Heroku, GitHub and Bitbucket) | |
Add a remote repo Remote name is usually โorigin", but can be whatever you want
| git remote add <REMOTE_NAME> <REMOTE_LINK>
|
Initialise new local rep Default initial branch is now called main
| |
Change the name of the recently created branch | |
Fetch data from a remote repo Downloads any changes from the remote repository to the local remote branch copy e.g. origin/main git fetch will not change anything about your local state. It will not update your main branch or change anything about how your file system looks right now.
This is important to understand because a lot of developers think that running git fetch will make their local work reflect the state of the remote. It may download all the necessary data to do that, but it does not actually change any of your local files. Think of running git fetch as a download step.
| git fetch
git fetch <REMOTE_NAME> <REF>
git fetch <REMOTE_NAME> <SOURCE_REF>:<DESTINATION_REF>
<EMPTY SOURCE_REF CREATES BRANCH ON LOCAL>
git fetch <REMOTE_NAME> :<DESTINATION_REF>
|
Working with remote repos Once you have new commits available locally (using git fetch ), you can incorporate them as if they were just normal commits on other branches. This means you could execute commands like: git cherry-pick origin/main
| git checkout <REMOTE_NAME>/<BRANCH_NAME>
git checkout origin/main
|
Fetch and merge The same as doing these two commands sequentially: git pull is essentially shorthand for a git fetch followed by a merge of whatever branch was just fetched
--rebase is shorthand for fetch then rebase
| git pull
git pull --rebase
git pull <REMOTE_NAME> <REF>
git pull <REMOTE_NAME> <SOURCE_REF>:<DESTINATION_REF>
|
Pushing changes to a remote Check default behaviour in git settings push.default upstream is a standard default option but look at other types
-u flag adds a tracking reference to the upstream server you are pushing to
What is important here is that this lets you do a git pull without supplying any more arguments. For example, once you do a git push -u origin main , you can later call git pull and git will know that you actually meant git pull origin main
<SOURCE_REF>:<DESTINATION_REF> can be used when the source reference is different from the destination reference
This could be if you wanted to push all the commits before your current commit in a feature branch straight to main If the <DESTINATION_REF> doesn't exits on the remote, then git will try to create a new branch with that name
| git push -u <REMOTE_NAME> <BRANCH_NAME>
git push <REMOTE_NAME> <SOURCE_REF>:<DESTINATION_REF>
<WHEN LOCAL IS BEHIND REMOTE & PUSH FAILS> git fetch
git rebase origin/maingit push -u <REMOTE_NAME> <BRANCH_NAME>
<EMPTY SOURCE_REF DELETES BRANCH ON REMOTE>
git push <REMOTE_NAME> :<DESTINATION_REF>
|
Stash changes when you want to switch to a new branch but don't want to commit the current changes Add drop to delete the last stash if you don't want it anymore Add apply to restore the previously stashed changes Repeat the command for each stash
| git stash
git stash drop
git stash apply
|
Templates If a repo is based on a template, you can pull changes from the template Then merge the changes with the current repo
| git remote add template [URL of the template repo]
git fetch --all
git merge --allow-unrelated-histories template/[branch to merge]
|