Difference between revisions of "Git 101"
Jump to navigation
Jump to search
| Line 88: | Line 88: | ||
== Git status explained == | == Git status explained == | ||
$ git status | $ git status | ||
| Line 112: | Line 115: | ||
* '''On Branch''': You're currently on the main branch. | * '''On Branch''': You're currently on the <code>main</code> branch. | ||
* '''Branch Status''': Your main branch is up to date with | * '''Branch Status''': Your <code>main</code> branch is up to date with <code>origin/main</code> (the main branch from the remote repository). | ||
* '''Changes to be Committed''': | * '''Changes to be Committed''': | ||
** <code>file1</code> has been modified and <code>file2</code> is a new file, both staged for the next commit. | ** <code>file1</code> has been modified and <code>file2</code> is a new file, both staged for the next commit. | ||
| Line 124: | Line 127: | ||
** <code>temp/</code> and <code>notes.txt</code> are not tracked by Git. | ** <code>temp/</code> and <code>notes.txt</code> are not tracked by Git. | ||
** To track, use <code>git add <file></code>. | ** To track, use <code>git add <file></code>. | ||
** To ignore, add them to the <code>.gitignore</code> file | |||
<br> | <br> | ||
<br> | <br> | ||
== Your branch is ahead of 'origin/main' == | |||
$ git status | $ git status | ||
On branch | On branch main | ||
Your branch is ahead of 'origin/main' by 3 commits. | Your branch is ahead of 'origin/main' by 3 commits. | ||
(use "git push" to publish your local commits) | (use "git push" to publish your local commits) | ||
| Line 136: | Line 142: | ||
* '''On Branch''': You're currently on the | * '''On Branch''': You're currently on the <code>main</code> branch. | ||
* '''Branch Status''': | * '''Branch Status''': | ||
** Your | ** Your <code>main</code> branch is ahead of <code>origin/main</code> by 3 commits. This means you have made commits locally that are not yet in the <code>main</code> branch on the remote repository. | ||
** To synchronize these changes with the remote repository, use <code>git push</code>. | ** To synchronize these changes with the remote repository, use <code>git push</code>. | ||
* '''Working Tree Status''': | |||
** Your working directory is clean, meaning there are no unstaged changes or untracked files. | |||
== Your branch is behind 'origin/main' == | |||
$ git status | |||
On branch main | |||
Your branch is behind 'origin/main' by 2 commits, and can be fast-forwarded. | |||
(use "git pull" to update your local branch) | |||
nothing to commit, working tree clean | |||
* '''On Branch''': You're currently on the main branch. | |||
* '''Branch Status''': | |||
** Your <code>main</code> branch is behind <code>origin/main</code> by 2 commits. This indicates that there are updates on the remote repository that you don't have locally. | |||
** You can fast-forward your local branch to catch up with <code>origin/main</code> using <code>git pull</code>. | |||
* '''Working Tree Status''': | * '''Working Tree Status''': | ||
** Your working directory is clean, meaning there are no unstaged changes or untracked files. | ** Your working directory is clean, meaning there are no unstaged changes or untracked files. | ||
Revision as of 17:45, 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
pushandpullcommands.
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'
Ignoring Files
.gitignore lists files and folders to be ignored. To update the list, just use any file editor.
More details
Git Fetch vs. Git Pull
git fetchis 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 pullis 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 fetchfollowed bygit merge.
Git status explained
$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: file1
new file: file2
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: file3
Untracked files:
(use "git add <file>..." to include in what will be committed)
temp/
notes.txt
- On Branch: You're currently on the
mainbranch. - Branch Status: Your
mainbranch is up to date withorigin/main(the main branch from the remote repository). - Changes to be Committed:
file1has been modified andfile2is a new file, both staged for the next commit.- To unstage, use
git reset HEAD <file>.
- Changes Not Staged for Commit:
file3is modified but not staged.- To stage, use
git add <file>. - To discard changes, use
git checkout -- <file>.
- Untracked Files:
temp/andnotes.txtare not tracked by Git.- To track, use
git add <file>. - To ignore, add them to the
.gitignorefile
Your branch is ahead of 'origin/main'
$ git status On branch main Your branch is ahead of 'origin/main' by 3 commits. (use "git push" to publish your local commits) nothing to commit, working tree clean
- On Branch: You're currently on the
mainbranch. - Branch Status:
- Your
mainbranch is ahead oforigin/mainby 3 commits. This means you have made commits locally that are not yet in themainbranch on the remote repository. - To synchronize these changes with the remote repository, use
git push.
- Your
- Working Tree Status:
- Your working directory is clean, meaning there are no unstaged changes or untracked files.
Your branch is behind 'origin/main'
$ git status On branch main Your branch is behind 'origin/main' by 2 commits, and can be fast-forwarded. (use "git pull" to update your local branch) nothing to commit, working tree clean
- On Branch: You're currently on the main branch.
- Branch Status:
- Your
mainbranch is behindorigin/mainby 2 commits. This indicates that there are updates on the remote repository that you don't have locally. - You can fast-forward your local branch to catch up with
origin/mainusinggit pull.
- Your
- Working Tree Status:
- Your working directory is clean, meaning there are no unstaged changes or untracked files.
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.