-
To create local repository
# mkdir /gitlab # cd /gitlab # git init It will create .git directory. Where the .git directory is there that is local repository. # ls -
How to add the file to Staging area
# git add <file_name> -
If you want to move all files to staging area
# git add . -
To see the status of a file
# git status -
To move a particular file from Stating space area to local repository
# git commit -m "<commit_name>" <file_name>i) without email and username we can't commit the file/s so, we have to set email and username globally
git config --list
git config --global user.email "email-id" git config --global user.name "username"
git config --global --unset user.email "email-id"
-
If you want to commit all files to local repository
# git commit -m "<commit_name>" -
To see the commit messages?
# git log -
To see the content of the commit?
# git show <commit_number> # git log --oneline # git log --since=2018-01-21 # git log --until=2018-03-18 # git log --author="harishkolanu235" # git log --grep="index" # git log --oneline --author="harishkolanu235" # git log --oneline --name-only ----> Show only names of changed files. # git log --oneline --pretty=format:%h%x09%an%x09%ad%x09%s %h ---> commit_id %x09 ---> space %an ----> author name %ad -----> date and time %s ----> commit message -
To show the recent commit logs
# git log -5 -
To see the difference between 2 commits
# git diff <commit_id1>..<commit_id2> -
To list of all branch
# git branch Note: (*) represents your currently in that branch. -
How to get back a commit to staging area?
# git reset --soft HEAD^ -
How to get back a file from staging area to working area?
# git reset head <file_name> -
How to get back a commit to work area?
# git reset --mixed <previous commit id> Note: We can get back first commit also but need repository default files commit Id -
discard the latest commit perminately including commit files ?
# git reset --hard HEAD^ ---> it'll delete the latest commit and it's files permenently. Note: if you want to discard the latest 3 commits than use HEAD^^^ (or) HEAD~3or
# git rebase -i HEAD^ or # git rebase -i HEAD~3 -
When file have staging area or file have committed if file is deleted in local repository unfortunately how to get back that file to staging area?
# git checkout -- <file_name> -
To Create branch
# git branch < branch_name > -
Change the branch
# git checkout <branch_name> Note: when newly or first-time checkout to branch, while checkout the committed files must copied from another branch to newly checkout branch. Whenever checkout to another branch, Uncommitted files are copied to checkout branch evrytime. -
To create a branch while switched to that branch
# git checkout -b <branch_name> -
To see the difference between 2 branches
# git diff <branch1>..<branch2 > -
To delete the branch
# git branch -d <branch_name> Note: if you want to delete forcefully, we can use below command # git branch -D <branch_name> -
How do you push the files to master branch in remote repo?
# git push (you must be in master branch) -
How do you push files from local to particular branch in remote repo?
# git push origin <branch_name> (or) # git push --set-upstream <branch_name> -
How to push new branch and its data to remote repository?
# git push <github_repository_path> <branch_name> (or) # git push --set-upstream <branch_name> -
To Merge the branches
# git merge <branch1> <branch2> Note: two diff content of the files -
If you want to data move from branch1 to branch2
# git merge <branch1> -
How will you know in GIT if a branch has been already merged into master?
# git branch --merged // It lists the branches that have been merged into the current branch. # git branch --no-merged // It lists the branches that have not been merged. -
How to selectively pickup the commit's from child branch
# git cherry-pick <commit_id1> <commit_id2> Example: # git cherry-pick f29604a efcb77a -
To store the data into stash?
# git stash save "<message>" Note: the files must and should in the stage/index/cache area -
To see the stash list?
# git stash list -
To copy the data into branches?
# git stash apply <stash#> -
To move the data into branches?
# git stash pop <stash#> -
To delete the stash area?
# git stash drop <stash#> // delete the particular stash # git stash clear // delete the entire stash list -
How to give an access to a specific person to repository?
You can invite users to become collaborators to your personal repository. • Under your repository, click on Settings. • In the left sidebar, click Collaborators. • Under "Collaborators", start typing the collaborator's username. • Select the collaborator's username from the drop-down menu. • Click Add collaborator. • The user will receive an email inviting them to the repository. Once they accept your invitation, they will have collaborator access to your repository. -
How to Lock a branch? why we need to lock a branch?
• On GitHub, navigate to the main page of the repository. • Under your repository name, click on Settings. • In the left menu, click on Branches. • Select the branch you want to mark protected using the drop-down menu. • Select Protect this branch. • Click Save changes. -
How to delete Repository in GitHub?
• On GitHub, navigate to the main page of the repository. • Under your repository name, click Settings. • Scroll to the bottom of the page and you will find Delete this repository button • When you click on that button, another pop up will appear, here you need to type in the name of your repository name and click on the button bellow which says: I understand the consequences, delete the repository. -
To get the latest commit from all branches in remotely
# for branch in `git branch -r | grep -v HEAD`; do git log --format=%h%x09%an%x09%ad%x09%s%x09%cr $branch -1 \\t$branch;done # for branch in `git branch -r | grep -v HEAD`; do echo $branch; git log --format=%h%x09%an%x09%ad%x09%s%x09%cr $branch -1; echo -e "\n";done -
To update all local branches
# git pull --all