DEV Community

Cover image for Survival Manual: How to Create and Manage a Project in Git
Daniele Minatto
Daniele Minatto

Posted on

Survival Manual: How to Create and Manage a Project in Git

Git is an essential tool for developers who want to efficiently manage version control for their projects. In this post, we'll explore how to start a project in Git and perform basic operations such as adding a remote repository, making commits, and synchronizing changes. Let's get started!

github

Initializing a Git Repository

To create a new project in Git, the first step is to initialize a repository. This can be done with the following command:

$ git init
Enter fullscreen mode Exit fullscreen mode

This command creates an empty Git repository in the current directory. For example, when you run git init in the directory c:/Users/Fulano/Posts/, you will see the following message:

Initialized empty Git repository in c:/Users/Fulano/Posts/.git/
Enter fullscreen mode Exit fullscreen mode

Adding a Remote Repository

After initializing the repository, you may want to connect it to a remote repository on GitHub or another Git hosting service. To do this, use the git remote add origin command followed by the URL of the remote repository:

$ git remote add origin git@github.com:fulano/30days.git
Enter fullscreen mode Exit fullscreen mode

If you need to change the remote repository, you can add another one:

$ git remote add origin git@github.com:fulano/the30day.git
Enter fullscreen mode Exit fullscreen mode

Checking the Repository Status

To see what is staged for addition or what has been modified, use the command:

$ git status
Enter fullscreen mode Exit fullscreen mode

This command shows the current state of the repository, including modified and untracked files.

Making Commits

After adding or modifying files, you may want to save these changes to the repository. To do this, use the git commit command with a descriptive message:

$ git commit -m "Here I add a message, like: committing the first post"
Enter fullscreen mode Exit fullscreen mode

Pushing Changes to the Remote Repository

To push your changes to the remote repository, use the git push command. The -u flag sets the default branch for the push:

$ git push -u origin master
Enter fullscreen mode Exit fullscreen mode

If you need to force the push (for example, after rewriting history), use:

$ git push -f origin master
Enter fullscreen mode Exit fullscreen mode

Synchronizing with the Remote Repository

To bring updates from the remote repository to your local repository, you can use git fetch or git pull.

  • git fetch: Downloads the headers (HEADs) and updates local references without merging with the local branch. It's useful to see what has changed in the remote repository:
$ git fetch git@github.com:fulano/books.git
Enter fullscreen mode Exit fullscreen mode
  • git pull: Incorporates changes from the remote repository into the local branch. It is equivalent to git fetch followed by git merge FETCH_HEAD:
$ git pull git@github.com:fulano/30days-30sites.git
Enter fullscreen mode Exit fullscreen mode
  • git pull --rebase: Updates the local repository by rewriting history to maintain a linear history:
$ git pull --rebase git@github.com:fulano/30days-30sites.git
Enter fullscreen mode Exit fullscreen mode

Conclusion

These are the basic commands to start working with Git. Over time, you will become familiar with more features and be able to manage your projects even more efficiently.

These commands are some of the most basic and important ones you'll use when working with Git. I always keep a file with these commands handy in case I forget any of them.

Remember to always keep your repository updated and make frequent commits to ensure your work is safe and well-documented

If you have any other essential Git commands that I might have missed, feel free to share them in the comments! Your input could be helpful to others in the community :)

Top comments (0)