31 Git-Github
31 Git-Github
• When we first initialized our project, the file was not being tracked by
Git. To do that, we use this command git add . The period or dot that
comes after add means all the files that exist in the repository. If you
want to add a specific file, maybe one named about.txt, you use
>git add about.txt
$ git add .
Now our file is in the staged state. You will not get a response after
this command, but to know what state your file is in, you can run the
git status command.
Committed state and Modified state
• A file is in the committed state when all the changes made to the file
have been saved in the local repo. Files in the committed stage are
files ready to be pushed to the remote repo (on GitHub).
• A file in the modified state has some changes made to it but it's not
yet saved. This means that the state of the file has been altered from
its previous state in the committed state.
Staged state
• The next state for a file after the staged state is the committed state.
To commit our file, we use the
>git commit -m "first commit“
• The first part of the command git commit tells Git that all the files staged are
ready to be committed so it is time to take a snapshot.
• The second part -m "first commit" is the commit message. -m is shorthand for
message while the text inside the parenthesis is the commit message.
Push the repository to GitHub
• At this point, I want to add more tasks to the list but I am not yet sure
whether I want them on my main list. So I will create a new branch
called test to see what my list would look like with more tasks
included.
create a new branch
• To create a new branch, run this command:
• $ git checkout -b test .
• I will break it down. checkout tells Git it is supposed to switch to a new
branch. -b tells Git to create a new branch. test is the name of the branch
to be created and switched to.
• After committing your test branch, switch back to the main branch by
running this command:
• $git checkout main.
• You can check all the branches that exist in your repo by running the
• $git branch command.
• Now we can merge the changes we made in the test branch into the main branch by
running.
• $git merge test.
Push new branch to git
• If you would like to push your test branch, switch to the branch using
• $git checkout test and then run
• $ git push -u origin test
To view all branches in repository
• $git branch –a /-r
• -a represents all the braches from local and remote
• It display all the branches in remote repository.
How to Pull a Repository in Git
• To pull in Git means to clone a remote repository's current state into your
computer/repository.
• This comes in handy when you want to work on your repo from a different
computer or when you are contributing to an open source project online.
• Go to GitHub, and on your repository's main page you should see a green
button that says "Code".
• When you click on the button, you should see some options in a dropdown
menu. Go on and copy the HTTPS URL. After that, run
• $git clone YOUR_HTTPS_URL.
• This command pulls the remote repository into your local computer in a
folder