Skip to content

Latest commit

 

History

History
executable file
·
122 lines (114 loc) · 8.46 KB

git-reference-card

File metadata and controls

executable file
·
122 lines (114 loc) · 8.46 KB

-*- mode: org; -*-

Config

git config –global user.name “FirstName LastName” git config –global user.email “[email protected]” git config –global color.ui “auto” #Fancy colors git config –global rerere.enabled 1 #Always enable git-rerere git config –global branch.autosetuprebase always #Always pull with the rebase switch git config –list #List your current git configuration

Adding a remote to git repo

git init git add . git commit -m git remote add origin [email protected]:vedang/reponame.git

Branching

git checkout -b <branchname> <commit> #The default commit is HEAD. git checkout -d <branchname> #delete branch git checkout -m <branchname> <newbranchname> #Rename a branch git merge <branchname> #Merge branch <branchname> with current branch git push <remotename> <localbranchname>:<remotebranchname> #Push a local branch to remote git push <remotename> :<branchname> #Delete branch on remote. Literally, push nothing into <branchname> — git symbolic-ref HEAD refs/heads/newbranch #Create an empty new branch rm .git/index git clean -fdx — #Creating a separate repository from a range of commits restricted to a sub-directory git filter-branch –subdirectory-filter trunk HEAD #Create a new repository from a sub-directory named trunk #Git will also automatically remove commits that #did not affect the subdirectory #from - http://progit.org/book/ch6-4.html git remote add trunk /path/to/trunk git push trunk master #this will create a new repository at given path #with only the commits for a certain sub-directory #Getting back original master git checkout masters_original_HEAD git checkout -b tmp git merge -s ours master git checkout master git merge tmp git reset –soft HEAD^ #To remove the merge and revert the local repo back to #state before filter-branch

Merging

— git checkout dev #Basically, replace the current master entirely with the dev branch. git merge -s ours master #Useful when the code on master has become a hodge-podge mess git checkout master git merge dev —

git remote add -f projA /path/to/projA #Path should be a remote. git merge -s ours –no-commit projA/master git read-tree –prefix=subdirA/ -u projA/master git ci -m “merging projA into subdirA” #This makes your history look like this directory was in the repo all along

Tagging

git tag -u <key-id> <tagname> <commitid> #Create a tag signed using gpg git push <remotename> <tagname> #Push single tag to remote git push <remotename> <branchname> –tags #Push all tags on branch <branchname> to remote git config –global user.signingkey <keyid> #Save the gpg id so that you don’t have to mention it everytime git tag -s <tagname> <commitid> #After above command git tag -l #list tags git tag -a #Create a tag without signing it git describe –match v* #Find out the latest version tag you have created (say v0.93.1.2) git show v2.5:fs/locks.c #Show an old version of the file git rev-parse name #Get the commit-id from a tag name git tag -d 12345 #Deleting a tag locally git push origin :refs/tags/12345 #and from origin

Listing and Display

git diff –name-only foo bar #list all the files that have changed between two revisions git show –pretty=”format:” –name-only <commitid> #list files in a single commit. format string is very, very useful git log –pretty=oneline #single line logs git log –author=foo #some other fun things with log: –graph, –topo-order, –since git diff –name-status master..branch #show files changed between branches in a ‘git status’ way git log -p <filename> #show changes in a single file git reflog #List of commits that HEAD pointed to in the past. git shortlog master..branch #Generate changelog for a branch

Patches

git diff [commit-id-before] [commit-id-after] > my.patch #Create a patch between two ids git format-patch -3 #Create a ready-to-send patch with the last 3 commits git format-patch -k -s –stdout R1..R2 | git am -3 -k #Apply commits selectively from one branch to another. #-k keep subject -s signoff -3 use diff3 in case of conflict git format-patch master –stdout > diff-with-master.patch #Create a patch of the commits present in this branch but not in master git apply –stat diff-with-master.patch #Check the stats of the patch git apply –check diff-with-master.patch #Check if the patch will apply cleanly git am -3 –signoff < diff-with-master.patch #Apply the patch to the master branch

Submodules

git submodule add path_to_git_repo local_dir #Adding another repository as a submodule git submodule init #Initialize the submodules in a cloned repository git submodule update #Pull in the code from submodules in a cloned repository #Git submodule update keeps the submodule in headless state. #When you want to bring the submodule up-to-date, remember to #checkout to a branch first.

Adding & Committing

git add -p #interactively stage changes to file git cherry-pick <commit-id> #selectively apply a commit from one branch to another git commit –amend #if you forgot to add a file to your latest commit, #you can use this command to amend the commit git commit –amend -C HEAD #this will reuse the commit message used in the latest commit, #so that you don’t have to retype the commit message

Debugging and Recovery

git bisect start #To find a regression that happened between v2.6.18 and master git bisect good v2.6.18 git bisect bad master

git fsck –lost-found –no-reflog #Show me a list of dangling commits and blobs git reflog gitk v1.5.0.. <filenames/directorynames> #Show me all commits that touched a file / multiple files / #files in a given directory / files in multiple directories #since version v1.5.0 gitk –since=”2 weeks ago” – <filename> #show me all changes to file in the in the last 2 weeks gitk –max-count=100 –all – <filename> #Show me the last 100 changes to the file across all branches

gitk –all $( git fsck –no-reflog | awk ’dangling commit {print $3}’ ) #gitk can take a selective list of commits and #show them to you, this allows us to see all the dangling #commits and select the ones we want to recover.

Misc

git apply –whitespace=fix #get rid of bloody trailing whitespace