Step 1 - Git Remote
Remote repositories allow you to share changes from or to your repository. Remote locations are generally a build server, a team members machine or a centralized store such as Github.com. Remotes are added using the git remote
command with a friendly name and the remote location, typically a HTTPS URL or a SSH connection for example https://github.com/rkidambi11/gitdemo.git or git@github.com:rkidambi11/gitdemo.git
Step 2 - Git Push
When you're ready to share your commits you need to push them to a remote repository via git push
. A typical Git workflow would be to perform multiple small commits as you complete a task and push to a remote at relevant points, such as when the task is complete, to ensure synchronisation of the code within the team.
The git push
command is followed by two parameters. The first parameter is the friendly name of the remote repository we defined in the first step. The second parameter is the name of the branch. By default all git repositories have a master branch where the code is worked on.
Step 3 - Git Pull
Where git push
allows you to push your changes to a remote repository, git pull
works in the reverse fashion. git pull
allows you to sync changes from a remote repository into your local version.
The changes from the remote repository are automatically merge into the branch you're currently working on.
Task
Pull the changes from the remote into your master branch.
Step 4 - Git Log
As described in the previous scenario you can use the git log
command to see the history of the repository. The git show
command will allow you to view the changes made in each commit.
In this example, the output from git log
shows a new commit by "Ranga" with the message "Fix for Bug #1234". The output of git show
highlights the new lines added to the file in green.
Step 5 - Git Fetch
The command git pull
is a combination of two different commands, git fetch
and git merge
. Fetch downloads the changes from the remote repository into a separate branch named remotes/<remote-name>/<remote-branch-name>. The branch can be accessed using git checkout
.
Using git fetch
is a great way to review the changes without affecting your current branch. The naming format of branches is flexible enough that you can have multiple remotes and branches with the same name and easily switch between them.
Stash
Scenario :
Edit the file1.txt and add it
got to remote fil1.txt and edit it from remote.
come to gitbash and run below command
$ git pull origin master ---- >>> See the error
Please commit your changes or stash them before you merge
Paras@DESKTOP-7N7KERE MINGW64 ~/project (master)
$ git pull origin master
From https://github.com/rkidambi11/project
* branch master -> FETCH_HEAD
error: Your local changes to the following files would be overwritten by merge:
file4
Please commit your changes or stash them before you merge.
Aborting
Updating 63d9786..e5c9cef
Paras@DESKTOP-7N7KERE MINGW64 ~/project (master)
$ git push origin +master
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/rkidambi11/project.git
+ 8d08804...e5c9cef master -> master (forced update)