git <command> --dry-run
, e.g., git commit -a -m "Add fature 1" --dry-run
.gitignore
file
*.exe
... all *.exe files
dir1/
... directory dir1
'git config merge.conflictstyle diff3'
git ls-files
#This shows the tracked files
git show HEAD:<filename>
git show master:<filename>
... in branch master
git show origin/master:<filename>
... in remote repo origin, branch master
git ls-tree --name-only -r HEAD
git log -2
... last two commits
git log --stat
... which files changes and the number of changes in them
git log --name-status
... show which files were (A)dded, (M)odified
git log --pretty=oneline
, pretty=oneline / short / full / fuller
git log --pretty=oneline --stat -- "*.csv" ".txt"
... shows commit message and which csv and txt files changed
git log --since=2.weeks
... since last 2 weeks
Good combos:
git log -5 --pretty=oneline --name-status
git log -5 --pretty=oneline --stat
- Create and go to your project folder
git init
git remote add origin https://github.com/cubicula/git101.git
# setup remote repo
git pull origin master
# ???
to-do: What about git clone
?
git push -u origin master
git rm <file>
git commit
But this will delete the file from working directory too!
To remove a file from repo and index and leave it in the working directory, do
git rm --cached <file>
git rm '*A.txt
!!! works on subdirs too !!!
git mv <filename1> <filename2> # also stages it
- go to the branch into which you want to merge
git merge <branch name to marge from>
git checkout --conflict=diff3 file.txt
... to see code how it was before changes in branches about to be merges
Notice it doesn't delete the commits of that branch, just the pointer.
git diff --no-index -- <file1> <file2>
To use a custom diff viewer, use git difftool ...
instead of git diff ...
(tentative)
git diff
git diff --name-only
git diff <filename>
git diff <local branch> <remote>/<remote branch>
git diff master origin/master
git diff
git diff --cached
git diff HEAD
Explained here: http://365git.tumblr.com/post/3464927214/getting-a-diff-between-the-working-tree-and-other
git <branch1> <branch2> <filename>
Ex.: git diff master dev file1.txt
git reset -- <filename>
git rm --cached <file>
Example git reset octofamily/octodog.txt
git checkout HEAD -- <file>
... this will copy to index as well as working directory
! This will only copy from index to working directory: 'git checkout -- ', so previous adds to index will get copied back to working dir. and you will not get the original.
From https://stackoverflow.com/questions/10228760/fix-a-git-detached-head: "Next time you have changed a file and want to restore it to the state it is in the index, don't delete the file first, just do"
git checkout -- path/to/foo
"This will restore the file foo to the state it is in the index."
# on branch master
git reset HEAD^
What this does:
- moves master pointer to one commit back
- moves HEAD to point there too(or it just keeps pointing at master, I don't know)
- Copies files from ref to index, but not to working directory Tentative: To copy to working directory, do now either `git reset ... --hard' or 'git checkout HEAD'
(Also - git amend
)
(Also - git revert
)
git reset <filename>
Example git reset octofamily/octodog.txt
git commit -a -m "<commit message>"
to-do: Is it really all files? Or just tracked ones? In that case untracked files would not get added with git commit -a
git add <file>
... this is needed
git commit --amend -m "<commit message>"
Idea - run diff before and after each step to verify it's doing the expected thing
git diff HEAD
... is working area different from repo? Run this at the end of the sequence to see nothing is returned and they are in sync.
Will this show differences for all files or just the ones that are tracked?
git status
git diff
... between working and staging, this is to see whether staging has the stuff from working area that we want to commit
git add <file_to_track>
git diff
... must show nothing, so we know staging has the stuff we want to commit
!! Careful - if a file has not been added to the staging area, diff will sho nothing, giving the impression working and staging are in sync even though there's a whole file missing
git diff --staged
... between staging and repo branch
git commit -m "<message>"
git diff --staged
... must show nothing, so we know staging has been committed to repo
git diff HEAD
... must show nothing, so we know working is in sync with repo
git diff origin/master
... between working(??? or staging or local repo???) and remote repo, to see what hasn't been pushed to remote
git push origin master
git diff origin/master
... must show nothing, so we know remote is synced with local
Go back to the last checked-in version of a single file
git checkout HEAD -- <filename>
... rewrite file in the working directory
git checkout HEAD -- octocat.txt
(not sure what it does in the staging area)
From branch Bsource destination branch Bdest:
git checkout <Bsource> <file_to_copy>
This really only copies one file and doesn't delete the other files in the destination branch.
It doesn't switch branch!
# on current commit:
git branch <branch name>
, git branch cleanup
# on a different commit:
git branch <branch name> <commit>
, git branch bugFix HEAD^2^1
git checkout <branch name>
, git checkout cleanup
git branch -d <branch_name>
git checkout master
... must be on the branch which is to be movedgit reset <commit>
... e.g.,git reset HEAD^
or
(tentative)
git branch -f master <commit>
... doesn't require to be first on master
git diff origin/master
... changes between working area and remote repo.
Non-empty result means there are local changes missing from remote repo.
These changes could be already committed locally, so all that is needed is to push them.
Run git diff HEAD
to see if they need to be committed first.
git ls-remote
... shows references in remote repository
and also
git branch -r
... shows remote-tracking branches
git remote show origin
... more detailes about origin remote
git branch -vv
git checkout --track origin/serverfix
or
git checkout -b <branch> <remote>/<branch>
e.g.
git checkout --track -b simple-name origin/some-long-complex-name
git branch branch_name --set-upstream-to remote/branch_name
E.g.,
Push in case of different name
git push <remote> local-name:remote-name
git tag <tagname> <commit>
git describe <commit|branch>
... the closest past tag, how many hops away
git config list
... show Git config