Useful .git commands
Here's a set of useful git commands:
Clone remote repository:
git clone git@gitlab.example.com:username/project.git .
Create bare repository:
git init --bare .git
Create a remote repository:
git remote add production ssh://user@server/var/www/example.com/.git
List the remote repositories:
git remote -v
Remove remote
git remote rm <remote-name>
Rename remote
git remote rename <old-name> <new-name>
Rename your local branch. If you are on the branch you want to rename:
git branch -m new-name
If you are on a different branch:
git branch -m old-name new-name
Delete the old-name remote branch and push the new-name local branch:
git push origin :old-name new-name
Set upstream for a branch
git push --set-upstream <origin> <remote-branch>
Create hook to checkout each time when you push to the remote bare repository.
The hook location should be /var/www/project/.git/hooks/post-receive and the file permissions should be set to 755.
The example includes update of a directory permissions but it could contain every bash command that you need.
git --work-tree=/var/www/project/ --git-dir=/var/www/project/.git checkout -f
chmod 766 -R /var/www/project/logs
Enable the .gitignore file when there are indexed files already.
WARNING: the ignored files will be deleted.
git rm -r --cached .
git add .
git commit -m ".gitignore is now enabled"
List only the file names of the different files:
git diff --name-only test/master
List the commits
git log
Revert specific commit
git revert 759572a06456cfbdc3e1a39443ab6767613549ec
Copy a repo in another one e.g. from Bitbucket to Amazon:
git remote add neworigin url-to-new-remote
git push neworigin --tags refs/remotes/origin/*:refs/heads/*
Change a remote origin
git remote set-url origin_name git@git.example.com:gituser/project.git
To stop tracking a file that is already tracked add it to .gitignore and execute:
git rm --cached <file>
Sort the git branches by commit date:
git branch --sort=-committerdate
Set of commands to reset the master (or any other branch) to the state in the remote repository:
git checkout "master" && git reset --hard remote-repo/master && git pull --rebase remote-repo master
In case of encoding issues with gitbash. ONE of the following settings in ~/.bashrc may help.
alias git='LANG=en_GB git'
alias git='LC_ALL=en_GB git'
How to revert multiple git commits already pushed
https://stackoverflow.com/questions/10780228/how-can-i-revert-multiple-git-commits-already-pushed-to-a-published-repository
Delete branch locally:
git branch -d localBranchName
Delete branch remotely:
git push origin --delete remoteBranchName
Search branch by name:
git branch --list <pattern>
Push only one commit:
git push <remote name> <commit hash>:<remote branch name>
Example:
git push origin 2dc2b7e393e6b712ef103eaac81050b9693395a4:master
It looks like the Pull Requests in Github doesn't keep track of changes to the target branch.
This could show some files that are not a part of the PR as changed.
Here's one
solution:
Suppose that you want to merge into master from feature-01:
git fetch origin
git checkout feature-01
git rebase origin/master
git push --force-with-lease
Find all branches that contain tag:
git branch -a --contains <tag>
Find origin branch of a tag:
git log -1 --format='%D' <tag>
Check git stash changes without pop:
git stash show -p stash@{n}