I am going to share the basic commands which we use for git, no extra stuff.
As you all know git is used to maintain the code/project information(called repository or repo). It is actually started by Linus Torvald for maintaining Linux kernel.
He is awesome, along with the kernel development, he thought a lot about future and its maintenance.
1st step of git:
Now coming to the first step of git usage, we are being assigned to a team. they maintain their project in a git server and
they will be providing the git server details and git repo details. Your job is to first clone (get the code) the repository into your development PC.
The administrator might have already provided you access to git server and the git server may be a secure server or non-secure server. If non-secure server, you can skip the secure git server stuff.
I came across secure git server using public and private keys. So if we have to clone the repository from the this secure git server. Its quite simple, we have to generate the public and private key pair and configure the git server with the public key.
Following steps help in configuring public key:
- I am assuming that you will be using Linux OS/MAC OS /MobaXterm in windows/Ubuntu for Windows and installed SSH on it.
Use “ssh-keygen” or an equivalent command to generate a key pair:
>>>>>>>ssh-keygen
output may show like below:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in
Your public key has been saved in /home/user/id_rsa.pub.
The key fingerprint is:
SHA256:YhncXb32voxIm+oHUiI5zI9NFrfUe6VteEmCKQMOlOU user-name
The key’s randomart image is:
+ — -[RSA 2048] — — +
| .ooo. . +. |
| o+o * = ..o |
| o +E= = . B..|
| * * o . +o= |
| % S ..o. |
| o = . .|
| . .. . |
| ..+ o .|
| .oo+ . o.|
+ — — [SHA256] — — -+ - Above command generated public and private key pair, now we can display the public key which was stored in /home/user/id_rsa.pub in my example.
>>>>>>>>cat /home/user/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7ibX/uvqEJdU8mwHKDgvyZYlMNoY42DmD67X0myls1iQw+15h8ov/6SqHM7/UQwukPYFlXELXwUmbpqJX3coh9fbvmnaaOUxPQhvTfpfwT8TonEoy8wSuX+zTzpOEgWEUEtAR46kIpUN932FaaVPe5jroVA+vGqSMxD90kyzxuWWVXMFkhtR6xO7N/nJkfNB1t9xFKP8daPMTnT4a/prdYrft+YcR8KVvs3B5mxQANacnEadzjmCK1N7E4b90jD53Gc9sHi3x+D5uUzlFR/vJK6CgMDmV3+YIhb9HGPowOxPwrScPdvxv2n+9RmF+pJBtsUkGWvpjmjATMCYOm4G9 user-name - Copy the public key and go to git server, login using your credentials,
after login, you can click on your account related icon and search for manage keys link, there you add the key with your public key.
Done. congratulations , you can now access the git server from your development PC.
Cloning the repository:
Now that you can successfully access the git server. You can start cloning the repository.
I am assuming that your development environment is having git installed and you know the repository details.
cloning repository is only one command, that is “git clone repository-details”
Below is one example to clone linux repo:
>>>>>>>git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
Now that you can clone the repository. Happy working and Happy development :)
Commit your changes to the git server:
Its good that you could do some development in your development PC.
Now we need to merge these changes to repository in git server.
Now we will learn those details.
Very simple steps:
- Here we need to understand one point. The repository to the team will be one maintained by few experts in a team.
Everyone in team cannot have direct permissions to operate on the repository other than cloning. This is mainly to protect from mistakes.
Now that we cant directly take the code changes to this repository, we need to have our specific (local) repository in git server.
Very simple, You can use “Fork Repository” option in git server with the option of sync with main repository.
With this, you have your own local repository in git server. - Now that you have the code with your changes on main repo and you have local repo in git server, you can merge your changes also called commit or push your changes to git server onto local repository.
Assuming that you know your local repository (or repo) details. If not, after fork repo, if you click on that repo and clone, you get the local repo details(local-repo-url).
Now you add remote name in your local directory for accessing local repository using the command called “git remote add”.
>>>>>>>>git remote add local_repo local-repo-url
note instead of local_repo in above command you can specify any other name.
You can view the remote details using “git remote -v” command. It will show the origin and local_repo remote details.
- Now you can use “git status” and “git diff” commands to know what changes you have made.
- You are now familiar with the changes you made and you need to merge to git server. You can create your own branch and go to that branch to merge your changes.
branch creation and branch moving is very simple , just 2 commands:
>>>>>>>>>git branch branch-name - You can view the branches on your local development PC using “git branch” command:
>>>>>>>>>git branch - Now moving to the branch you created, using the command “git checkout”:
>>>>>>>>>git checkout branch-name - Now you created your own local repo and your own branch, you can start using git add/git rm commands to make changes to repo.
“git add path1/file1 path2/file2” command can be used to add a new file or existing file with modifications.
“git add *” command can be used only if you are sure that all modified files to be merged to your branch.
It is also better to use “git add” command with specific files.
“git rm path/file” command to remove a file from the branch. - Now you have made the changes, you have to commit those changes using the “git commit” command.
After entering the git commit, you can add information about your code changes details.
>>>>>>>git commit - Now that you have committed your changes in your development PC , that commit information should be pushed to git server.
“git push” command can be used to push to git server.
e.g:
>>>>>>>>git push -f local_repo branch-name
Now your changes are pushed to your local repo in git server.
- Now suppose you missed some code changes or want to modify commit information, you can still do it.
You can modify file , then use “git status” command , then use “git add” command , then use “git commit — amend” command to alter the last commit you have made in that branch.
>>>>>>>>>git commit — amend
then again to push those changes to the git server , you can use the same “git push” command:
>>>>>>>>>git push -f local_repo branch-name
Congratulations. You have pushed your changes to git server.
Pull request creation:
Now that you have pushed your changes to your local branch in your local repo. Those should be merged to team specific repo (or main repo).
Simple, we have to create a pull request on it. This is to tell the team that you have made code changes, which have to be reviewed and merged to main repo.
Pull request creation is a simple process:
- Go to git server website, select create pull request, select the source as your branch-name and your local repo
and destination name as team suggested branch and team repo.
Congratulations you have created pull request. You are almost done with the task.
Code reviewed and suggested some changes after pull request creation:
It is obvious that team suggests some modifications. Some may be applicable and acceptable. Now you will come back to your development PC and do the modifications.
Then you will be using the below commands same as the one you have used for merging:
>>>>>>>git status
>>>>>>>git add path/file
>>>>>>>git commit — amend
Note that you are making the changes to same old commit , so you have to use “ — amend” option to the “git commit” command.
>>>>>>>>>git push -f local_repo branch-name
Merge pull request:
Now that you have made modifications and pushed, reviewers may ask for more changes. then you will be repeating the above process.
Reviewers approve the pull request , after approval , you can suggest the concerned team member to merge the pull request by adding a comment in pull request.
Congratulations. You now know the git process .
More on git:
https://manpages.debian.org/stretch/git-man/git.1.en.html
and internet