Skip to content

Latest commit

 

History

History
226 lines (158 loc) · 8.49 KB

File metadata and controls

226 lines (158 loc) · 8.49 KB

Git Hints

Guidelines for good git hygiene

  • Turn off unnecessary warnings git add . -u
  • Always push to the server when leaving a PC
  • Always fetch -a and git pull <current branch> before starting
  • Always use git log to make sure you are working on the latest code
  • Create a branch if you're not sure if you forgot to push work from another PC to the remote
  • Create a branch for every experiment
  • Commit a lot with solid descriptive names, so you can read the history
    • Commit every time you move from one change to another -For instance, if you are adding a class and you see an opportunity to rename another class, finish your original work (with frequent commits), commit, make the rename, commit..
  • If in doubt, create a branch
  • If bringing back files from older branches / commits do so in a new branch
  • Tidy up branches by deleting any failed experiments
  • Tidy up branches by merging little offshoots back to the branch you branched from and deleting them-when you are sure the code is clean
  • Never merge to the 'develop' branch without thorough testing
  • Never merge to master until you have deployable code
  • New branch, commit, commit, commit, repeat...
  • Did I mention, CREATE A NEW BRANCH and COMMIT A LOT! 😄

To stash or not

Not sure why you would do this as the stash does not contain a description of what is in it. Wouldn't you just want to create a new junk branch from the current state, checkout the branch you were on before creating the junk branch, muck about with the current working directory and index (staging area) as you see fit, and delete the junk branch (aka your system stash) when you don't need it. If you think all this branching could go wrong, you can always bring an old branch/commit to life after deleting it with reflog.
Always remembering-don't ever re-write history if that history was saved to a remote! Bring the old code back as a new branch instead of detaching the head as the detached commits will be garbage collected and your team will reprimand you-behind your back if not to your face!

Different ways to identify a commit

Use any of these where you see in this documentation

  • HEAD~x *where x is the number of commits you want behind the current commit
  • commit sha
  • Date range (<from> // <to>)
  • --after/before="yesterday",}
  • --after/befpre="ccyy-mm-dd"
  • <since>..<until>
  • --author="<author name>"
  • --grep="<message>"
  • tag

Normal Work Flow (opinionated)

git fetch -a
git status
git pull
git status
    <work like a banshee>
git status
git add .
git commit -m "commit message"
git status
git push

If you are using pull requests on a remote repo, remember to git pull <branch> the branch you updated in the pull request to keep your local code synced up!

Git while the gitting is good

My favourite logging technique

git log --pretty=format:"%h--%ad--%an--%B" --date=format:'%Y-%m-%d %H:%M' --all --since=2.weeks.ago

git config --global alias.nicelog 'log --pretty=format:"%h--%ad--%an--%B" --date=format:'%Y-%m-%d %H:%M' --all --since=2.weeks.ago

Branching out with branches

git push origin --delete <branchName> Delete remote branch-CAREFUL!.

git branch <branchname> <commit ID> To create a branch with an old commit

git remote prune origin Removes ghosts of deleted branches when listing branches

git branch -r List remote branches

Looking at history

git log --oneline --decorate

git log -all Show history across branches

git log -3 Show last 3 commits

git log -p Show diff of changes

git log --stat Shows number of insertions, deletions etc.

git shortlog Seems fairly pointless :)

git log <since>..<until> and all the variations I listed in the commot ID section

Wow! Awesome filtering!

git log -- <filename1>...<filenamex> Show history of chosen files

git log -S"<pattern>" Show history where files contain this pattern

git log -S"<pattern>" Show history where files contain this pattern

git log --pretty=format:"<options>" - %cn Commiter name - %h Abbreviated hash - %cd Commit date

Getting to know your files; working area, staging area, HEAD commit

git diff show differences between working directory and staging area

git diff --cached show differences between staging area and latest commit

git diff HEAD show differences between working directory and latest commit

git reset --hard <Commit ID> Current branch point, working directory and staging area become <Commit ID>.

git reset HEAD Unstage all files in staging area. Leave working directory untouched

Gitting when things went south

Find the issue

git diff <branchName>..remotes/origin/<branchName> Compare the entire fileset between local and remote

git diff --name-only <commit ID> <commit ID> Show filenames that changed between commits

Example: git diff --name-only HEAD5 HEAD6

gitk --all -- <filename> graphical interface to see old file versions

git show <commit ID>:path/to/file See a version from a previous commit

git rev-list -n 1 HEAD -- <filepath> _show the last commit containing a file- to show when it was deleted

git log --diff-filter=A --<filepath> _show commit when a file was added

git diff HEAD~10 HEAD Compare entire project between current and 10 commits ago

gitk [filename / pattern] _see history of file

Fix the issue

git checkout <branchName>@<commit ID> -- <path/to/file> Get an old version of a file back into the current working directory, so it can be added and committed after testing

Working with remotes flawlessly and easily

git push --set-upstream origin <branchName> Push a new branch to remote

git remote add origin \<URL of origin\> _attach your repo to an existing remote_

To create a new remote from a local repo git push --set-upstream https://gitlab.example.com/namespace/nonexistent-project.git master
git remote add origin https://gitlab.example.com/namespace/nonexistent-project.git

Example git push --set-upstream https://gitlab.com/WeeMan/newGitRepo.git master
git remote add origin https://gitlab.com/WeeMan/newGitRepo.git

Delete remote branch git push -d remote

Rewriting history

git reset --hard <commit ID / branch> Update repo, staging area and working directory

git reset --mixed [the default]] <commit ID / branch> Update repo and staging area only

git reset --soft <commit ID / branch> Update repo only. Not sure when you'd want to do this

If you want to rename a branch while pointed to any branch, do:

git branch -m <oldname> <newname>

If you want to rename the current branch, you can do:

git branch -m <newname>

Tools sometimes make life easier

posh git gives autocompletion and shows number of added, deleted, modified files on current branch in index and working tree [master +1, ~0 -0 | +0 ~5 -0 !]

cd ~/
git clone https://github.com/dahlbyk/posh-git

git commit --amend -m "\<new commit message\>

git commit --amend --no-edit _Change files from the previous commit without changinmg the message_

Remove files from git repo

List of files in repo

git ls-tree --full-tree -r --name-only HEAD git rm [filename / pattern] git status git add . git commit -m 'removed [filename/pattern]' git push