Pull and Push Workflow: Mastering Git Branching in 3 Easy Steps

Git Pull

Git Pull is a command used to fetch and download content from a remote repository and immediately update your local repository to match that content. It essentially combines two operations: git fetch and git merge.

  • git fetch downloads the latest commits from the remote repository.
  • git merge integrates these fetched commits into your local branch.

Importance of Git Pull:

  • Synchronisation : Keeps your local repository up-to-date with the remote repository.
  • Collaboration: Enables seamless teamwork by accessing and integrating changes from others.
  • Conflict Prevention: Minimises  conflicts by regularly merging changes.
  • Preserving Work: Avoids overwriting changes and ensures that everyone’s work is preserved.
  • Organised  History: Maintains a clean and understandable project history.

Git Push

Git Push is a command used to upload local repository content to a remote repository. This is essential for sharing your changes with others or for syncing your local development with a public or shared repository. By pushing your commits, you’re essentially making your work accessible to others and creating a backup of your changes on a remote server.

Importance of Git Push:

  • Sharing Changes: Allows you to share your work with others on the remote repository.
  • Collaboration: Enables collaborative development by making your changes accessible to team members.
  • Backup: Creates a remote backup of your code, protecting it from accidental loss.
  • Deployment: Facilitates the deployment of your code to production or staging environments.
  • Version Control: Contributes to the project’s version history, making it easier to track changes and revert to previous versions.

Pull a Branch from GitHub

Now continue working on our new branch  in our local Git.
Lets pull  from our GitHub repository again so that our code is up-to-date:

git pull
* [new branch] branch-2 -> origin/branch-2
Already up to date.

 

Now our main branch  is up to date. And we can see that there is a new branch available on GitHub.

Do a quick status check:

git status
On branch master
Your branch is up to date with ‘origin’.
nothing to commit, working tree clean

 

And confirm which branches we have, and where we are working at the moment:

git branch
* master

 

So, we do not have the new branch  on our local Git. But we know it is available on GitHub. So we can use the -a option to see all local and remote branches:

git branch -a
* master
remotes/origin/branch-2
remotes/origin/master

Note: Branch -r is for remote branches only.

We see that the branch branch-2 is available remotely, but not on our local git. Lets check it out:


git checkout html-skeleton
(Switched to a new branch ‘html-skeleton’)
(Branch ‘html-skeleton’ set up to track remote branch ‘html-skeleton’ from ‘origin’.)

And check if it is all up to date:

git pull
(Already up to date.)

Now, open your favourite editor and confirm that the changes from the GitHub branch carried over.
That is how you pull a GitHub branch to your local Git.

Push a Branch to GitHub

Let’s try to create a new local branch, and push that to GitHub.
Start by creating a branch, like we did earlier:

git checkout -b branch-3
(Switched to a new branch ‘branch-3’)

And we make some changes to the multiply.java  file. Just add a new line.

So now we check the status  of the current branch.


git status
(On branch branch-3)
(Changes not staged for commit:)

 

We see that multiply.java is modified but not added to the Staging Environment:

git add multiply.java

Check the status of the branch:

git status
(On branch branch-3)
(Changes to be committed: )

 

We are happy with our changes. So we will commit  them to the branch :

git commit -m “Updated multiply.java”
(1 file changed, 1 insertion(+))

 

Now push the branch from our local Git repository, to GitHub, where everyone can see the changes

git push origin branch-3

 

Go to GitHub, and confirm that the repository has a new branch:

 

push and pull in git

Git Pull from GitHub 

Pull used to Keep up-to-date with Changes

When working as a team on a project, it is important that everyone stays up to date. when you start working on a project it is important to stay updated with all recent changes made by other members of your team to the project.

Pull is combination of two commands

  • fetch
  • Merge

Git Fetch

Fetch gets all the change history of a tracked branch/repo.

So, on your local Git, Fetch updates to see what has changed on GitHub:

git fetch origin

Now that we have the recent changes, we can check our status:

git status
On branch master
Your branch is behind ‘origin/master’ by 1 commit, and can be fast-forwarded.
(use “git pull” to update your local branch)

 We are behind the origin by 1 commit . That should be the updated README.md, but lets double check by viewing the log:

git log origin

verify by showing the differences between our local master  and origin

git diff origin

 Now we can safely merge.

Git Merge

Merge combines the current branch, with a specified branch.
We have confirmed that the updates are as expected, and we can merge our current branch (master) with origin:

git merge origin

Check our status again to confirm we are up to date:

git status
(On branch master)
(Your branch is up to date with ‘origin’.)
(nothing to commit, working tree clean)

now your local git is up to date

Git Pull

But what if you just want to update your local repository, without going through all those steps?

Pull is a combination of fetch and merge. It is used to pull all changes from a remote repository into the branch you are working on.

Make another change to the Readme.md file on GitHub.

Use pull  to update our local Git:

git pull origin

hat is how you keep your local Git up to date from a remote repository. In the next chapter, we will look closer at how push works on GitHub.

 

Git Push to GitHub

Let’s try making some changes to our local git and pushing them to GitHub.

After the changes have made, Commit the changes:

git commit -a -m “Updated demo.java.added a new variable”

And check the status:

git status

Now push our changes to our remote origin:

git push origin
(Enumerating objects: 9, done.)
(Counting objects: 100% (8/8), done.)
(Delta compression using up to 16 threads)
(Compressing objects: 100% (5/5), done.)
(Writing objects: 100% (5/5), 578 bytes | 578.00 KiB/s, done.)
(Total 5 (delta 3), reused 0 (delta 0), pack-reused 0)
(remote: Resolving deltas: 100% (3/3), completed with 3 local objects.)

Go to GitHub, and confirm that the repository has a new commit:

Now, we are going to start working on branches on GitHub.

 

 

 

Don’t  forget to check out our LinkedIn page –Click Here

Want to learn about git more – Click here

If you using visual studio code instead of terminal visit the link for Microsoft tutorial –Click here