Difference between revisions of "Git 101"
Jump to navigation
Jump to search
Line 26: | Line 26: | ||
=== Syncing with Remote Repository === | === Syncing with Remote Repository === | ||
* To | * To download updates from the remote repository without merging them: | ||
<code> | <code> | ||
$ git fetch | $ git fetch | ||
</code> | </code> | ||
* To | * To fetch changes from the remote repository and merge them into your current branch: | ||
<code> | <code> | ||
$ git pull | $ git pull | ||
</code> | </code> | ||
* To push local commits to the remote repository: | * To push local commits to the remote repository: | ||
<code> | <code> | ||
$ git push | $ git push # pushes your commits to the remote repository | ||
</code> | </code> | ||
* To view the remote repository information: | * To view the remote repository information: | ||
<code> | <code> | ||
$ git remote -v | $ git remote -v # lists the remote repositories and their URLs | ||
</code> | |||
=== Viewing Changes and Status === | |||
* To see the status of the working directory and staging area: | |||
<code> | |||
$ git status | |||
</code> | |||
* To list the commit history: | |||
<code> | |||
$ git log | |||
</code> | |||
* To view differences since the last commit: | |||
<code> | |||
$ git diff <file> | |||
</code> | |||
* To see tracked files: | |||
<code> | |||
$ git ls-files | |||
</code> | </code> | ||
=== Committing Changes === | |||
* To commit a single tracked file (file to staging area and commit changes in two steps): | |||
$ git add <file> | |||
$ git commit -m 'commit message' | |||
* To commit all tracked files at once, use option <code>-a</code> (add to staging area and commit in a single step): | |||
<code> | |||
$ git commit -am 'commit message' | |||
</code> | |||
== | == More details == | ||
=== Git Fetch vs. Git Pull === | |||
* <code>git fetch</code> is a command that '''downloads''' changes from a remote repository, but '''doesn't integrate any of these changes''' into your working files. It's essentially a safe way to review changes before integrating them into your local repository. | |||
* <code>git pull</code> is a command that not only '''downloads''' changes from the remote repository but also immediately attempts to '''merge''' them into the branch you are currently working on. It is a combination of <code>git fetch</code> followed by <code>git merge</code>. | |||
== Basic Workflow Example == | == Basic Workflow Example == |
Revision as of 17:25, 21 November 2023
Git Workflow Components
- Working Directory: Your local workspace where you edit files. Changes here are not tracked until moved to the staging area.
- Staging Area: A prep zone for changes to be committed. You can selectively choose which changes to include in a commit.
- HEAD: The latest commit in the current branch, acting as a pointer to your most recent work.
- Local Repository: Your computer's storage for all your commits, branches, and the entire change history. It operates independently of network access.
- Remote Repository: A server-hosted repository (e.g., GitLab, GitHub) for code sharing and backup. It syncs with the local repository through
push
andpull
commands.
Git File Status
Tracked vs Untracked Files
- Tracked files are those that Git knows about and has in its version history.
- Untracked files are new or unrecorded files in your working directory that Git isn't keeping track of yet; e.g.
temp/
,auto_settings.sav*
,auto_positions.sav*
, etc.
Staged vs Unstaged
- Staged files are those that have been marked for inclusion in the next commit, showing Git exactly what changes you want to commit.
- Unstaged files are the modified files in your working directory that have not been marked for the next commit yet.
Basic Commands Cheat Sheet
Syncing with Remote Repository
- To download updates from the remote repository without merging them:
$ git fetch
- To fetch changes from the remote repository and merge them into your current branch:
$ git pull
- To push local commits to the remote repository:
$ git push # pushes your commits to the remote repository
- To view the remote repository information:
$ git remote -v # lists the remote repositories and their URLs
Viewing Changes and Status
- To see the status of the working directory and staging area:
$ git status
- To list the commit history:
$ git log
- To view differences since the last commit:
$ git diff <file>
- To see tracked files:
$ git ls-files
Committing Changes
- To commit a single tracked file (file to staging area and commit changes in two steps):
$ git add <file> $ git commit -m 'commit message'
- To commit all tracked files at once, use option
-a
(add to staging area and commit in a single step):
$ git commit -am 'commit message'
More details
Git Fetch vs. Git Pull
git fetch
is a command that downloads changes from a remote repository, but doesn't integrate any of these changes into your working files. It's essentially a safe way to review changes before integrating them into your local repository.
git pull
is a command that not only downloads changes from the remote repository but also immediately attempts to merge them into the branch you are currently working on. It is a combination ofgit fetch
followed bygit merge
.
Basic Workflow Example
To add new files:
$ git add <newfile>
To modify files:
$ git add <modifiedfile>
To commit changes:
$ git commit -m 'Description of changes'
To push to the remote repository:
$ git push origin <branch>
Ignoring Files
.gitignore
lists files and folders to be ignored. To update the list, just use any file editor.