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 false
This option is useful if the file permissions are not important to us, for example when we are on Windows.
git config --list
cd existing-project/
git init
git clone https://github.com/user/repository.git
This 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 -v
git remote set-url origin http//github.com/repo.git
git remote add remote-name https://github.com/user/repo.git
git diff
Note that this does not track new files.
git diff --cached
git diff origin/master
Note 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_ID
git diff --name-only COMMIT1_ID COMMIT2_ID
git diff-tree --no-commit-id --name-only -r COMMIT_ID
or
git show --pretty="format:" --name-only COMMIT_ID
Source: http://stackoverflow.com/a/424142/1391963
git diff --cached origin/master
git show COMMIT_ID
git status
git 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 master
git log
git log -2
git log -p -2
git log --pretty=oneline
git revert dd61ab21
git push origin master
git reset 56e05fced
git reset --soft HEAD@{1}
git commit -m "Revert to 56e05fced"
git reset --hard
Source: http://stackoverflow.com/q/1895059/1391963
git reset --soft HEAD~1
git reset --hard HEAD~1
git reset --mixed HEAD~1
Or git reset HEAD~1
See also http://stackoverflow.com/q/927358/1391963
git reset origin/master
git fetch origin
git reset --hard origin/master
git branch
git branch -a
git diff > patch-issue-1.patch
git add newfile
git diff --staged > patch-issue-2.patch
git add newfile
git diff HEAD > patch-issue-2.patch
git format-patch COMMIT_ID
git format-patch HEAD~2
git format-patch origin/master
git format-patch --binary --full-index origin/master
git apply -v patch-name.patch
git am patch1.patch
git 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.3
git push origin 7.x-1.3
git checkout master
git branch new-branch-name
Here 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-commit
or using a symbolic reference (e.g. last commit):
git branch branchname HEAD~1
You can also use
git checkout -b branchname sha1-of-commit
Source: http://stackoverflow.com/a/2816728/1391963
git checkout new-branch-name
git cherry -v master
(master is the branch you want to compare)
git checkout master
git merge branch-name
Here we are merging all commits of branch-name to master.
git merge branch-name --no-commit --no-ff
git diff branch-name
git diff branch-name path/to/file
git branch -d new-branch-name
git push origin new-branch-name
git fetch origin
git rev-parse --show-toplevel
Source: http://stackoverflow.com/q/957928/1391963
git rm $(git ls-files --deleted)
Source: http://stackoverflow.com/a/5147119/1391963
git clean -f
git clean -f -d
git clean -n -f -d
Source: http://stackoverflow.com/q/61212/1391963
git ls-files --deleted -z | xargs -0 git rm
Source (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.txt
git 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; done
Use Ctrl+a Ctrl+d to detach the screen.
history | grep git
or
grep '^git' /root/.bash_history
git for-each-ref --sort=-committerdate refs/heads/ | head
Source: http://stackoverflow.com/q/5188320/1391963
cd ..
tar cJf project.tar.xz project/ --exclude-vcs
git 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