GIT Commands

Add

1
2
3
4
git add .                              ~ add all changes
git add foo\bar\baz.md ~ add just one file

git reset ~ un-stage any files added

Branch

1
2
git branch                             ~ display the current local branches
git branch -d cool-new-branch ~ delete local branch (to delete on the server see PUSH)

Checkout

1
2
3
4
5
6
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

Clean

1
2
git clean -d --dry-run                 ~ Purge untracked files/dirs
git clean -d -f ~ -f is required if clean.requireForce is not set to false

Clone

1
git clone https://github.com/carlpaton/RedisAdministrator.git

Config

1
2
3
git config --list                                                                                                        ~ useful things include: user.name= user.email=
git config --global http.sslCAinfo "c:/data/certs/certname.crt" ~ update to self signed cert
git config --global http.sslCAInfo "C:\Program Files\Git\etc\pki\ca-trust\extracted\openssl\ca-bundle.trust.crt" ~ update to default cert

Personal access tokens

Generate Personal access tokens from GIT UI under Settings - Developer settings

1
git config --global credential.helper store                  ~ he “store” mode saves the credentials to a plain-text file on disk, and they never expire.

Then in a git repo make some changes and authenticate with the token instead of your password.

It will now be saved.

core.autocrlf

If you’re on a Windows machine, set it to true — this converts LF endings into CRLF when you check out code.

In Visual Studio Code you can add the extension render line endings to see these characters.

1
git config --global core.autocrlf true

Commit

Commit local changes after add.

1
2
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

1
2
3
4
# ... hack hack hack ...
git add .
git commit --amend
git push origin abc-123-feature-branch-name -f

Diff

View changes, press Q to quit.

1
git diff

Log

1
2
3
git log -5                                   ~ shows last 5 logs
git log --graph --decorate --oneline --all ~ shows all log nicely
git log --walk-reflogs cool-branch-name ~ show logs for your branch only (Kindof works)

Merge

No remotes (Trunk Based)

If you need to merge master into your dev branch and you are not using remotes

1
2
3
4
git checkout master
git pull origin master
git checkout my-sweet-branch
git merge master

Now go resolve any conflicts manually, these files are marked as modified but not commited from the command above.

1
git status

The simplest way to do this is with VS Code.
Once conflicts are resolved then add, commit and push.

With remotes (Gitflow)

If you need to update your fork origin with the latest changes from upstream

From your local fork

1
2
3
4
git checkout master
git fetch upstream
git merge upstream/master
git push origin master

Pull

1
2
3
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.

Push

1
2
3
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`

Rebase

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

Restore

Restore working tree files (changes or deletes), simliar to git clean -d -f and git checkout -- * above.

1
git restore .

Update commit history

Change the commit message of older commits.

1
git rebase -i HEAD~2               

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

Reset

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

1
git reset --soft HEAD@{1}             ~ Undo git commit amend

Reset 2

Revert to a previous commit and trash whats local

1
2
git reset --hard 88039ae                    ~ revert to 88039ae
git push origin cool-branch-name -f ~ you can then update the remote repository

Remotes

A remote in Git is a common repository that all team members use to exchange their changes. Generally the repository you forked from is called upstream and your copy in your namespace ie: carlpaton is the origin.

1
git remote -v                                                     ~ display remotes

If you forked from for example from https://github.com/bradvin/SOLID to your own repository https://github.com/carlpaton/SOLID then bradvin/SOLID is upstream

1
git remote add upstream https://github.com/bradvin/SOLID.git

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)

So before the change

1
2
3
4
5
6
git remote -v

origin https://github.com/carlpaton/SOLID (fetch)
origin https://github.com/carlpaton/SOLID (push)
upstream https://github.com/bradvin/SOLID (fetch)
upstream https://github.com/bradvin/SOLID (push)

Then run the command

1
git remote set-url origin https://github.com/bradvin/SOLID.git

So now its the same for origin and upstream which means I will then just branch from master in origin

1
2
3
4
origin        https://github.com/bradvin/SOLID (fetch)
origin https://github.com/bradvin/SOLID (push)
upstream https://github.com/bradvin/SOLID (fetch)
upstream https://github.com/bradvin/SOLID (push)

Status

Show local changes

1
git status

Stash

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

References