-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbranching_and_merging.txt
118 lines (93 loc) · 5.63 KB
/
branching_and_merging.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
From experience, you will probably know it is entirly possibly to screw up your code and have no idea how to unscrew it.
This is the power of GIT. Branching allows you to switch to a branch (copy) of your project, while retaining the MASTER branch.
This allows you to develop a new feature or fix without messing up the master branch.
Best practice encourages short lived branches with small changes that are quickly merged back to the master branch. For more detail:
REMEMBER:
- Don't give branches and folders the same name
- You can't switch branches if you have any uncommitted changes that conflict with the branch you’re trying to switch to
- MASTER is just a branch name given by 'git init' when a repo is created (sometimes you might see MAIN or DEFAULT)
- NEVER change the name of the MASTER/MAIN/DEFAULT branch, it really screws up the repo history in collaborations
- HEAD refers to a pointer that points at the current branch you are working on, you move it when you use 'git switch' (old: checkout)
See: https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell
https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_advanced_merging
#####################
# Creating a branch #
#####################
# Lets set up a branchtest_repo to play around with branches
mkdir branchtest_repo && cd branchtest_repo
git init
touch test.txt && echo "First Line: This is the master branch" >>test.txt
git add . && git commit -m 'initial commit' && git status -s
# Now view all branches and then create + switch to a new branch, and view again. See the difference?
git branch -vv
git switch --create branchname
git branch -vv
# Now edit the test.txt file and commit
touch test.txt && echo "Second Line: This is my_first_branch" >>test.txt
git add . && git commit -m 'Added a second line'
git status -s
# Now compare the test.txt file of both branches, can you explain what happened?
cat test.txt
git switch - # return to previous branch
cat test.txt
So now you have created a branch, modified a file, and committed the modification.
Then when you switched between branches, each branch conserves the files in the state they were in when that branch was last commited.
It is like turning back the clock!
####################
# Pushing branches #
####################
git push -u origin branchname
This command pushes branchname to remote.
The '-u' flag tells git to associate local branch with remote branch (i.e. for tracking) so that 'git pull' will pull all branches and not just MASTER
####################
# Merging a branch #
####################
# Remember: Always check the remote for new commits before pushing anything
git switch branchname
git pull origin branchname # or fetch/merge
# Next, while switched to master, merge any branches not merged with master
git branch --no-merged # view branches not merged with current branch
git merge branchname # merge branchname into current branch
git branch --merged # to view branches merged with current branch
# Finally, push to remote
git push remote branchname
# Merged branches should always be deleted locally and on the remote
git branch --delete branchname
git push origin --delete branchname
#################################
# Stashing branch modifications #
#################################
Sometimes you may be making some modifications and are not ready to commit them but you need to switch branches or temporarily go back to where you started.
Git allows you to temporarily store the modifications for you to retrieve later.
Then you return the branch to the last commit before the modifications, so you can go do whatever it is you needed to do.
Remember: When retrieving the stash, make sure you are back on the branch you stashed from
git stash save "leave a detailed informative message" # creates a stash
git stash list # shows your modifications are stored in a stash
# notice the stash reference is stash@{0}
# previous stashes will have moved down the stash list
git stash pop # retrieves the stash, and deletes it from stash list
# NB: retrieves stash@{0} only
git stash apply stash_ref # retrieve a specified stash from stash list
# but does not delete the stash from stash list
git stash drop stash_ref # delete (forever) a specified stash from stash list
git stash drop clear # delete (forever) all stashes in the stash list
######################
# Cleaning your repo #
######################
Try to keep your repository free from clutter
'$git clean' does this by by deleting untracked and ignored files/directories
But... it DELETES UNTRACKED AND IGNORED FILES/DIRECTORIES! If you have stuff you need, they'll get deleted! (Been there... Aagggh!)
See: https://travisjeffery.com/b/2011/12/keeping-your-git-repo-clean-of-orig-files/
https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/How-to-use-the-git-clean-command
https://panjeh.medium.com/cleaning-up-git-github-repository-without-deleting-git-directory-c86b7415b51b
git clean -n # ALWAYS FIRST! dry run, tells you what will be deleted
git clean -f # CAREFUL! force, deletes untracked files
git clean -d # CAREFUL! deletes untracked directories
git clean -x # CAREFUL! deletes ignored files
git clean -fdx # CAREFUL! deletes all untracked files/directories and ignored files
###########################
# Other relevent commands #
###########################
git log --oneline --decorate --graph --all # shows all local branches + pointers
git branch --move oldbranchname newbranchname # renames a branch (don't rename MASTER/MAIN/DEFAULT)