Pushing and Pulling Changes with GitHub
Learn how to push local commits to a remote GitHub repository (`git push`) and pull changes from GitHub to your local machine (`git pull`).
Learn Git and GitHub - Basic Operations
Creating a Local Repository
A local repository is where your project's files and their history are stored on your computer. Think of it as your own private copy of the project. To create one, follow these steps:
- Choose a Project Directory: Decide where you want to store your project files. This could be a new folder or an existing one.
- Open Your Terminal: Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) and navigate to your project directory using the
cd
(change directory) command. For example:cd /path/to/your/project
- Initialize the Repository: Run the following command to turn the directory into a Git repository:
This command creates a hidden directory namedgit init
.git
inside your project directory. This.git
directory is where Git stores all the information about your repository, including your commit history, configurations, and other metadata. Do not modify this directory directly! Let Git manage it.
After running git init
, you should see a message similar to: "Initialized empty Git repository in /path/to/your/project/.git/" This confirms that the repository has been successfully created.
Initializing a New Git Repository, Creating Files, Staging Changes, and Committing Them Locally
Now that you have a repository, let's add some files, stage them, and commit them. This is the basic workflow for tracking changes in Git.
- Create Files: Create some files in your project directory. These can be any type of file (text files, code files, images, etc.). For example, you could create a file named
README.md
using a text editor or the command line:
Then add some text to the file like "My awesome project".touch README.md # On macOS/Linux notepad README.md # On Windows (opens Notepad)
- Check the Status: Use the
git status
command to see which files Git is tracking. Initially, Git will show that your new file is untracked.
You will see output similar to:git status
Untracked files: (use "git add <file>..." to include in what will be committed) README.md
- Stage Changes: Staging is the process of selecting which changes you want to include in your next commit. Use the
git add
command to stage your new file.
To stage all changes in your project (including new and modified files), you can use:git add README.md
After staging,git add .
git status
will show the file as staged for commit.Changes to be committed: (use "git restore --staged <file>..." to unstage) new file: README.md
- Commit Changes: A commit is a snapshot of your project at a specific point in time. Use the
git commit
command to create a commit. You must provide a commit message to describe the changes you made. The message should be concise and informative.
Thegit commit -m "Add README.md file with project description"
-m
flag allows you to specify the commit message directly in the command. You'll get an output like:[master (root-commit) a1b2c3d] Add README.md file with project description 1 file changed, 1 insertion(+) create mode 100644 README.md
Congratulations! You have successfully created a local Git repository, added a file, staged the changes, and committed them. You've taken your first steps towards using version control!