git - goatandsheep/goatandsheep.github.com GitHub Wiki
git clone https://github.com/angular/angular-phonecat.git
Light-cloning: Choose the size of your clone with --depth=<number of commits>
git clone --depth=5 https://github.com/angular/angular-phonecat.git
Clone to server with self-signed certificate: git config http.sslVerify "false"
git reset --hard
git clean -f -d
- Commit & Push / Clean working directory. It's better to branch before you start making changes. I'm not sure about how to do it if you want to push a local commit to a new branch.
- Update and switch to working directory to match branch:
git checkout <existing-branch>
- Commit & Push / Clean working directory. It's better to branch before you start making changes. I'm not sure about how to do it if you want to push a local commit to a new branch.
- Create and switch to new branch:
git checkout -b <branch_name>
- Push branch to server:
git push --set-upstream origin <branch_name>
- Merge your changes
- Push deletion to
remote
:git push origin -d <branch_name>
(git push origin --delete <branch_name>
) git branch -d <branch_name>
- Clean your local workspace
- Push forced deletion to
remote
:git push origin -D <branch_name>
(git push origin --delete --force <branch_name>
) git branch -D <branch_name>
When you have branches with squashed merges, prune doesn't always delete locally.
Powershell:
git branch -vv | Select-String -Pattern ": gone]" | % { $_.toString().Trim().Split(" ")[0]} | % {git branch -D $_}
A tip for merging is to use origin/branchB
instead of branchB
because be default it tries to merge the local copy of the branch, which may not have been updated. Alternatively, pull the most recent changes to branchB before merging.
git merge -X ours branchB
git merge -X theirs branchB
- Add the other repo as a remote:
git remote add <nickname for secondary remote, use a single word> <remote location>
, e.g. for merginghttps://github.com/bob/by
into your localhttps://github.com/rob/by
,git remote add bob https://github.com/bob/by
. git remote update
- Checkout into
<new-remote>/<branch>
and pull - Checkout into local
<branch>
-
git merge
/` - Fix conflicts and push!
Test merging a commit locally before committing to it
git cherry-pick <commit hash>
- See / fix merge conflicts
- Test
- To revert marge:
git reset --hard
- Else commit, push
If cherry-pick doesn't work, step 2
- Create hotfix branch from production code
- Manually re-create fix in the production code without applying the new dev version
When you try something but it doesn't quite work out, but you learn stuff on the way, it may be useful to keep that code.
- Commit code
- Revert commit:
git revert <commit hex>
- Commit message (I usually just leave it how it is).
:q
for the vim noobs. - Push
for d in *; do pushd $d && git pull && popd; done
If you've made some changes but need to go to a different branch briefly before you're ready to commit you can use git stash
Initial saving:
git stash
List saved stashes:
git stash list
Revert to first stash in list:
git stash apply
Reverting to saved stash:
git stash apply <number>
git stash
git checkout correct-branch
git stash pop
Easy, but relies on multiple histories of code.
- After changes are ready, merge current state of
master
into the feature branch:git pull
git merge origin master
- Resolve conflicts
- Push changes:
git commit -m "merges+conflicts"
git push
- Create pull request
This creates an additional commit for the merge itself.
A trickier strategy uses rebase
to make for cleaner progression because it ensures a linear code history. Rebase to change the course of time to make your current code changes happen after the current changes in master. It can be tricky to use when you have already pushed some of the changes to remote.
git checkout branch
git rebase master
git push -f origin branch
Or if you look at the history, you can get the commit hash and do:
git checkout <branch>
git rebase -i <hash>
git push -f origin branch
When you get to the interactive rebase screen, change the pick to squash for the commits you are trying to squash.