0% found this document useful (0 votes)
20 views2 pages

Use Git To Add A File To A Repository

This document outlines the steps to add a file to a Git repository from the command line. It describes changing directories, creating or switching branches, copying the file to the project directory, tracking and committing the file locally, and pushing the changes to the remote repository.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

Use Git To Add A File To A Repository

This document outlines the steps to add a file to a Git repository from the command line. It describes changing directories, creating or switching branches, copying the file to the project directory, tracking and committing the file locally, and pushing the changes to the remote repository.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Use Git to add a file to a repository

To add a new file from the command line:

1. Open a terminal.
2. Change directories until you are in your project’s folder.
3. cd my-project

4. Choose a Git branch to work in.


o To create a branch: git checkout -b <branchname>
o To switch to an existing branch: git checkout <branchname>
5. Copy the file you want to add into the directory where you want to add it.
6. Confirm that your file is in the directory:
o Windows: dir
o All other operating systems: ls

The filename should be displayed.


7. Check the status of the file:
8. git status

The filename should be in red. The file is in your file system, but Git isn’t
tracking it yet.
9. Tell Git to track the file:
10. git add <filename>

11. Check the status of the file again:


12. git status

The filename should be green. The file is tracked locally by Git, but has not
been committed and pushed.
13. Commit the file to your local copy of the project’s Git repository:
14. git commit -m "Describe the reason for your commit here"

15. Push your changes from your copy of the repository to GitLab. In this
command, origin refers to the remote copy of the repository.
Replace <branchname> with the name of your branch:
16. git push origin <branchname>

17. Git prepares, compresses, and sends the data. Lines from the remote
repository start with remote::
18. Enumerating objects: 9, done.

19. Counting objects: 100% (9/9), done.


20. Delta compression using up to 10 threads

21. Compressing objects: 100% (5/5), done.

22. Writing objects: 100% (5/5), 1.84 KiB | 1.84 MiB/s, done.

23. Total 5 (delta 3), reused 0 (delta 0), pack-reused 0

24. remote:

25. remote: To create a merge request for <branchname>, visit:

26. remote: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/new?


merge_request%5Bsource_branch%5D=<branchname>

27. remote:

28. To https://gitlab.com/gitlab-org/gitlab.git

29. * [new branch] <branchname> -> <branchname>

30. branch '<branchname>' set up to track 'origin/<branchname>'.

Your file is copied from your local copy of the repository to the remote repository.

To create a merge request, copy the link sent back from the remote repository and
paste it into a browser window.

Add a file to the last commit


git add <filename>

git commit --amend

You might also like