== Search ==
<code>git grep</code> only works on the current content of your working copy. To search through all revisions, you can use something like
<source lang="bash">
git rev-list --all | ( while read revision; do git grep -F 'wgAWSCredentials' $revision; done; )
</source>
== Ignore File Mode ==
Sometimes you have to temporarily change file modes (or some script might alter your working directory). Anyway, to ignore file mode changes temporarily, you can just add <code>-c core.fileMode=false</code> to your command. E.g.:
<source lang="bash">
git -c core.fileMode=false status
</source>
You can also put this into a repo or global config, but you probably shouldn't.
If you need to change filemodes back to the way they were in the repo, you can do something like
<source lang="bash">
git diff --summary | grep --color 'mode change 100755 => 100644' | cut -d' ' -f7 | xargs chmod +x
git diff --summary | grep --color 'mode change 100644 => 100755' | cut -d' ' -f7 | xargs chmod -x
</source>
== Working on a remote terminal ==
Checkout a specific branch from origin, and track it.
What if you enter <code>git checkout -b REL1_29</code> when you *should have entered* <code>git checkout -b REL1_29 origin/REL1_29</code>? Just J<s>ust tell git that you meant to track the branch in origin: <code>git branch --set-upstream-to=origin/REL1_29 REL1_29</code></s> Set upstream doesn't do what's intended here! You should delete the branch that you created as a 'copy'; and then checkout the branch correctly. To checkout the branch correctly, just IGNORE the <code>-b</code> flag altogether. If you ''do'' use the <code>-b</code> flag, then you ''must'' use the long form that references the remote branch to track. If you just do a '<code>git checkout foo</code>' and there exists the same branch upstream at <code>origin/foo</code> then git will checkout a new branch set to track the remote.
== Tracing in Status ==
== What is this miscellaneous file, and how does it compare to what's in my repo? ==
Say you've got a config file lying around (untracked) in your local working tree. It's not in your current project branch, but you know it's an important file. How does it compare to what's in the '''freephile''' remote, '''es128''' branch version of the file?
<source lang="bash">
git diff freephile/es128:config/core/MezaLocalExtensions.yml config/core/MezaLocalExtensions.yml
</source>
== How has this file changed over time? ==
While you can use gitk on your desktop, if you're on a headless server, you can ask <code>git log</code> to show you the '''patch''' for each commit: <code>git log -p path/to/file</code>
== Compare a file between branches ==
Do you think a particular file has changed between two separate feature branches (ea based off master)?
<source lang="bash">
# git diff branch1..branch2 -- path/to/file
git diff es128-rebased..get-to-know-meza -- manual/commands.md
</source>
git log --stat
</source>
== Undo a commit ==
Sometimes a commit is just wrong or 'breaks the build' so to speak. If you really just want to '''undo''' a commit, then
<source lang="bash">
# view what changes were made in the last commit
git difftool HEAD~
# reset --soft will undo the last commit, but leave the changes locally as 'modified' files that you can re-work or commit.
git reset --soft HEAD~
# reset --hard will undo the last commit, and discard any changes that were made in the commit. Your work tree will be identical to the way it was after the prior commit.
git reset --hard HEAD~
</source>
Using <code>reset</code> you can even go backwards several commits if you want.
The entire internet tells you not to <code>reset</code> if you've already pushed. But if you're just pushing (from machine A: your desktop) to a remote (machine B: eg. GitHub) so that you can then pull those changes into another space (machine C: development/staging/production/other machine) you can push your changes with <code>--force</code>; and pull them from that other environment. It may be quicker and easier though to identify the SHA of the n-1 commit and then <code>reset</code> on machine C
<source lang="bash">
git log -n 4
# find the commit SHA, and use it on machine C
git reset --hard d89c004cfe4bd67838fb41c7a6644bb15feee5cc
</source>
Credit: there's a clear explanation at https://www.git-tower.com/learn/git/faq/undo-last-commit
== Git Branch ==
* list the branches you have locally <code>git branch --list</code>
* list the branches that are on remotes <code>git branch -r --list</code>
* checkout a remote branch, setting the local one to track <code>git checkout -t freephile/patch-1</code>
* git branch --merged (while on master) shows you branches which are fully contained in HEAD, and can be deleted.
== Comparing local to upstream ==
If you do a <code>git status</code> and it tells you that your (local) branch is behind the remote tracking branch, then maybe you want to look at what those changes are.
<pre>
# On branch master
# Your branch is behind 'freephile/master' by 6 commits, and can be fast-forwarded.
# (use "git pull" to update your local branch)
</pre>
Tell git to fetch the branch named 'master' from the remote named 'origin'. Git fetch will not affect the files in your working directory; it does not try to merge changes like <code>git pull</code> does.<br />
<code>git fetch origin master</code>
When the remote branch is fetched, it can be referenced locally via FETCH_HEAD. Tell git to diff the working directory files against the FETCHed branch's HEAD and report the results in summary format.<br />
<code>git diff --summary FETCH_HEAD</code>
If you want to see changes to a specific file, for example myfile.js, skip the --summary option and reference the file you want (or tree). Note: paths (or pathspecs) should be separated from command options with a double dash (<code>--</code>)<br />
<code>git diff FETCH_HEAD -- mydir/myfile.js</code>
== Comparing Branches ==
What's in this old branch, not in my new branch (ignoring merges)?
* git log oldbranch ^newbranch --no-merges
* git log master..feature (everything on the feature branch that is not in master)
* git log HEAD..freephile/master (everything in the remote 'freephile/master' that's not in HEAD locally)
* git log origin/master..HEAD (everything in the local branch that you would push)
* git log --left-right master...experiment (triple dot shows everything since the common ancestor) <ref>https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#Commit-Ranges</ref>
== Don't merge, Rebase! ==
Get familiar with how to rebase your work each day http://www.bitsnbites.eu/a-tidy-linear-git-history/
If you forget to pull before you commit some local changes, you might just be able to `git rebase` to pull and re-play your changes on top of the branch.
== Delete local and remote merged branches ==
<source lang="bash">
git branch --merged | egrep -v "(^\*|master|dev)" | xargs -I % echo 'git branch -d % ; git push --delete freephile % ;'
</source>
Using '''echo''' helps you look before you leap. '''%''' is the replacement string in <code>xargs</code>. Change '<code>echo</code>' to '<code>sh -c</code>' to execute. Does both local and remote prunes.
== Git Log ==
What's the commit history? The [https://git-scm.com/docs/git-log man page for git log] is so big, it's a forest. There are examples in the book for [https://git-scm.com/book/en/v2/Git-Basics-Viewing-the-Commit-History viewing commit history]. For a quick cheatsheet, try these.
== Tags ==
With git, you can just tag something with <code>git tag foo</code>. This produces a 'lightweight' tag <ref>[https://stackoverflow.com/questions/21031201/how-can-i-list-all-lightweight-tags how can I list all the lightweight tags]</ref>. Use "annotated tags" whenever you want to know '''when''' something was tagged and '''who''' did it. Pass an empty message if you really don't care or need extra annotation.
<source lang="bash">
== [https://stackoverflow.com/questions/12921125/git-merge-branch-of-another-remote Git merge branch of another remote] ==
When using git between a local repository and a single 'origin' remote, it's a simple process to work locally and push things back up to origin. But, what if you have a separate remote repository... perhaps on GitHub, or a collaborator who has similar sources but not using your origin (so disconnected, and perhaps not even linked ancestrally like a fork). How do you add that other remote to your project and then pull in the code "they" have on top of yours? Here's an example of how we started with a repo from github and added a repo that we were developing privately. (The reality is that we were developing a repo privately; created a sibling version of the code at github; and then wanted to re-incorporate the changes of the github repo back into our private repo.)
<source lang="bash">
== Unbloat ==
https://stackoverflow.com/questions/3797907/how-to-remove-unused-objects-from-a-git-repository
== Put your project on GitHub ==
Been hacking away on a project and now it's time to unveil it? Here are the quick and easy steps to get your local repo into your GitHub account.
<source lang="bash">
</source>
== Remember your password ==I know how to configure my user and email, but how do I tell git to remember my password for repos that I'm interacting with?The answer is credential helper
{{References}}
[[Category:Development]]
[[Category:Version Control]]