1. GIT SETUP ON LINUX WITH BITBUCKET
Step 1. Install git on your PC
→ Open Terminal (ctrl+alt+t)
→ $ sudo apt-get install git
Verify the installation was successful by typing which git at the command line.
→ $ which git
“/usr/bin/git”
Configure your username using the following command.
→ $ git config --global user.name “Git_USER_NAME”
Configure your email address using the following command.
→ $ git config --global user.email “xyz@atlassian.com"
CREATE BITBUCKET REPOSITORY
Step 2. Create a repository on Bit-bucket
To create a repository, follow these steps:
1. Log into Bit-bucket Cloud under your individual account on https://bitbucket.org
2. Click Repositories > Create repository or the Create new repository link.
3. Choose a repository Owner.
This only appears if you are creating under an account with membership in one or more
teams.
4. Enter a Name and Description for your repository.
5. Tick Private if you want to hide your repository from the general public, so that only
selected people can see it.
6. Select the Repository type.
7. Click Create repository.
Step 3. Repository setup
→ $ mkdir /path/to/your/project
→ $ cd /path/to/your/project
→ $ git init
→ $ git add —all
Note: check status of adding file “Green files means chosen for repository”
→ git status
→ $ git remote add origin https://user_name@bitbucket.org/yourRepos.git
This is repositories path
→ $git commit -m 'Initial commit with contributors’
→ $git push -u origin master
Step 4. Create Branch on Bit-bucket
2. Branch-> Create Branch
Step 5. Clone Repositories
→ $ git clone https://user_name@bitbucket.org/yourRepos.git(remote
address)
Step 6. Using Git to checkout a branch on the command line
→ $ cd gitProject
→ $ git fetch && git checkout yourBranchName (Most Imp to change
branch for fetch & checkout)
→ $git branch -a
* yourBranchName
remotes/origin/HEAD -> origin/master
remotes/origin/abc_dev
remotes/origin/development
remotes/origin/master
remotes/origin/ yourBranchName
(The green colour of text with asterisk before it, showing that your current branch on which
you are.)
Step 7. Changes staging for commit
→ git add <file>( make a .gitignore file and add file names which
you don't want to commit on remote, and add into your project.)
→ git status
green files means added for commit
→ $ git commit -m 'commit message'
→ $ git push origin branch_name(on which you want to push code)
Provide username & password
Step 8. Pull (Daily Basis before push)
→ $ git pull origin branch_name (from which you want to pull)
DIFFERENCE B/W CHECKOUT & CLONE
To sum it up, clone is for fetching repositories you don't have, checkout is for switching
between branches in a repository you already have.
→ $ git checkout branch_name(from where you want to check out)
3. Once you completed all the above now follow the steps below…
1.Open your project on any editor (for eg. VSCode).
2. Make some changes in your files.
3. On the left menu on VSCode you find an option for git (or shortcut->> ctrl+shift+G). now you
can see the files on which you changed the code, once you ensure the changes, you need to commit
the code, now open terminal ( press ctrl+alt+t).
i. $ cd /path/to/your/project
ii . $ git status
(this will show you modified file names with path)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: app/folderName/file1.js
modified: app/folderName/file2.js
if you added new file in project which you didn't push yet that will show like this Untracked files:
(use "git add < file>..." to include in what will be committed)
newFile.js
iii. $ git add <fileName> (on which changes done -->> git add app/projectFolder/xyz.js ).
check status after add new files
iv. git status (After Adding)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: app/projectFolder/xyz.js
Changes not staged for commit:
(use "git add < file>..." to update what will be committed)
(use "git checkout -- < file>..." to discard changes in working directory)
modified: app/projectFolder/abc.js
Untracked files:
(use "git add < file>..." to include in what will be committed)
newFolder/newFile.js
(you can add files for commit as above)
v. $ git commit -m 'changes in xyz page'
“[your_branch_name ab3caa3] changes in xyz page
1 file changed, 9 insertions(+), 9 deletions(-)”
(you can also check the status of you commit by entering command $ git status )
iv. you can see the logs by entering command
$ git log
(show all commits in the current branch’s history, press 'q' from exit log window)
you can see the difference by entering command
$ git diff
4. (this will show you the difference between files, diff of what is changed but not staged press 'q'
for exit )
vi. now push your code to remote.
Before push code you can set your bit bucket password into your git config file
run commands
$ gedit .git/config
(This will open your git config file in editor now you can set or change password from here.)
[remote “origin"]
url = https://user_name:password@bitbucket.org/daffodilsoftware/yourRepos.git
fetch = +refs/heads/*:refs/remotes/origin/*
(Note : Do not use '@' in your password it will give error while pushing)
$ git push
(Transmit local branch commits to the remote repository branch)
Message on terminal—>>
“Counting objects: 16, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 483 bytes | 0 bytes/s, done.
Total 5 (delta 4), reused 0 (delta 0)
remote:
remote: Create pull request for yourBranchName:
remote https://bitbucket.org/daffodilsoftware/yourRepos/pull-requests/new?
source=YourBranchName&t=1
remote:
To https://userName:Password@bitbucket.org/daffodilsoftware/yourRepos.git
commit Id YourBranchName - > YourBranchName”
vii. Now its time to merge the code to your master branch or the branch you want
$ git merge [alias]/[branch]
merge a remote branch into your current branch to bring it up to date .
viii. $ git pull
'fetch and merge any commits from the tracking remote branch'
5. Some other useful command
REWRITE HISTORY
Rewriting branches, updating commits and clearing history
$ git rebase [branch]
apply any commits of current branch ahead of specified one
$ git reset --hard [commit]
clear staging area, rewrite working tree from specified commit
$ git log branchB..branchA
show the commits on branchA that are not on branchB
git show
show any object in Git in human-readable format
git reset [file]
Unstage a file while retaining the changes in working directory.
Remove git initialization
$ rm -rf .git
Display current git configuration
$ cat .git/con g
$ git con g user.name “username”
$ git config user.email “email”