Skip to content

Instantly share code, notes, and snippets.

@AliciaMoses
Last active July 4, 2024 08:22
Show Gist options
  • Save AliciaMoses/127680bcc092ddfed4053fb0ce7ab1f2 to your computer and use it in GitHub Desktop.
Save AliciaMoses/127680bcc092ddfed4053fb0ce7ab1f2 to your computer and use it in GitHub Desktop.

Building Git Workflows

We'll now experiment with various commands which form a basic workflow

✨ Making changes and staging them

After each step view the status with git status

# 1. create a file with content 

echo "one" > numbers.txt

# 2. stage the file (add it to the index)

git add .  # or you can use git add numbers.txt

# 3. unstage the file (remove it from the index) - git gives you a massive hint as to how to do this!

⌛ We'll pause here for more discussion


✨ Making making commits

# 1. Add the file to the staging area again

# 2. Create a commit with the message "first commit"

git commit -m "first commit"

We have created the first commit in the repository, this is the root commit

[main (root-commit) 55e1343] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 numbers.txt

Git automatically gave us info on the commit that's just been created

[<branch> (root-commit) <commit hash>] <commit message>
 1 file changed, 1 insertion(+)
 create mode <file permissions code> numbers.txt

Let's view the working tree to see what it says

# view the working tree
git ls-tree -r HEAD

100644 blob 5626abf0f72e58d7a153368ba57db4c673c0e171 numbers.txt

View the content of the blob

git cat-file -p 5626abf0f72e58d7a153368ba57db4c673c0e171

View the contents of the commit

git show <commit hash>

commit 55e1343833c4c73f4792ce642a585127d0abf05d (HEAD -> main)
Author: AliciaMoses <email>
Date:   Mon Apr 1 03:33:48 2024 +0100

    first commit
    
diff --git a/numbers.txt b/numbers.txt
new file mode 100644
index 0000000..5626abf
--- /dev/null
+++ b/numbers.txt
@@ -0,0 +1 @@
+one
(END)

Note that the SHA hash can be abbreviated and still used to search for objects

Let's view the commit graph

git log --graph --oneline

⌛ We'll pause here for more discussion


✨ Stashing changes

echo "two" >> numbers.txt 

Comparing the working tree

echo "three" >> numbers.txt

git diff numbers.txt

git status -s

Shows the status of file changes in the working tree and their staging status

We can stash changes for later

git stash

git stash list 

git stash apply stash @{n} # in our case it's 0

git stash

git stash pop

Discarding changes

git reset --hard

git stash list

git clean -df

✨ Creating conflict

git branch my-branch
git branch their-branch
git checkout my-branch

git log --graph --oneline

Currenly all branches point to the same commit

echo "three" >> numbers.txt
git add .                
git commit -m "third commit on my-branch"
git checkout their-branch
echo "3" >> numbers.txt 
git add .             
git commit -m "third commit on their-branch"
 git checkout my-branch
 git merge their-branch  
Auto-merging colours.txt
CONFLICT (content): Merge conflict in numbers.txt
Automatic merge failed; fix conflicts and then commit the result.

✨ Keeping up to date - merging in changes

on main create two new files and commit them
echo "rectangle" >> shapes.txt 
echo "A" >> letters.txt

git add -p

git log --graph --oneline git checkout main

Switch to my-branch and pull in the changes from main

git checkout my-branch

git merge main

When we merge, git creates a merge commit - let's see what has happened

The branch main points to the last commit made there

The branch my-branch also points to the last commit - the merge commit

 echo "B" >> letters.txt
 git add . 
 git commit -m "Add letter B to letters.txt"    
 
 git checkout main
 
 git merge my-branch
 
 git log --graph --oneline
 
 git show HEAD
 
 git show HEAD^
 
* f94db50 (HEAD -> main, my-branch) Add letter B to letters.txt
*   5e678d0 Merge branch 'main' into my-branch
|\  
| * 42ef31c another new commit on main
| * 6c89df7 new commit on main
* |   0e12a9b Merge branch 'their-branch' into my-branch
|\ \  
| * | fea224c (their-branch) third commit on their-branch
| |/  
* / 78b3555 third commit on my-branch
|/  
* f596643 second commit
* 55e1343 first commit
~

✨ Undoing changes

git reset --hard HEAD^

HEAD is now at 5e678d0 Merge branch 'main' into my-branch

Use reflog to find the commit you want to revert to, to undo undo the merge commit
git reflog
git reflog --date=iso
git reflog --relative-date --all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment