20

Git Basics

 4 years ago
source link: https://towardsdatascience.com/git-basics-ec81696be4e6?gi=7e52e314ff45
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

y26vy2a.jpg!web

Photo by Yancy Min on Unsplash

Git is used as a version control software — used to maintain multiple versions of software.

But now, used for much more than that — collaborating with others, code versioning, sharing, deploying code from local machine to staging server, i.e., “deploying code to the cloud” and so on and so forth.

What does Git do?

  • Suppose starting out with the first version of code, Git allows us to have access not just to the latest version of our code but all the lines added/removed are tracked and we know all about the history of our project.
  • Git is very good at synchronising code between different people. Git stores the code in a remote server and so other people we are working with have access to the same code. When multiple people make changes to the code, Git retains an updated version of the code that merges all the changes made, so everyone in the team is on the same page.
  • Git is very good at testing code. While making changes to the code and testing to see how well/if it works, Git allows us to maintain an original copy of the code. We can revert to it after testing new pieces of code or if happy with the new code, replace it with the original. Hence, Git is great at allowing to revert to old versions of code.

What is the remote server that Git communicates with?

Git communicates with a remote server which hosts repositories (central storage place where all the code is kept and changes are tracked). A number of Git hosting sites include Github, Bitbucket, Gitlab, Beanstalk etc, that are used to store Git repositories on the internet for software development version control. We can “push” our repositories to the remote server so our collaborators and other people on the internet can see and work on those projects as well. Likewise, we can “clone” repositories of projects that we want to work on.

Git Commands

git clone <url>
git add <filename>
git add *
git commit -m "message"
git status

How do we send any changes we make locally to the hosting site?

git push
git pull

Merging conflicts — Git will try to combine all the changes made. But there may be a conflict when multiple changes have been made to the same lines of a particular file which leads to a conflict when we try to run git pull . In such a case, the pull cannot happen since the merge cannot occur. A message appears that says, “automatic merge failed. You need to fix the conflicts and then commit the results.” After deciding what modifications are to be kept, the changes must be pushed.

  • git log : shows a history of all the commits made. Each commit is distinguishable by a unique commit hash.
  • git reset --hard <commit hash> : resets the repository back to the version specified by that commit hash.
  • git reset --hard origin/master : origin/master denotes the version of the repository that was the origin of the repository where we got it from.

Note: Any changes made to the repository on our computer does not affect what’s going on online on the host site unless we push those changes to it.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK