Git
Various useful commands
A brief Git cheatsheet. Only the commands I found more useful (or that I repeatedly forget the syntax :) )
git mv oldfile newfile= Move a file, or simply rename itgit reset --hard SHA1_HASH= Revert to the commit withSHA1_HASHand delete all subsequent commitsgit revert SHA1_HASH= Like above but write a new commit that restore the repository as it was onSHA1_HASHcommit. All the subsequent commits, that with--hardwould have been lost, are still in the log and can be quickly reverted back togit log > Changelog= Save the log as a simple changeloggit log file= Show all commits that modifiedfile. Usuallyfileis a full path but it is also possible to use wildcards, like**/*.jsgit log -Sstring -p= Show all commits that matchstringand show also their changes (-p)git log -p --grep "perl_regexp"= Show all commits whose message matchesperl_regexpgit merge somebranch= Mergesomebranchwith current branchgit checkout -b somebranch= Create a new branchsomebranchand copy the content of master in itgit checkout .= Cancel all changes not yet committed (but keep new files). Basically return the repository state to the last commitgit cherry-pick SHA1_HASH= Apply the changes in the commit corresponding toSHA1_HASH. Commit can be on another branch (and usually it is). For multiple commits, it’s simpler to merge and then rebasegit cherry-pick -X theirs SHA1_HASH= Apply the changes in the commit corresponding toSHA1_HASH. In case of conflicts, prefer the commit’s version to the current branch one. The reverse can be preferred with-X oursinsteadgit stash= Save changes in the stash and return the repository to a clean state. Later they can be recovered and applied withgit stash applyand shown withgit stash show. To have a list of all changes in the stash usegit stash listgit stash -p= Interactively select which changes and files to stashgit commit --amend= Modify the last commit messagegit commit --amend -a= Modify the last commit message and add also all new changes yet to be committed in itgit rebase -i HEAD~3= Permit to modify, delete or merge all the last 3 commits from the HEADgit blame file= Show all the editing done in file and who did itgit bundle create somefile HEAD= Save the whole repository insomefile. To then import it back, usegit pull somefilegit bundle create somefile HEAD ^COMMON_SHA1= As above but only fromCOMMON_SHA1onward. This is useful when there’s an older version of repository, offline, that needs to be just updated with the latest commitsgit format-patch START_COMMIT= Create a patch that includes all changes fromSTART_COMMITtill the HEAD. An email header is also generated, for quicker sendinggit format-patch START_COMMIT..END_COMMIT= As above but consider only the commits betweenSTART_COMMITandEND_COMMITgit diff START_COMMIT..END_COMMIT= Create a patch of commits betweenSTART_COMMITandEND_COMMIT. Unlikeformat-patch, it’s a plain patch ready to be used withpatchgit diff --unified=10= Show 10 lines before and after a change instead of the default 3git am < patch= Apply patch to the repositorygit archive --format=tar --prefix=project-1.2.3/ -o outputfile HEAD= Save a tarball (or a zip if format iszip) of the whole repository inoutputfile, with the given prefix, in the current directory, as is in HEADgit add -p= Allow to select which changes to include in the next commitgit clean -x= Remove untracked and explicitly ignored files. With alsondoes a dry run, without actually removing anythinggit reflog= Show an extended log, including various operations like merges and checkouts, along with commitsgit filter-branch --index-filter 'git update-index --remove file' master= Completely removefilefrom the history, from the branch master, in every commit it appears. It’s like it never existed. To repeat for every branch in which the file was presentgit pull --rebase= Pull from a remote branch but don’t generate a commit message for the mergegit show :/regexp= Show last commit message matchingregexpgit show <commitid>:filename= show the status offilenameatcommitidgit remote rename aaa bbb= Rename remote branchaaatobbbgit pull origin branch_remote:branch_local= Pull a remote branch in the local one, creating the latter if it didn’t already existgit merge --edit= Edit the commit message when doing a mergegit merge --squash branch= Merge from branch in the current one, squashing all commits messages from when it diverged. After this, usegit committo record a single commit of changesgit diff --word-diff= Highlights word changes, inline, instead of whole linesgit status -sb= A more concise output for statusgit checkout -t origin/feature= Creates and checks out the local “feature” branch that tracks the remote “origin/feature”git name-rev --name-only COMMIT_SHA1= Prints where theCOMMIT_SHA1is in the history, relative to a tag. It can output things likemaster~2(commit is 2 commits before latest in master branch) ortags/v2.3~6(commit is 6 tags before the one with tag 2.3)git branch -a --contains COMMIT_SHA1= Shows all branches, and also remotes with-a, that have the given commit in their historiesgit show :/fix= Shows the last commit that contains the word “fix”. Also supports regexp, like:/^Testgit log --diff-filter=D --summary= Show files, with the corresponding commits, that have been deletedgit show commit:file= Show the full file as it was at commitgit log -p --author=name= Show all commits whose author matchesname(it may be a partial name or a regexp)git log -p master ^origin/master= Show a diff of what is new inmastercompared toorigin/master. It is also possible to use multiple arguments, likegit log -p master ^origin/master ^upstream/mastergit update-index --assume-unchanged file= Assumefileis unchanged even if it’s not, excluding it from being committed. Re-enable the modifications then withgit update-index --no-assume-unchanged filegit ls-files= List all files tracked in the repogit push origin --delete branchname= Delete the remotebranchnamegit cherry -v --abbrev testing master= Show a list of commits present in master but not in testinggit clone --depth 1= Clone only 1 revision in a repo’s history. To save space, mostly, or to send a quick patch or if you are only interested in the latest state of the project. A repository thus created can’t be cloned, fetched or pushed from or into it.git show branch-name:filename.js= Showfilename.jsas it currently is inbranch-namegit cherry-pick SHA1_HASH..SHA1_HASH= Apply changes in after the first hash till, including, the second one. To also include changes from first hash useSHA1_HASH^..SHA1_HASHformatgit remote set-url branch-name url= Set url of remotebranch-nametourl. Adding--pushalso changes the remote url to push to for specified branchgit config --list --show-origin= list all configuration settings for Git, both global and local, and where each is set
Cherry picking a range of commits
- Create a new branch from the branch where are the commits you want to apply by specifying the last wanted commit:
git checkout -b newbranch SHA1_HASH_OF_COMMIT - Then rebase from the older wanted commit into master:
git rebase --onto master SHA1_HASH^
Github
Sync with origin (the remote repository) after a local fork:
- Create a local branch to be linked with the master of origin:
git checkout -b origin/master - Add the origin repository as a remote:
git remote add origin git://github.com/username/repo.git - Pull:
git pull origin remote - Checkout into local master:
git checkout master - Merge origin master in local master:
git merge origin/master