git checkout -b cool-new-branch ~ create new branch and switch to it git checkout cool-branch-name ~ Switch to another branch git checkout -- * ~ Clear tracked modified files not committed git checkout -- foo/bar/file.cs ~ Clear tracked modified `file.cs` not committed git checkout [revision] ~ Fall back to revision (run 'git log' to see commits) ~ run `git switch -` to revert the head detachment
git commit -m "commit message" git commit -a ~ automatically stage files that have been modified and deleted (excludes new files)
If you are pushing to your own branch and made a silly mistake such a typo, this will create noise in the commits to fix, to get around this you can amend
git pull ~ Pull anything new from remote server (for origin) git pull upstream master ~ Pull anything new from the remote name (upstream) on branch master git pull upstream master --rebase ~ Rebase the current branch on top of the upstream branch after fetching.
git push ~ Push staged and committed to remote (for origin) git push -d origin cool-branch-name ~ Push and delete the branch git push origin master ~ Push changes to origin, you will do this if you have run `git pull upstream master`
Reapply commits on top of another base tip, normally master :D
1 2 3 4 5
git checkout master ~ switch to master branch git pull upstream master --rebase ~ rebase your copy of master with any changes in `upstream` git checkout cool-branch-name ~ switch to feature branch git rebase master ~ rebase your feature branch with master git push origin cool-branch-name -f ~ when pushing you may need to force push if you are now changing history on pushed branches
If there are conflicts
1 2 3 4
git status ~ to see which files are unmerged # ... resolve conflict git add <conflicted_files> ~ mark files as resolved, you could also `rm` if you dont want the file git rebase --continue
If this are totally sideways run git rebase --abort
Displays the last 2 commit message in your configured editor, change pick for the line you wish to update to be reword, save and close.
1 2
pick 0e0e0e0 last commit pick 0e0e0e1 first commit
That commit message will then open with the option for you to change it from first commit to sweet first commit. You then need to force the changes to the remote repository.
1
git push --force
Rm
If you have a file that is tracked by source control and you need it removed.
1 2 3 4
git rm 'foo/.vs/foo/v16/.suo' ~ after doing this update .gitignore to include *.suo git add . git commit -m "remove .suo tracking" git push
You can jump any amount of heads below, example HEAD~2 will jump back 2.
1 2 3
git reset HEAD~1 --mixed ~ Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. git reset head --hard ~ clear the local changes that were reset above (that are now un-commited) git push origin cool-branch-name -f ~ forces the changes you did with the above resets to origin
This was useful when I had run git commit --amend on the wrong head
You can then pull from this upstream source and push the changes to your own fork. (See push and pull above)
If you want to change your remote url you can use git remote set-url. I needed to do this when a code base was changed from gitflow (so with a fork) to trunk based (so no form)
Lets you stash local changes, keep them local and pop them back if you want them again
1 2 3 4 5 6 7
git stash ~ stash anything that is not committed git stash list ~ list stack-order of stashed file changes git stash apply 0 ~ 0,1,2 ect (on index) ~ Then just discard if you want to use another stash
git stash pop ~ pop stashed items back (at index 0) git stash drop ~ discard the changes from top of stash stack