Here there are some examples of git commands that I use often.
Not all commands written here are git commands, but all of them are related to git. Please refer to the documentation for more details.
git config --global user.name "John Doe"
git config --global user.email "[email protected]"Use --global to set the configuration for all projects. If git config is used without --global and run inside a project directory, the settings are set for the specific project.
cd project/
git config core.filemode falseThis option is useful if the file permissions are not important to us, for example when we are on Windows.
git config --listcd existing-project/
git initgit clone https://github.com/user/repository.gitThis creates a new directory with the name of the repository.
git clone https://github.com/user/repository.git .Get help for a specific git command git help clone
cd repository/
git pull origin master
```bash
Where origin is the remote repository, and master the remote branch.
If you don't want to merge your changes, use git fetch.
####View remote urls
```bash
git remote -vgit remote set-url origin http//github.com/repo.gitgit remote add remote-name https://github.com/user/repo.gitgit diffNote that this does not track new files.
git diff --cachedgit diff origin/masterNote that origin/master is one local branch, a shorthand for refs/remotes/origin/master, which is the full name of the remote-tracking branch.
git diff COMMIT1_ID COMMIT2_IDgit diff --name-only COMMIT1_ID COMMIT2_IDgit diff-tree --no-commit-id --name-only -r COMMIT_IDor
git show --pretty="format:" --name-only COMMIT_IDSource: http://stackoverflow.com/a/424142/1391963
git diff --cached origin/mastergit show COMMIT_IDgit statusgit add changed_file.txt
git add folder-with-changed-files/
git commit -m "Commiting changes"git rm removeme.txt tmp/crap.txt
git mv file_oldname.txt file_newname.txt
git commit -m "deleting 2 files, renaming 1"git commit --amend -m "New commit message"git push origin mastergit loggit log -2git log -p -2git log --pretty=onelinegit revert dd61ab21
git push origin mastergit reset 56e05fcedgit reset --soft HEAD@{1}
git commit -m "Revert to 56e05fced"git reset --hardSource: http://stackoverflow.com/q/1895059/1391963
git reset --soft HEAD~1git reset --hard HEAD~1git reset --mixed HEAD~1Or git reset HEAD~1
See also http://stackoverflow.com/q/927358/1391963
git reset origin/mastergit fetch origin
git reset --hard origin/mastergit branchgit branch -agit diff > patch-issue-1.patchgit add newfile
git diff --staged > patch-issue-2.patchgit add newfile
git diff HEAD > patch-issue-2.patchgit format-patch COMMIT_IDgit format-patch HEAD~2git format-patch origin/mastergit format-patch --binary --full-index origin/mastergit apply -v patch-name.patchgit am patch1.patchgit add --patch file.txt(press 'y' for the chunks to add)
git commit -m 'first part of the file'(repeat if desired) Sources: https://stackoverflow.com/q/4948494/1391963, https://stackoverflow.com/q/1085162/1391963
git tag 7.x-1.3git push origin 7.x-1.3git checkout master
git branch new-branch-nameHere master is the starting point for the new branch. Note that with these 2 commands we don't move to the new branch, as we are still in master and we would need to run git checkout new-branch-name. The same can be achieved using one single command: git checkout -b new-branch-name
git branch branchname sha1-of-commitor using a symbolic reference (e.g. last commit):
git branch branchname HEAD~1You can also use
git checkout -b branchname sha1-of-commitSource: http://stackoverflow.com/a/2816728/1391963
git checkout new-branch-namegit cherry -v master(master is the branch you want to compare)
git checkout master
git merge branch-nameHere we are merging all commits of branch-name to master.
git merge branch-name --no-commit --no-ffgit diff branch-namegit diff branch-name path/to/filegit branch -d new-branch-namegit push origin new-branch-namegit fetch origingit rev-parse --show-toplevelSource: http://stackoverflow.com/q/957928/1391963
git rm $(git ls-files --deleted)Source: http://stackoverflow.com/a/5147119/1391963
git clean -fgit clean -f -dgit clean -n -f -dSource: http://stackoverflow.com/q/61212/1391963
git ls-files --deleted -z | xargs -0 git rmSource (and alternatives): https://stackoverflow.com/a/5147119/1391963
Short answer: Git does not do that. Long answer: See http://stackoverflow.com/a/10847242/1391963
git reset HEAD file.txtgit describe --tags `git rev-list --tags --max-count=1`Source: http://stackoverflow.com/q/1404796/1391963. See also git-describe.
for((i=1;i<=10000;i+=1)); do sleep 30 && git pull; doneUse Ctrl+a Ctrl+d to detach the screen.
history | grep gitor
grep '^git' /root/.bash_historygit for-each-ref --sort=-committerdate refs/heads/ | headSource: http://stackoverflow.com/q/5188320/1391963
cd ..
tar cJf project.tar.xz project/ --exclude-vcsgit diff --name-only | xargs tar -cf project.tar -T -grep -H -r "<<<" *
grep -H -r ">>>" *
grep -H -r '^=======$' *There's also git-grep.
patch -p1 < file.patch