Creating Git local repository and connecting with remote repository:
Git init Git remote add origin <url> Git config --global user.email <anyemail>.com Git config --global user.name <anyname> Git push --set-upstream origin master
Cloning a repository:
git clone <repository_git_url>
Creating new remote branch:
git checkout -b <branch_name> git push origin <branch_name>
Delete a remote branch:
git push origin :<branch_name>
Delete a local Branch:
git branch -d branch_name
git branch -D branch_name
Note: -D deletes the branch even if the branch is not pushed yet ( its similar to –delete –force)
How to change the remote URL:
git remote set-url origin <url>
How to reset local changes:
git reset --hard origin/master
How to revert last commmit:
use below command to get commit id:
git log
revert:
git revert <commit hash>
How to checkout to different branch:
git checkout <branchname>
To push an old commit as final head
git checkout <commit-id> //now make changes and commit it git branch <new_branchname> //(create a new branch) git push -f origin <new_branchname>:master git checkout master git branch -d <new_branchname> // remove new temp branch
If merge conflic come , then just backup and delete the local repo and clone it again. CHeck whether you have the indended changes
How to test a pull request:
git remote add origin <url> git pull git fetch origin pull/<id>/head:branch-name git checkout branch-name
Eg:
To test the below pull request:
copy the ID : 1479
copy the branch-name: fix-incorrect-override

use the below command:
git remote add origin https://github.com/DefectDojo/django-DefectDojo.git git pull git fetch origin pull/1479/head:fix-incorrect-override git checkout fix-incorrect-override
Git ignore:
Sometimes you need to prevent git from uploading sensitive files to remote repository.
This could be done by creating a gitignore file that tells git not to track the sensitive files.
To create gitignore file , open git bash and enter below command
touch .gitignore //will create the gitignore file
If files are already tracked, we should first untrack it else the rules in gitignore won’t come to effect.
git rm --cached <filename>
Now manually add files and folders to the .gitignore file
So, if we do git add . , the files add in .gitignore won’t be tracked
Note:
.gitignore file will get checked in and the rule gets applicable to all the users of the project
If you want to create rules locally only for you, then use your favorite text editor to open the file called .git/info/exclude within the root of your Git repository, to create ignore rules that need not be checked in. Any rule you add here will not be checked in, and will only ignore files for your local repository.
If you want to create git ignore globally for all git projects in your system, use below steps:
# Declare the global .gitignore git config --global core.excludesfile ~/.gitignore_global # Create the .gitignore_global file touch .gitignore_global # Go into edit mode so you can add the unwanted file listing vim .gitignore_global
Renaming a remote branch:
# Rename branch locally #if you are in a different branch git branch -m old_branch new_branch OR #if already in the old branch git branch -m new_branch # Delete the old branch git push origin :old_branch # Push the new branch, set local branch to track the new remote git push --set-upstream origin new_branch