Difference between revisions of "Git 101"

From Beam Line Controls
Jump to navigation Jump to search
(Created page with "== Basic commands == === To ignore a file === <code>.gitignore</code> contains the list of file to be ignore; to ignore a new file/folder, just add it to the list using a te...")
 
Line 1: Line 1:
== Basic commands ==
== Git Workflow Components ==
<br>
[[File:git_101.jpg|thumb|center|900px]]


=== To ignore a file ===


<code>.gitignore</code> contains the list of file to be ignore; to ignore a new file/folder, just add it to the list using a text editor.  
* '''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 <code>push</code> and <code>pull</code> commands.


=== To commit ALL tracked files ===  
== Git File Status ==


Use option <code>-a</code>
=== 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. <code>temp/</code>, <code>auto_settings.sav*</code>, <code>auto_positions.sav*</code>, etc.


$ git commit -am 'commit message goes here'   # single line message
=== Staged vs Unstaged ===
$ git commit -am 'Message                    # multi lines message
* '''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.
$ goes
$ here'


=== To commit ONE file ===


$ git add myfiletocommit        # stage file for commit
$ git commit myfiletocommit    # commit file


=== To see the difference since last commit ===  
== Basic Commands Cheat Sheet ==


$ git diff somefile


=== To push to remote repository ===
=== Syncing with Remote Repository ===
* To update your local repository to match the remote repository, use:
<code>
$ git fetch  # fetches updates made in the remote repository
</code>


* To apply changes fetched from the remote repository to your working directory:
<code>
$ git pull  # fetches and merges changes from the remote repository to your current branch
</code>
* To push local commits to the remote repository:
<code>
$ git push  # pushes your commits to the remote repository
</code>
* To view the remote repository information:
<code>
$ git remote -v  # lists the remote repositories and their URLs
</code>
=== Committing Changes ===
* To commit a single tracked file:
$ git add &lt;file&gt;  # add file to staging area
$ git commit -m 'commit message'  # commit file changes
* To commit all tracked files at once, use option <code>-a></code>:
$ git commit -am 'commit message'  # add all tracked files to staging area & commit them
=== Viewing Changes and Status ===
* To view differences since the last commit:
$ git diff &lt;file&gt;
* To see tracked files:
$ git ls-files
=== Syncing with Remote Repository ===
* To push local commits to the remote repository:
  $ git push
  $ git push


=== To see the tracked files ===


To display files under version control:


$ git ls-files
 
 
== Basic Workflow Example ==
 
To add new files:
<code>
$ git add &lt;newfile&gt;
</code>
 
To modify files:
<code>
$ git add &lt;modifiedfile&gt;
</code>
 
To commit changes:
<code>
$ git commit -m 'Description of changes'
</code>
 
To push to the remote repository:
<code>
$ git push origin &lt;branch&gt;
</code>
 
 
=== Ignoring Files ===
<code>.gitignore</code> lists files and folders to be ignored. To update the list, just use any file editor.

Revision as of 17:14, 21 November 2023

Git Workflow Components


Git 101.jpg


  • 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 and pull 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 update your local repository to match the remote repository, use:

$ git fetch # fetches updates made in the remote repository

  • To apply changes fetched from the remote repository to your working directory:

$ git pull # fetches and merges changes from the remote repository to your current branch

  • 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




Committing Changes

  • To commit a single tracked file:
$ git add <file>   # add file to staging area
$ git commit -m 'commit message'  # commit file changes
  • To commit all tracked files at once, use option -a>:
$ git commit -am 'commit message'  # add all tracked files to staging area & commit them


Viewing Changes and Status

  • To view differences since the last commit:
$ git diff <file>
  • To see tracked files:
$ git ls-files


Syncing with Remote Repository

  • To push local commits to the remote repository:
$ git push



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.