Git setup and command
What is Git?
Git is a distributed version-control system for tracking changes in source code during software development.It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.
How to setup git?
- Generate SSH Key, Run below command and enter path and passphrase.
ssh-keygen -t rsa
- Initiate git and configure user
git init git config --global user.name "name" git config --global user.email "user@email.com"
- Get git configuration list
git config --list
- Update git configuration, In case you wish to update default editor
git config --global core.editor "/usr/bin/vim"
- View git configuration
cat ~/.gitconfig
- List .git directory contents
ls .git
HEAD - Refers to current branch we working on.
Config - Details about the developers user name and emaills.
hooks - Hooks are customization scripts used by various Git commands.
index - The current index file for the repository.
info - Additional information about the repository is recorded in this directory.
logs - Commits and changes logs.
refs - References are stored in subdirectories of this directory.
Frequently used git commands
- Download project from github
git clone <git repos> example. git clone https://github.com/rajnathsah/AutoResumeUpload
- Add new file to repository for commit
git add <file name/directory name>
- Exclude files and directory from repository using .gitignore.
touch .gitignore git add .gitignore echo "build/*" >> .gitignore
- Commit the changes
git commit -m "<comments for commit>"
- Push local changes to github repository
- Push changes from all local branches to matching branches the origin remote.
git push origin
- Push changes from the local master branch to the remote master branch.
git push origin master
- Push changes from the local master branch to the remote staging branch if it exists.
git push origin master:staging
- Push changes from all local branches to matching branches the origin remote.
- Pull/Update the changes from git repository to local
git pull
- Fetch all branch for a git repo
git fetch --all
- Create branch in git
git branch <branch name>
- List braches
git branch
- Checkout remote branch
git checkout <remote branch>
- Add a new remote for your branch
git remote add [name_of_your_remote] [name_of_your_new_branch]
- Push changes from your commit into your branch
git push [name_of_your_new_remote] [url]
- Update your branch when the original branch from official repository has been updated
git fetch [name_of_your_remote]
- Apply to merge changes if your branch is derivated from develop you need to do
git merge [name_of_your_remote]/develop
- Delete a branch on your local filesystem
git branch -d [name_of_your_new_branch]
- To force the deletion of local branch on your filesystem
git branch -D [name_of_your_new_branch]
- Delete the branch on github
git push origin :[name_of_your_new_branch]
Git branch with example
- Creating Master and Feature Branches.
Here is the scenario we will create:
In the above example, we are taking the following path:
Commit A: we add a.txt file in the ‘master’ branch
Commit B: we add b.txt file in the ‘master’ branch
At this stage, we create the branch ‘feature’ which means it will have a.txt and b.txt
Commit C: we add c.txt file in the ‘master’ branch
We go to the ‘feature’ branch
Commit E: we modify a.txt in ‘feature’ branch
Commit F: we modify b.txt in ‘feature’ branch
You can create a folder and run the following code inside the folder to create the above situation:
git init
touch a.txt
git add -A
git commit -m "Commit A: added a.txt"
touch b.txt
git add -A
git commit -m "Commit B: added b.txt"
git branch feature
touch c.txt
git add -A
git commit -m "Commit C: added c.txt"
git status
git checkout feature
echo aaa > a.txt
git add -A
git commit -m "Commit E: modified a.txt"
echo bbb > b.txt
git add -A
git commit -m "Commit F: modified b.txt"
- Simple Merge Let’s use the log command to check both branches.
Results for ‘master’:
git checkout master
Switched to branch 'master'
git log --oneline
2bbde47 Commit C: added c.txt
b430ab5 Commit B: added b.txt
6f30e95 Commit A: added a.txt
ls
a.txt b.txt c.txt
Results for ‘feature’:
git checkout feature
Switched to branch 'feature'
git log --oneline
0286690 Commit F: modified b.txt
7c5c85e Commit E: modified a.txt
b430ab5 Commit B: added b.txt
6f30e95 Commit A: added a.txt
ls
a.txt b.txt
Notice how the feature branch does not have Commit C
Now let’s run merge ‘feature’ branch with ‘master’ branch. You will be asked to enter a comment. In the comment, add “Commit G:” at the beginning to make it easier to track.
git checkout master
Switched to branch 'master'
git merge feature
Merge made by the 'recursive' strategy.
a.txt | 1 +
b.txt | 1 +
2 files changed, 2 insertions(+)
Results for ‘master’:
git checkout master
Already on 'master'
git log --oneline
d086ff9 Commit G: Merge branch 'feature'
0286690 Commit F: modified b.txt
7c5c85e Commit E: modified a.txt
2bbde47 Commit C: added c.txt
b430ab5 Commit B: added b.txt
6f30e95 Commit A: added a.txt
ls
a.txt b.txt c.txt
Results for ‘feature’:
git checkout feature
Switched to branch 'feature'
git log --oneline
0286690 Commit F: modified b.txt
7c5c85e Commit E: modified a.txt
b430ab5 Commit B: added b.txt
6f30e95 Commit A: added a.txt
ls
a.txt b.txt
In the ‘master’ branch, you will notice there is a new commit G that has merged the changes from ‘feature’ branch. Basically, the following action has taken place:
In the Commit G, all the changes from ‘feature’ branch have been brought into the master branch. But the ‘feature’ branch itself has remained untouched due to the merge process. Notice the hash of each commit. After the merge, E (7c5c85e) and F (0286690) commit has the same hash on the ‘feature’ and ‘master’ branch.
- Merging with Rebasing Let’s repeat step 1 to create the ‘master’ and ‘feature’ branches again.
Results for ‘master’:
git checkout master
Switched to branch 'master'
git log --oneline
7f573d8 Commit C: added c.txt
795da3c Commit B: added b.txt
0f4ed5b Commit A: added a.txt
ls
a.txt b.txt c.txt
Results for ‘feature’:
git checkout feature
Switched to branch 'feature'
git log --oneline
8ed0c4e Commit F: modified b.txt
6e12b57 Commit E: modified a.txt
795da3c Commit B: added b.txt
0f4ed5b Commit A: added a.txt
ls
a.txt b.txt
Let’s rebase from the ‘feature’ branch.
git checkout feature
Switched to branch 'feature'
git rebase master
First, rewinding head to replay your work on top of it...
Applying: Commit E: modified a.txt
Applying: Commit F: modified b.txt
Then merge ‘feature’ into ‘master’.
git checkout master
Switched to branch 'master'
git merge feature
Updating 7f573d8..9efa1a3
Fast-forward
a.txt | 1 +
b.txt | 1 +
2 files changed, 2 insertions(+)
Results for ‘master’ branch:
git checkout master
Already on 'master'
git log --oneline
9efa1a3 Commit F: modified b.txt
8710174 Commit E: modified a.txt
7f573d8 Commit C: added c.txt
795da3c Commit B: added b.txt
0f4ed5b Commit A: added a.txt
ls
a.txt b.txt c.txt
Results for ‘feature’ branch:
git checkout feature
Switched to branch 'feature'
git log --oneline
9efa1a3 Commit F: modified b.txt
8710174 Commit E: modified a.txt
7f573d8 Commit C: added c.txt
795da3c Commit B: added b.txt
0f4ed5b Commit A: added a.txt
ls
a.txt b.txt c.txt
Notice that after the rebase and merge both branches are the same. Also, the hashes for E and F have changed in both branches. Basically, in the rebase scenario, this is what happened:
That’s why there is no new commit. The E and F commits have been recalculated and latched to the end of the ‘master’ branch.
Rebasing is a useful tool when you want to clean up the history of your work. However, there is a danger which has given birth to the golden rule.
Golden Rule of Rebasing Never rebase a public branch.
Happy learning.
Leave a comment