Here are some common git operations for which we often find ourselves searching.
Workflow
Checkout a remote branch ‘develop’ and keep it linked to the origin
It should report: “Branch develop set up to track remote branch develop from origin.”
List all branches (both local and remote)
List local branches and their corresponding upstream remote branches
Revert local changes to a file (prior to commit)
Revert all local changes to the current subtree (prior to commit)
Cute hack to revert all local changes (prior to commit)
Undo a commit
Undo multiple commits
Where 2 is the number of commits to undo.
Update to latest HEAD, preserving local changes and local commits on top
Push changes on main to origin
Delete untracked files and directories
Interactively stage patches from changed file(s)
Roll back the last commit’s changes to a particular file
Branches
Branch main to a new local branch “new_branch”
Push local branch to remote
Make the current local branch start tracking a corresponding remote branch
This is not necessary if you used git push -u as suggested above.
List the local branches that have already been merged to this one
Diff a file between two branches
Delete a branch both locally and remotely
Move a commit from bad_branch to good_branch
For more on branching, see Git topic branches.
Rename the current branch
Git + SVN
Clone an SVN repository to a local Git repository
Commit and push changes, even with local changes in the working copy
Update to latest trunk, preserving local changes and local commits on top
Searching
Recursively search for HelloWorld.file (and display the most recent commit modifying it)
Recursively search for all files containing the phrase ‘import HelloWorld’
Recursively search for all files in any topic branch containing the phrase ‘import HelloWorld’
History
Display a log with colored word diffs
Add -S to less to virtually wrap long lines.
Display a diff with colored words between a file in one commit and a file in another commit
Add -S to less to virtually wrap long lines.
Display all contributing authors of a project including their e-mail
Respects .mailmap.
Viewing the history for a single file
This history is algorithmically calculated and must be carefully preserved.
Simultaneous (within a single commit) significant changes + file renaming (including relocation) can prevent the algorithm from successfully tracing the file’s history, or cause it to begin tracing the wrong file.
Keeping code changes separate from renames should prevent this confusion, but it is good practice to check log --follow before pushing to a remote repository.
See commits in branch B not present in branch A
There are two main options. The first:
will display the different commits in full git log format. NB: the .. between commits is important to sure only the difference in commits is considered.
The second:
will display a simple list of the different commits, one per line, with commit message and hash.
Scripts
There are some Git-related scripts available in the scijava-scripts project.
List information about all remote branches including last author, commit date and unmerged commit count
Advanced and/or dangerous
Create a repository with g+w permissions
Or for a bare repository:
(Bare repositories are meant for a remote server repository that all your coworkers push into and pull/fetch from.)
Push all remote branches from one remote (e.g., “origin”) to another (e.g., “github”)
Another way to push all remote branches between remotes
Fully garbage collect and compact the repository (deletes all orphaned refs!)
Rewriting history
Split a subdirectory into a separate git repository
See these posts on Stack Overflow:
- Detach subdirectory into separate Git repository
- Detach subdirectory (that was renamed!) into a new repo
- Split large Git repository into many smaller ones
Throw away git-svn-id metadata
Combine the first two commits of a Git repository
See this post on Stack Overflow:
Change the author of a commit
Change the author of many commits
See this post on Stack Overflow:
Merge multiple repositories
See these posts on Stack Overflow:
Tutorials
Creating a shared remote repository
Creates a bare remote repository at ssh://server/home/you/remote.git that tracks your local repository in /home/you/local. Adopted from Tim Lucas.
Displaying a filtered set of commits
Assume you want to see commits in branch stephan, but only those that are not part of the history of branch saalfeld:
More realistically, if you want to see all the commits which are in a topic branch, but not yet merged into main:
If you want to see the changes which come from a topic branch which was merged in commit deadbeef, use this command line:
Explanation: deadbeef is a merge commit, so its first parent (deadbeef^, can also be written as deadbeef^1) was the current HEAD when the merge was performed, and the second parent (deadbeef^2) is the tip of the branch which was merged. The argument A..B is short form of ^A B, i.e. all commits reachable from B excluding those which are also reachable from A.