Git is a local version control tool. Git also has an online centralized repository which is known as GitHub.
Getting started
Merge:
Forks are very useful when it comes to accepting public contributions to your repository. One would not want changes to be done to the original repository, hence full access is provided to forked copies. Changes from the forked repository to the seed repository can only be done by the developer who has an write access to both the repositories.
While working with a scenario like this, the need to have multiple named remote repositories might get evident.
$ git remote -v
> origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
> origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
> upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
> upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
In the example above the origin would refer to the clone repository created on GitHub and upstream is another remote link added to the original seed repository. Additional remote repo link is creating using :
$ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
If you are hoping to contribute back to the original repository, you can send a request to the original author to pull your fork into their repository by submitting a pull request.
Getting started
- Once you have installed git you can use the GitBash console to configure the local git.
- git config --global user.name "abc@abc.com'"
- git config --global user.email "abc@abc.com"
- git config --global core.editor "code --wait --new-window"
The global setting are saved in an xml file on the as done above can be viewed using
- git config --edit --global
connecting with the GitHub might require the user name and password to be authenticated each time. This can be avoid by using the SSH connection to the GitHub. To generate an SSH key use the command prompt and type the following command:
- ssh-keygen -t rsa -b 4096 -C "yourmail@domain.com"
Before the next step, please ensure that the service for ssh is running in windows services "Open-SSH authentication agent". Please ensure that that you are in the %userprofile% directory before you issue the next command you can do
- C:\cd %userprofile%
- ssh-add ./.ssh/id_rsa : This command will add the identity created to the ssh agent running
- cd .ssh
- type id-rsa.pub
Goto gitHub-> Profile -> settings and add the contents of the id_rsa.pub to the ssk section and give it a description from which computer this was added. To test if the ssh connection is working fine please use
- ssh -T git@github.com
Concept
======
Git is a distributed source code management software. In centralized VSS tools entire history is on the central server and only the latest copy (checked in version) is in the local directory. In git the entire history of all the versions and check ins is on every local copy as well.
- Branches : These are features being worked on. A branch is created locally and when finalized is pushed. One user can be working on multiple features at the same time and hence can have multiple branches open at the same time.
- Push : process of sending finalized changes to the GitHub
- Pull : process of pulling the latest copies from GitHub
- File states
- Committed : stored locally
- Modified: changed but not committed
- Staged : marked for next commit.snapshot.
- File areas
- Working directory: editing areas
- Staging : files waiting for the commit
- .git repo : committed files
- Repositories: its a folder for your project integrated with git. A new
Git process outline:
=============
- git init : creates a local repository
- git clone: clone a repository into a local directory and create a remote branch
- git add : add a file into the staging area
- git commit: commit the changes to the local repository
- git push origin master: push the changes from the local master to the origin (remote repo)
- git pull remote master:
Branches:
=======
In a nutshell a branch is parallel copy of the original source code which can at sometime be merged into the original source. In traditional system this was done using a copy of the source being hosted as a separate repository.
Git is based on snapshot (differential and not incremental changes) and hence its easier for Git to implement this concept. Each commit creates a differential snapshot and files changed are marked in the snapshot. If a file is not changed it remains pointed to the original commit pointer. Hence a branch is also a pointer to one of the commits
The master is the default branch and it always points to the last commit made. It also automatically moves forward when a commit is made.
Branches should be used to work on a feature and should only be committed to the master once completed and tested. To avoid conflicts the branches should be logically arranged to avoid the same object in multiple branches, this would ease the merge operations.
- git branch <branchName>: This command is used to create a new local branch.
- git branch -a : This command will give a list of all the local and remote branches.
- git branch -vv: This command will give the local branches and their corresponding upsteam branches on remote repo.
- git checkout : At any given point there can be multiple branches that are active and the active branch can be changed using checkout command. If the branch is missing it will get created.
- git checkout -- . :this command can be used to discard all the unstaged changes and revert to the last committed stage on the current branch.
- git push -u origin <newbranch>: This command pushes the branch created locally to GitHub make sure the branch name is same locally and on remote.
- git fetch : This command is used to fetch any branch from GitHub to the local repository.
- git pull : this is used to merge branches. A pull request is from the owner of the branch to the owner of the master branch (collaborators) to merge the changes.
- git log --all --oneline --decorate --graph: This command can be used to look at all the commit log and branches there are.
- git --rebase: If you don’t want a bunch of commits in a pull request, you may need to use git rebase to “squash the commit history”.
Remote repo
=========
remote repositories are the once located on GitHub. Origin keyword in an alias that is created by default for the remote repository if a name is not specified when we clone it. Alternatively if we initialized a local repo (not cloned), we can link it to a remote repo using:
- git remote add origin https://github.com/santoshkmrsingh/abc.git
we can also have multiple remote alias created (this would be logical when we are using forks (copies) of the same repository)
- git remote add afzMobile https://github.com/Alfazance/mobile.git
given the above two commands we have two remote alias created. These can be seen using
- git remote: refers the link to the master branch on the remote repository.
- git remote -v : list the full details of the remote links
remote tracking branch tells us what the current branch looks like at origin. The remote tracking branches cannot be directly worked at. We will always work with local branches.
- origin/master : refers to the master branch on the remote repository.
- git merge origin/master: merges the current branch with the remote repository
pull and push
==========
pull and push work from the local branch to a corresponding branch on the remote repository. It hence important that before a pull or a push is used, the local repo and branch have a corresponding link to a remote repo and a branch. A push always works from the current local branch to a branch on the remote repo. Hence, it is important that before a push request is issued the correct branch is checked out.
To link the current branch to one on the remote, you can use the following command.
- git branch --set-upstream-to origin/branch: This command is used to link a local branch to a branch on the origin
- git push : this command will push the contents of the current branch to the corresponding branch ont the remote server
- git push -u origin:
branch on the remote repo. If the branch does not exists on the remote repo it is created.
Note: the branch name on the local repo should match the branch name on the remote. You cannot push changes from one branch name in the local directory to another branchname on the remote.
Pull request:
Pull request can be more relevantly called merge request. Once the local contents have been pushed to the remote server the pull request can be used to merge these changes on the master branch.
To have better control we can configure the pull requests branch rules (in settings on GitHub) where we can configure options like merging can only happen through pull requests and not directly through a local merge.
=====
Merge is used whenever there is a conflict i.e more than one user has made a change on the same file and same line. Merge can happen locally or on the server.
If the local timestamp of a file in a branch is older then the one on the server, then the same should be locally merged (using a pull command), before it can be pushed up. For the below commands please assume that we have the master branch checked out
- git merge feature: this command will merge the feature branch into the current branch (master in this case) along with all the commits of the feature branch.
- get merge --squash feature : this will merge the feature branch into the current branch (master) without the commit history of the feature branch.This should be followed by a commit on the master branch git commit -m "squashed commit with features".
- git rebase master: If the current branch has a base in the master, the rebase will bring the latest commit from the master into the current feature branch and reapply all the local commits in the feature branch on top of it. Thus we will have a new base with the latest changes from master.
- git reset --hard <commitid>: This command deletes all the commits post the commitId this can be used to undo any unwanted commits.
- git stash : stash commands can be used to undo and redo changes in the current working tree. This command can be used to revert all the current changes and revert to the last clean working directory status. Each time a stash command is used the changes are pushed to the stash list.
- git stash push -m "comment" : this can be used to push a change to the stash with a custom message.
- git stash list : this can be used to list all the changes that are pushed to the stash.
- git stash apply: will reapply all the changes that are parked in stash. If there are a no of stash on the stack we can choose which one to apply from the list using its index.
- git stash drop <index>: this can be used to drop a given checkpoint from the stack.
- git stash clear : can be used to clear the stack maintained for stash.
Fork:
====
Fork is just a copy of the repository. Any changes to the fork does not impact the original repo. This is however internally still linked to the original repo. All the other commands to interact with a fork are the same as with any GitHub repository.
Forks are very useful when it comes to accepting public contributions to your repository. One would not want changes to be done to the original repository, hence full access is provided to forked copies. Changes from the forked repository to the seed repository can only be done by the developer who has an write access to both the repositories.
While working with a scenario like this, the need to have multiple named remote repositories might get evident.
$ git remote -v
> origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch)
> origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
> upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch)
> upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
In the example above the origin would refer to the clone repository created on GitHub and upstream is another remote link added to the original seed repository. Additional remote repo link is creating using :
$ git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
If you are hoping to contribute back to the original repository, you can send a request to the original author to pull your fork into their repository by submitting a pull request.
NOTES
=====
1. HEAD refers to the changes in the current branch
2. When prompted for message
press "i"
write your merge message.
press "esc"
write ":wq"
then press enter.
3. Create a file not possible using windows explorer (eg. a filename starting with period(.))
touch
4. To see a history of commits
git log --oneline --decorate --graph --all
5. Edit the .gitignore file
That can be done using the vim editor.
Simply type vi [path-to-file]\file and you will open the vim terminal editor.
Press a to toggle the edit mode
Edit stuff
Press Esc to finish edit mode and toggle the command mode
Press/type : to indicate you want to type a command
Type and enter wq to save the changes and exit
If you want to delete all your commit history but keep the code in its current state, please use the following:
Checkout
git checkout --orphan latest_branch
Add all the files
git add -A
Commit the changes
git commit -am "commit message"
Delete the branch
git branch -D master
Rename the current branch to master
git branch -m master
Finally, force update your repository
git push -f origin master
=====
1. HEAD refers to the changes in the current branch
2. When prompted for message
press "i"
write your merge message.
press "esc"
write ":wq"
then press enter.
3. Create a file not possible using windows explorer (eg. a filename starting with period(.))
touch
4. To see a history of commits
git log --oneline --decorate --graph --all
5. Edit the .gitignore file
That can be done using the vim editor.
Simply type vi [path-to-file]\file and you will open the vim terminal editor.
Press a to toggle the edit mode
Edit stuff
Press Esc to finish edit mode and toggle the command mode
Press/type : to indicate you want to type a command
Type and enter wq to save the changes and exit
If you want to delete all your commit history but keep the code in its current state, please use the following:
Checkout
git checkout --orphan latest_branch
Add all the files
git add -A
Commit the changes
git commit -am "commit message"
Delete the branch
git branch -D master
Rename the current branch to master
git branch -m master
Finally, force update your repository
git push -f origin master
No comments:
Post a Comment