Trufa Git Cheatsheet Save

This is a compilation of common git commands. It is to keep them all handy at one place.

Project README

#git Cheat Sheet

###always pull from the same git config branch.master.remote origin git config branch.master.merge refs/heads/master

###Start from existing code (-u is to set the upstream.)

git remote add origin [email protected]:trufa/test.git
git push -u origin master 

###Use kdiff3 as mergetool to solve conflicts

(paste this inside of your .git/config file)

[merge]
    tool = kdiff3

[mergetool "kdiff3"]
    path = C:/Program Files/KDiff3/kdiff3.exe
    keepBackup = false
    trustExitCode = false
    

###Four ways of undoing last n commits

Read explanation here!

You want to nuke commit your and never see it again (Careful! Permanent loss).

git reset --hard HEAD~n

You want to undo the commit but keep your changes

git reset HEAD~n

Undo your commit but leave your files and your index:

git reset --soft HEAD~n

If you only want to change the last commit message

git commit --amend -m "New commit message"

###Retrieve a specific version of a file

git show <commit-id>:<path>

###Basic branching (tutorial)

Checkout to a new branch

git checkout -b newBranchName

Go back and merge

git checkout master
git merge newBranchName

###Push the current branch and set the remote as upstream

git push --set-upstream origin branch

###Help for resolve merge conflicts

When at a merge conflict, find changes between the common ancestor and the file on our branch which was merged

git diff :1:<path> :2:<path>

Find changes between the common ancestor and the file on the branch we are merging into the current branch

git diff :1:<path> :3:<path>

###Revert uncommited file to last version

git checkout filename

###Set vimdiff as default difftool (source)

git config --global diff.tool vimdiff
git config --global difftool.prompt false
git config --global alias.d difftool

###Stash local changes to be able to pull without errors (source)

git stash save --keep-index

###Remove untracked files

Files are lost permanently. Use -n or --dry-run to preview the effected files.

####Remove only untracked files

git clean -f

####Remove untracked files & directories

git clean -f -d

###Check if there is anything to pull, bring remote up to date (source)

git remote update

###Know what you pulled (source)

git diff master master@{1}

###Checkout to a remote branch

git checkout -b abranch origin/abranch

###Move uncommitted changes to a new branch (source)

git checkout -b <new-branch>
git add <files>
git commit

###Move uncommitted changes to existing branch (source)

git stash
git checkout branch2
git stash pop

###Get commits by certain user (source)

git log --author=<pattern>

###Rename a file

git mv dir/oldName dir/newName

And commit the changes.

Note: the file needs to be added (git add file).

###Exclude file from diff (source)

Create a repository specific diff driver with this command

git config diff.nodiff.command /bin/true

or for all your repos with --global.

Assign the nodiff driver to those files you want ignored in your .git/info/attributes file.

irrelevant.php    diff=nodiff

###See what was changed by one specific commit

git show HashOfTheCommmit

###See history of one file (source)

gitk fileName

Or

git log -p fileName

###List changes between two branches

List changes files

git diff --name-status firstBranch secondBranch

List changed files and number of changes

git diff --stat --color firstBranch secondBranch

List changed commits in one branch but not another (in secondBranch not in firstBranch).

git log --oneline ^firstBranch secondBranch

###Tags (annotated, the kind you can push to another repo)

Make an annotated tag

git tag -a -m "Tag for 1.2.3.4 release" release-1.2.3.4

Push an annotate tag to another repo

git push origin release-1.2.3.4

or push all tags

git push --tags origin

Rename a tag in local and remote repo

git tag newTagName oldTagName
git tag -d oldTagName
git push origin :refs/tags/oldTagName
git push --tags

###Repo splunking

List files in the repo on abranch (use 'HEAD' for current branch)

git ls-tree --full-tree -r abranch | awk '{print $4}'

###Compare the commits from two branches (source)

Display all commits that are in A but not in B

git log B..A
Open Source Agenda is not affiliated with "Trufa Git Cheatsheet" Project. README Source: trufa/git-cheatsheet
Stars
80
Open Issues
19
Last Commit
10 years ago

Open Source Agenda Badge

Open Source Agenda Rating