Push Project to gitHub

HermantoXYZ
7 min readApr 30, 2024

--

Git is a very useful version control system for managing changes in code or software development projects. Here are the initial steps to get started with Git:

  • Install Git: First, make sure you install Git on your computer. You can get it from its official website.
  • Set Up Configuration: After Git is installed, you need to set up user configuration. This includes your username and email address that will be associated with your commits. You can do this with the following commands in the terminal:
git --version
(Cek Version Git App)
# Configure Git

git config --global user.name "Your Name"
git config --global user.email "email@example.com"
git config --list

This command will display all the configuration settings along with their values, including user settings, repository settings, and system-wide settings.

  • Create a Repository: Now, you can start using Git in your project. Navigate to your project directory via the terminal and run the command git init. This will create a local Git repository in that directory.
  • The .gitignore file is used to specify intentionally untracked files that Git should ignore. This is particularly useful when you have files or directories in your project that you don’t want to include in version control, such as temporary files, compiled code, logs, or sensitive information like passwords. Save the changes and exit Nano. In Nano, you typically save by pressing Ctrl + O, then confirm with Enter, and exit by pressing Ctrl + X. The nano .gitignore command is used to create or edit the .gitignore file using the Nano text editor. This bellow picture, example code .gitignore or:
echo > .gitignore

#Create .gitignore
 /gambar
/file/index.html
Pict after write git .ignore
  • git status When you run git status, Git will provide information such as: Files that have been modified but not yet staged for commit.
    Files that have been staged for commit.
    Untracked files (i.e., files that Git is not currently tracking).
git status
  • Add Files: Once the repository is created, you can add files to it with the command git add filename. This will stage the files to be committed.
    Here’s how it works:
  1. Adding Specific Files: You can add specific files to the staging area by specifying their paths. For example:
git add myfile.txt

or

git add -f file/myfile.txt

Make sure you really want to add this file to version control before using the `-f` flag, as ignored files are often excluded for a reason.

<For example:>
- To stage a single file named myfile.txt, you would run: git add myfile.txt.
- To stage all files in the current directory, you can use a wildcard: git add *.
- To stage all files in the current directory and its subdirectories, you can use: git add . (the dot represents the current directory).

2. Adding All Changes: You can also add all changes in the working directory to the staging area using a wildcard:

git add .

3. Interactive Mode: Git also provides an interactive mode for adding changes, allowing you to review each change before staging it. You can enter interactive mode using:

git add -i

4. Adding Parts of Files (Patch Mode): If you only want to stage specific parts of changes within a file, you can enter patch mode with:

git add -p

5. Removing Files: Additionally, git add can be used to stage removals. If you delete a file and want to stage that deletion, you can run:

git add -u
  • Commit Changes: After adding files, you need to commit those changes. Run the command git commit -m "Commit message". The commit message is a short message explaining the changes you made.
git commit -m "commit message"
Output: git commit -m “commit message” & git add .
  • Changes file:

Example: I will update file file/index.html

<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>

</body>
</html>
Output git status after update update file file/index.html
git add . / git status / git commit -m “ditambah”
  • Git historical In Git, the log command is used to track a branch’s commit history. After running, it will display project-related information, such as a detailed commit history, which can be used as a reference to go back in time to a previous version of the project.
git log
git log
git log — oneline

Here’s a basic guide on how to get started:

It looks like you’re trying to add a remote repository to your Git project with the command git remote add origin https://github.com/hermantoXYZ/disini.git. This command associates the name "origin" with the URL of the remote repository located at https://github.com/hermantoXYZ/disini.git.

git remote add origin https://github.com/hermantoXYZ/disini.git
  1. Creating a repository on GitHub is an essential step in collaborating on projects, sharing your code, or simply backing up your work. Now that you’ve created the repository learngit on GitHub, you can start adding files, making commits, and pushing changes to it from your local machine.
git clone https://github.com/hermantoXYZ/learngit.git

2. Add Files and Make Changes: Once you’ve cloned the repository, you can add files, make changes, and create new files as needed. You can use any text editor or IDE to make changes to your files.

git add myfile.txt

or

git add -f file/myfile.txt

3. Stage and Commit Changes: After making changes to your files, you’ll need to stage them for commit using git add, and then commit them using git commit. For example:

git commit -m "commit message"

4. Push Changes to GitHub: Finally, you’ll push your committed changes to GitHub using the git push command:

git push origin master

5. Pull Changes from GitHub: If you’re collaborating with others or working on multiple machines, you may need to pull changes from GitHub before pushing your own changes. You can do this using the git pull command:

git pull origin master
echo "#padinusantara" >> README.md 

Problem Solving
User From https://github.com/hermantoXYZ/belajargitmaha * branch master -> FETCH_HEAD fatal: refusing to merge unrelated histories PS D:\REACT NATIVE\WebApp\Test>

git merge --allow-unrelated-histories origin/master

git pull origin main --allow-unrelated-histories

or

switch branch..

Git Branch

In Git, a branch is a new/separate version of the main repository.

  • Check list branch local
git branch
  • Check list branch remote
git branch -r
  • Switching Between Branches : Now let’s see just how quick and easy it is to work with different branches, and how well it works.
git checkout name_branch
  • So I will create a new branch:
git checkout -b emergency-fix

Acces repository to a defferent remote

It seems like you’re trying to update the URL of the remote repository named “origin” to a new URL. The command you provided, git remote set-url origin https://github.com/hermantoXYZ/repository-baru, accomplishes that. It changes the URL associated with the remote repository named "origin" to https://github.com/hermantoXYZ/repository-baru.

This command is useful if you want to point your local repository to a different remote repository while keeping the same remote name (“origin” in this case).

git remote set-url origin https://github.com/hermantoXYZ/repository-baru

Submodules Command

When you run the git submodule add command, the second argument is the local location where you want to store the submodule. The error message indicates that Git considers the local location to be outside the main repository.

You can try using a relative or absolute path within the main repository, like this:

git submodule add https://github.com/hermantoXYZ/django-blog django
git add .
git status
git commit -m "new folder"
git push origin main
git pull origin main
git submodule add https://github.com/hermantoXYZ/django-blog django
git status
git commit -m “new folder”
git push origin main

Update SubModules

cd ..
git submodule update --remote
or
git submodule update --remote --recursive
 git commit -m "baru ditambahkan dalam module"
 git push origin main

--

--

No responses yet